动作监听器是快速的方式,我进入下一个菜单

时间:2017-02-09 21:09:09

标签: java swing actionlistener

这很难解释但是当我在主菜单上时,我按下按钮1进入游戏,按钮2进入等级,按钮3退出。现在,如果我按下按钮2进入排名,然后按下按钮1返回菜单我会直接进入游戏而不会停留在菜单上。

无论如何我可以在菜单上编写代码而不是立即进入游戏吗?

发动机:

import javax.swing.*;

import com.wicke.main.game.Level1;

@SuppressWarnings("serial")
public class Engine extends JFrame {

    // Adding Classes
    Level1 lvl1 = new Level1(this);

    // System Variables
    public JButton button1;
    public JButton button2;
    public JButton button3;
    public JTextArea textArea1;

    public static void main(String[] args){

        new Engine();

    }

    public Engine(){
        // GUI Variables
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(900,600);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setTitle("Dungeon Drivers Alpha 0.2.3");

        JPanel thePanel = new JPanel();

        textArea1 = new JTextArea(33, 80);
        textArea1.setText("Window launch was Successful.\n");
        textArea1.setEditable(false);
        textArea1.setLineWrap(true);
        textArea1.setWrapStyleWord(true);
        thePanel.add(textArea1);

        JScrollPane scrollbar1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        thePanel.add(scrollbar1);

        button1 = new JButton();
        button1.setText("1");
        thePanel.add(button1);

        button2 = new JButton();
        button2.setText("2");
        thePanel.add(button2);

        button3 = new JButton();
        button3.setText("3");
        thePanel.add(button3);

        this.add(thePanel);
        this.setVisible(true);

        lvl1.Game();
    }

}

然后是Level1:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.wicke.main.Engine;
import com.wicke.main.data.Ranks;

public class Level1 {
    private Engine engine;

    public Level1(Engine engine) {
        this.engine = engine;
    }

    public void Game() {
        engine.textArea1.append("\tWelcome to Dungeon Drivers!\n");
        engine.textArea1.append("\tPress 1 to Start\n");
        engine.textArea1.append("\tPress 2 to Enter Local Leaderboard\n");
        engine.textArea1.append("\tPress 3 to Exit\n");
        engine.button1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){
                   start();
               }
            });
        engine.button2.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){
                   Leaderboard();
               }
            });
        engine.button3.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){
                   System.exit(0);
               }
            });
    }

    private void Leaderboard() {
        engine.textArea1.setText("");
        engine.textArea1.append("\t1. " + Ranks.rank1 + "\n");
        engine.textArea1.append("\t2. " + Ranks.rank2 + "\n");
        engine.textArea1.append("\t3. " + Ranks.rank3 + "\n");
        engine.textArea1.append("\t4. " + Ranks.rank4 + "\n");
        engine.textArea1.append("\t5. " + Ranks.rank5 + "\n");
        engine.textArea1.append("\tPress 1 to return.");
        engine.button1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){
                   engine.textArea1.setText("");
                   Game();
               }
            });
    }

    private void start() {
        engine.textArea1.setText("");
        engine.textArea1.append("Test");
    }
}

1 个答案:

答案 0 :(得分:2)

所以,你的代码就在这里,很好,你向ActionListener添加button1button2

engine.button1.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent e){
         start();
     }
});
engine.button2.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent e){
         Leaderboard();
     }
});

button2,您的代码就转到此处了......

engine.button1.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
       engine.textArea1.setText("");
       Game();
   }
});

等等,您已将ActionListener添加到button1,所以当您按下它时,它会同时执行startGame方法......

相反,为每个视图创建单独的组件,一个用于菜单,一个用于排行榜,一个用于游戏等,每个组件都有自己的内部管理和功能,与其他组件分开。

也许利用CardLayout来促进它们之间的导航

我鼓励您也了解MVC并尽可能地利用它。

虽然可能会更高级,但您可以使用this examplethis example来查看在MVC样式实现中使用CardLayout的示例