【发布时间】:2013-06-24 04:22:09
【问题描述】:
我已经设置好我的 Jframe 并开始为我的游戏原型编写代码。我可以直接开始游戏,但现在尝试为我的游戏构建一个主菜单。我已经使用 Jbuttons 设置了我的主菜单,并且我的退出按钮可以根据需要工作。现在我正试图让我的开始按钮开始我的游戏。尝试编写代码以关闭主菜单类并启动我设置的类来运行我的游戏。这是我的代码片段...
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();
}
}
然后是我试图关闭的 MainMenu 类,以便我可以开始我的游戏类...
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 之外找不到任何东西(可能是用错误的词搜索)。我不想这样做。只是尝试使用允许进入游戏本身的入口点的主菜单来做其他游戏所做的事情。
【问题讨论】:
-
1) 请参阅 The Use of Multiple JFrames, Good/Bad Practice? 2) 不要在没有正当理由的情况下混合使用 Swing(例如
JPanel)和 AWT(例如Frame)组件。在这种情况下,请使用基于 Swing 的JFrame。 3) 请对代码、输入/输出和结构化文档(如 HTML 或 XML)使用代码格式。为此,请选择示例并单击消息发布/编辑表单上方的{}按钮。 4)不要设置顶级容器的大小。而是布局内容并致电pack()。 -
同上@Andrew 提出的所有伟大建议。 1) 另外,不要过度使用,尤其是不要不恰当地使用 static 修饰符,因为它会使你的程序难以扩展、调试、测试和维护。 2)尽量避免空布局和
setBounds(...)。学习使用布局管理器来帮助您创建令人愉悦、可自行调整大小的复杂而和谐的 GUI,这些 GUI 可以干净利落地调整大小并在任何平台上运行良好。 -
@HovercraftFullOfEels 是的,很好。 绝大多数新程序员使用
static关键字是 a) 不必要的。 b) 大量错误的来源。 -
@Andrew 前几天晚上阅读了 1 中的教程。似乎 multi Jframes 作为一个长时间的游戏是反直觉的,而且我永远不想搞砸。 #2 将对此进行一些搜索,并且应该对我有很大帮助。 #3 & 4 你是否有更多关于这方面的信息可以联系起来,因为不是 100% 确定你的意思。在#4 我认为你的意思是我的 Jframe 的大小。如果是这种情况,我希望有一个选项允许用户设置分辨率。很快就会通过选项菜单进行编码。我会给“java pack()”一个谷歌搜索,但如果你知道任何好的教程,我会看看
-
@Hover & Andrew 关于静态。认为这是我从不同的教程中养成的习惯。大多数都没有很好地解释它。在我什至不知道它做了什么之前,我花了几个月的时间学习 java/android。很多时候eclipse会给我关于“以非静态方式访问静态引用”的错误。使项目“静态”是推进代码的一种快速/肮脏的方式。我会尝试更多地了解这个问题并打破自己的习惯。