Action Listener 是快速的方式,我进入下一个菜单

Action Listener is way to fast and I enter the next menu

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

有什么办法可以让我在菜单上说出来而不是立即进入游戏吗?

引擎:

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");
    }
}

所以,你的代码在这里,很好,你添加一个 ActionListenerbutton1button2

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();
   }
});

等等,你在 button1 中添加了另一个 ActionListener,所以当你按下它时,它会同时执行 startGame方法 ...

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

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

我鼓励您也学习 MVC 并尽可能地使用它。

虽然可能比您习惯的更高级一些,但请查看 this example and this example 以了解在 MVC 样式实现中使用 CardLayout 的示例