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

  • Home
  • SEARCH
  • 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 7909379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:23:15+00:00 2026-06-03T12:23:15+00:00

I’m a beginner trying to create a game. The game is empty, I didn’t

  • 0

I’m a beginner trying to create a game. The game is empty, I didn’t start to write it.

My problem: when I click in the start button I want to start the game in the same frame, but with this code when I’ll click on start the program opens another frame with the menu, and in the original frame will delete the panel.

How can I do this simple menu?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import panelimage.Image;

public class Project0 extends JFrame{
private static final long serialVersionUID = 1L;
private Game JPanelGame;
private Image MainMenu = new Image(new File("src/img/menu_background.jpg"));
JButton start = new JButton("START GAME"), exit = new JButton ("LEAVE GAME");

public Project0() {

    super();

    initFrame();
    initMenu();

    this.addKeyListener(new KeyAdapter() {

        public void keyPressed(KeyEvent evt) {
            JPanelGame.keyPressed(evt);
        }

        public void keyReleased(KeyEvent evt) {
            JPanelGame.keyReleased(evt);
        }            
    });

}


private void initFrame() {

    setResizable(false);
    setBounds(new Rectangle(400, 400, 600, 400));
    MainMenu.setLayout(new BorderLayout()); 
}


private void initMenu() {

    initButtons();

    MainMenu.setLayout(null);       
    MainMenu.add(start);
    MainMenu.add(exit);

    add(MainMenu);      
    setContentPane(MainMenu);

    setTitle("Project0");

}

private void initButtons() {

    start.addActionListener(new ActionListener() {  

        public void actionPerformed(ActionEvent e){

            JPanelGame = new Game();

            remove(MainMenu);

            add(JPanelGame, BorderLayout.CENTER);
            setContentPane(JPanelGame);

            invalidate(); validate();

            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    Project0 Game = new Project0();
                    Game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    Game.setVisible(true);
                }
            });
        }
    });      

    start.setLocation(225,100);
    start.setSize(150,50);

    exit.addActionListener(new ActionListener() {   

        public void actionPerformed(ActionEvent e){

            System.exit(0);
        }
    });

    exit.setLocation(225,200);
    exit.setSize(150,50);       

}


public static void main(String[] args) {    
    Project0 Game = new Project0();
    Game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Game.setVisible(true);

}

}

and this frame will start

import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;

import java.io.File;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

import highscores.*;


public class Game extends JPanel implements Runnable {

private static final long serialVersionUID = 1L;    

Thread game;
BufferedImage background;
HighscoreManager scores = new HighscoreManager();

public Game(){

    super(true);
    try{
        background = ImageIO.read(new File(""));
    }catch(Exception e) {}


    game=new Thread(this);
    game.start();
}

public void paintComponent(Graphics graphic2d){

    setOpaque(false);       
    super.paintComponent(graphic2d);      

    graphic2d.drawImage(background, 0, 0, null);
}   

public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {

    }

    if (key == KeyEvent.VK_RIGHT) {

    }

    if (key == KeyEvent.VK_UP) {

    }

    if (key == KeyEvent.VK_DOWN) {

    }

    if (key == KeyEvent.VK_A) {

    }

    if (key == KeyEvent.VK_D) {

    }

    if (key == KeyEvent.VK_W) {

    }

    if (key == KeyEvent.VK_S) {

    }
}

public void keyReleased(KeyEvent e) {

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {

    }

    if (key == KeyEvent.VK_RIGHT) {

    }

    if (key == KeyEvent.VK_UP) {

    }

    if (key == KeyEvent.VK_DOWN) {

    }

    if (key == KeyEvent.VK_A) {

    }

    if (key == KeyEvent.VK_D) {

    }

    if (key == KeyEvent.VK_W) {

    }

    if (key == KeyEvent.VK_S) {

    }
}


public void run(){

        try {Thread.sleep(50);}catch(InterruptedException ex){}     
}

}

  • 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-06-03T12:23:20+00:00Added an answer on June 3, 2026 at 12:23 pm

    I see two options, depending on your needs. Either you just remove all components from the frame and add the new contents. But by doing so, you loose your ‘menu-frame’. The other option (and IMO the best of the two) is to opt for a CardLayout, where one card is your menu and the other your game. The ‘start’ button would then simply switch from the ‘menu-card’ to the ‘game-card’ (and if needed start the game). In your game, you can have a button/key/… which does the inverse (switching from the game card to the menu card)

    I was going through your code but I gave up (certainly since @orzechowskid already pointed out what you need to adjust). A few tips in case you are going to post more code on this site:

    1. Read the SSCCE.org website and try to post code as described there
    2. Follow the Java naming conventions (e.g. classes start with an uppercase letter, variables with a lowercase letter)
    3. Do not give your own classes the same names as standard JDK classes. Makes it very hard for someone not familiar with your code (but probably rather familiar with the JDK classes) to read the code. E.g. it took me some time before I realized you have your own panelimage.Image class which has nothing to do with the java.awt.Image class

    And then a few Swing related tips based on what I saw of your code:

    1. Learn how to use LayoutManagers and get rid of the null layouts. The Visual guide to layout managers might be a good starting point
    2. Swing is designed to work with KeyBindings and not with KeyListeners. See the tutorial
    3. Be careful with the Thread.sleep usage and running multiple Threads in combination with a Swing UI. Make sure you are aware of the Swing threading rules
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.