Reputation: 33
So I'm making a game and It's working quite well, but I just need to have a start menu with 3 buttons. A Play, Instructions and Exit button. The problem is that I want it in a different frame than the game itself, if you press on Start the frame should switch to the game frame. Can someone help me with this? This is a part of my code:
package frametest;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.*;
public class FrameTest extends JFrame implements ActionListener {
public JPanel createContentPane() {
//Jpanels and stuff
}
public JPanel createMainMenu() {
//Start menu JPanels and stuff
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Poker Game");
FrameTest demo = new FrameTest();
frame.setContentPane(demo.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1080,640);
frame.setVisible(false);
JFrame menu = new JFrame("Poker Game");
menu.setContentPane(demo.createMainMenu());
menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menu.setSize(1080,640);
menu.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}
Upvotes: 1
Views: 2307
Reputation: 353
You must have a setting frame
class for your main function and essential for frame
and you should have another class for game controlling and a class for menu.
You should get instant of game controller in frame
and do the same in controller for menu then you can have separate frame
that you want.
please use interfaces to have a clean code
.
Upvotes: 2