java - Action 监听器太快了,我进入下一个菜单

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

最佳答案

所以,您的代码放在这里,很好,您将一个 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();
   }
});

等等,您已经向 button1 添加了另一个 ActionListener,因此当您按下它时,它将执行 startGame 方法...

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

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

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

虽然可能比您习惯的更高级,但请查看 this examplethis example有关在 MVC 样式实现中使用 CardLayout 的示例

关于java - Action 监听器太快了,我进入下一个菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42146908/

相关文章:

java - Swing 图形用户界面;能够在运行长进程时调整窗口大小,但主框架内的面板在进程完成之前不会更新

java - 我应该如何以及在何处将 ActionListener 添加到我的代码中?

java - 将JAVA代码KeyStore.getInstance转为c#

java - Java 对象的多个 HashCode

java - 如何在 JAVA 中打印数组列表?

java - 如何验证输入的文本是否为数字?

java - JTree 单元渲染器问题

java - 等待按钮按下 JButton

Java - 使用二维数组创建按钮网格

java - 如何剪辑 Path2D?