Java 关闭当前类并启动另一个类
我已经设置了Jframe并开始为我的游戏原型编写代码。我可以直接开始游戏,但现在我正在尝试为我的游戏建立一个主菜单。我已经用Jbuttons设置了我的主菜单,我的退出按钮可以根据需要工作。现在我试着用我的开始按钮开始我的游戏。尝试编写代码关闭主菜单类并启动我设置的运行游戏的类。这是我的代码剪辑Java 关闭当前类并启动另一个类,java,swing,class,jframe,Java,Swing,Class,Jframe,我已经设置了Jframe并开始为我的游戏原型编写代码。我可以直接开始游戏,但现在我正在尝试为我的游戏建立一个主菜单。我已经用Jbuttons设置了我的主菜单,我的退出按钮可以根据需要工作。现在我试着用我的开始按钮开始我的游戏。尝试编写代码关闭主菜单类并启动我设置的运行游戏的类。这是我的代码剪辑 public class Frame extends JFrame{ //snip MainMenu mainMenu; Screen screen; public Frame(){
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();
}
}
然后我的主菜单类,我试图关闭,这样我就可以开始我的游戏类
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...
我试着用谷歌搜索我的问题,但除了打开更多的Jframes(可能是用错误的词搜索)外,找不到其他任何东西。我不想这样做。只是尝试使用允许进入游戏本身的主菜单来完成其他游戏的功能。将用户界面编码为一系列模块/组件,每个模块/组件锚定在JPanel上。然后根据需要调出这些模块,以显示在任何特定时刻所需的用户界面。1参见2无正当理由不要混合使用Swing(例如JPanel和AWT),例如机架组件。在这种情况下,使用基于Swing的JFrame。3请对代码、输入/输出和结构化文档(如HTML或XML)使用代码格式。为此,选择示例并单击消息发布/编辑表单上方的{}按钮。4不要设置顶级容器的大小。相反,要布局内容和呼叫包。与@Andrew提出的所有伟大建议一样。1另外,不要过度使用,尤其不要不适当地使用静态修改器,因为它会使程序难以扩展、调试、测试和维护。2避免空布局和立根。。。尽可能多。学习使用布局管理器来帮助您创建令人愉悦的、自调整大小的复杂但和谐的GUI,这些GUI可以干净地重新调整大小,并且在任何平台上都能很好地工作。@HovercraftFullOfEels是的,很好。绝大多数情况下,新程序员使用static关键字,这是不必要的。b许多错误的来源。@Andrew前几天晚上在1中阅读了教程。似乎多重Jframes作为一个长时间的游戏是违反直觉的,我永远不想搞乱它。2将对此进行一些搜索,应该会对我有很大帮助。3&4您是否有更多关于这方面的信息可以链接,因为无法100%确定您的意思。我想你指的是我Jframe的尺寸。如果是这种情况,我想有一个选项,允许用户设置分辨率。很快就会出现在列表中,通过选项菜单进行编码。我会给java pack一个谷歌搜索,但如果你知道任何好的教程,我会带一个look@Hover&关于静态。我想这是我从不同的教程中养成的习惯。大多数人解释得不太清楚。学习java/android几个月后,我甚至还不知道它做了什么。而且很多时候eclipse会在以非静态方式访问静态引用时出错。使项目保持静态是代码向前推进的一种快速/肮脏的方式。我将努力学习更多关于这个问题的知识,打破自己的习惯。