Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6883439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:23:00+00:00 2026-05-27T05:23:00+00:00

i make simple game , and it consist of 2 files , first file

  • 0

i make simple game , and it consist of 2 files , first file is “Alibaba.java” which is extended from JFrame , i used it to display general contents of the game !,

and the second file is “intro.java” which is extended from JPanel , i used it to show intro of the game which include (title & background & person) ,

my problem occured when i tried to add a simple button in the intro ! , i did a function to create the button , but the problem is when i run the game , the button which i added it don’t appear !! , but when i tried add it from the first file which extended from JFrame , its appeared ! ,
so what is the problem in my code ? is JPanel don’t accept JButtons ! or i must create the buttons from the JFrame file ?!

so i need to know how to add Jbutton inside Jpanel instead of add Jbutton in JFrame Direct !!,

this is my samples of my codes which contain the problem :

1st file (Alibaba.java)

package alibaba;

import java.awt.Color;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import javax.swing.JFrame;


public class Alibaba extends JFrame {

    public Alibaba(){
        super("Alibaba");    


        Intro intro =   new Intro();
        this.add(intro);


        GraphicsEnvironment environment =   GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice  device  =   environment.getDefaultScreenDevice();

        device.setFullScreenWindow(this);    
    }

    public static void main(String[] args) { 

        Alibaba alibaba =   new Alibaba();
    }
}

2nd file(Intro.java) :

package alibaba;


import javax.swing.JButton;




public class Intro extends javax.swing.JPanel implements Runnable{

    Thread _intro_run;

    public Intro() {
        _intro_run  =   new Thread(this);
        _intro_run.start();
    }


    @Override
    public void run() {

    // Here i tried to add a button to the Intro !!! 

        this.add(this.createbutton("Exit"));
    }


    public JButton createbutton(String text){

        JButton _button  = new JButton(text);

        return _button;
    }

}

So please tell me what is the problem and how to solve it , sorry but iam new to java , new to programming games world ! ,, thank you 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:23:01+00:00Added an answer on May 27, 2026 at 5:23 am

    You have to add the JButton inside the main thread, cross thread Component manipulation is bad.

    For example:

    public Intro() {
        JButton exitButton = new JButton("Exit");
        this.add(exitButton);
    } 
    

    Alternatively, use SwingUtilities.invokeLater(Runnable). For example, in your run method:

    @Override
    public void run() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                add(createbutton("Exit"));
            }
        }
    } 
    

    Based on discussion it seems that you are overriding the paint or paintComponent methods. You need to call super in them, like:

    void paint(Graphics g) {
        super.paint(g);
        // do other stuff to g
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm coding simple game which I plan to make multiplayer (over the network) as
For quite awhile I have been trying to make a simple game in Java
I'm attempting to make a simple game of Pairs for Android. Program Structure Menu.java
I'm attempting to make a simple game of Pairs for Android. Program Structure: Menu.java
I'm trying to make a simple java game -- driving a car through a
I'm currently using AMFPHP 1.9 to make a simple flash game which uses a
I'm starting to code my first game and I want to make simple 2D
I would like to make a simple game in Java that has already been
I'd like to make a simple game where discs fall from the top of
Im going to make simple game with fallowing architecture: Initialization OpenWindowAndGraphics while(game_not_end) { ReadEvents

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.