-2

我已经设置了我的 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(可能用错误的词搜索)之外找不到任何东西。我不想这样做。只是尝试使用允许进入游戏本身的入口点的主菜单来做其他游戏所做的事情。

4

1 回答 1

0

将您的用户界面编码为一系列模块/组件,每个模块/组件都锚定在 JPanel 上。然后根据需要换出这些模块,以显示在任何特定时刻需要什么用户界面。

于 2013-06-24T05:07:48.127 回答