I've setup my Jframe and started coding for my game prototype. I can start the game directly just fine but now trying to build a main menu for my game. I've setup my menu main with Jbuttons and my quit button works as I need. Now I'm trying to get my Start button to start my game. Trying to code to close out the mainmenu class and start the class I've setup to run my game. Here is my code snip...
public class Frame extends JFrame{
//snip
MainMenu mainMenu;
Screen screen;
public Frame(){
init();
}
public void init(){
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(null);
xValue = 800;
yValue = 600;
size = new Dimension (xValue, yValue);
setTitle(title);
setSize(size);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainMenu = new MainMenu(this);
add(mainMenu);
setVisible(true);
}
public void startGame(){
//Trying to close MainMenu class via below code but appears to do nothing yet system.out is being called
this.remove(mainMenu);
mainMenu.dispose();
System.out.println("I was clicked");
screen = new Screen(this); //this is my class to run the game
this.add(screen);
}
public static void main(String args[]){
Frame frame = new Frame();
}
}
Then my MainMenu class that I'm trying to close so I can then start my game class...
public class MainMenu extends JPanel implements ActionListener {
public static int myWidth, myHeight;
public static int stringWidth, stringHeight;
public static int buttonSpace;
public static Frame frame;
public Font smallFont,largeFont;
public static JButton startButton, continueButton, optionButton, exitButton;
public static boolean isFirst = true;
public static Point mse = new Point(0,0);
public MainMenu(Frame frame) {
frame.addKeyListener(new Listener());
frame.addMouseListener(new Listener());
frame.addMouseMotionListener(new Listener());
buildButton();
frame.add(startButton);
frame.add(continueButton);
frame.add(optionButton);
frame.add(exitButton);
}
public void define(Graphics g){
//frame = new Frame(); Trying to init frame to prevent a nullpointexception error but causes the problem of opening a second jframe
//snip code
}
public void paintComponent(Graphics g){
//snip code
}
public void buildButton(){
startButton = new JButton("Start");
startButton.setBounds(325, 200, 150, 50);
startButton.setRolloverEnabled(true);
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
frame.startGame(); //want to start game code from here
}
});
//snip code
exitButton = new JButton("Quit");
exitButton.setBounds(325, 200 + 180, 150, 50);
exitButton.setRolloverEnabled(true);
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
} //snip...
I've tried to google my issue but can't find anything other then opening up more Jframes (probably searching with the wrong words). I don't want to do this. Just trying to do what other games do with a mainmenu that allows a entry point into the game itself.