关闭当前类并启动另一个

时间:2013-06-24 04:22:09

标签: java swing class jframe

我已经设置了我的Jframe并开始为我的游戏原型编码。我可以直接启动游戏,但现在尝试为我的游戏构建一个主菜单。我用Jbuttons设置了我的主菜单,我的退出按钮可以正常工作。现在我正试图让我的开始按钮开始我的游戏。尝试编写代码以关闭mainmenu类并启动我设置的类以运行我的游戏。这是我的代码片段......

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 个答案:

答案 0 :(得分:0)

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