[英]How to make a start menu with another frame in Java?
So I'm making a game and It's working quite well, but I just need to have a start menu with 3 buttons. 因此,我正在制作一个游戏,并且运行良好,但是我只需要一个带有3个按钮的开始菜单。 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();
}
});
}
}
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. 您必须具有用于主要功能的设置frame
类,并且对于frame
必不可少,并且还应该具有用于游戏控制的另一个类和用于菜单的一个类。
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. 您应该在frame
获取游戏控制器的即时信息,并在菜单的控制器中执行相同的操作,然后您才可以拥有单独的frame
。
please use interfaces to have a clean code
. please use interfaces to have a clean code
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.