java - JFrame (swing) 用按钮切换重复 Action

标签 java swing jframe

我想要一个带有 2 个按钮(最终更多)的 JFrame 应用程序,我可以用它在多个重复操作之间切换,为了简单起见,我现在只使用控制台打印,尽管稍后它可能会调用一个方法。这是 JFrame 的框架:

public class DayNight extends JFrame implements ActionListener{

    //JFrame entities
    private JPanel animationPanel;
    private JButton button;
    private JButton button2;


    public static void main(String[] args) {
        DayNight frame = new DayNight();
        frame.setSize(2000, 1300);
        frame.setLocation(1000,350);
        frame.createGUI();
        frame.setVisible(true);
        frame.setTitle("Day/Night Cycle, Rogier");
    }

   private void createGUI() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container window = getContentPane();
    window.setLayout(new FlowLayout() );
    animationPanel = new JPanel();
    animationPanel.setPreferredSize(new Dimension(2000, 900));
    animationPanel.setBackground(Color.black);
    window.add(animationPanel);

    button = new JButton("choice1");
    button.setFont(new Font("Arial", Font.PLAIN, 50));
    window.add(button);
    button.setActionCommand("choice1");
    button.addActionListener(this);

    button2 = new JButton("choice2");
    button2.setFont(new Font("Arial", Font.PLAIN, 50));
    window.add(button2);
    button2.setActionCommand("choice2");
    button2.addActionListener(this);
   }
}

我尝试过以下方法:

public void actionPerformed(ActionEvent event) {
    String command = event.getActionCommand();
    while ("Stop"!=(command)){
        command = event.getActionCommand();
        try{
            Thread.sleep(500);
            if ("choice1".equals(command)){
                System.out.println("choice1");
            }
            else if("choice2".equals(command)){
                System.out.println("choice2");
            }
            else{
                System.out.println("no choice");
            }
        }   
        catch(InterruptedException ex){
            Thread.currentThread().interrupt();
        }

    }
}

但是在我单击按钮后,它一直卡在该打印上,我什至无法再与按钮交互。我是否遗漏了什么或者我需要一个完全不同的结构?我已经检查了很多不同的程序,但它们太复杂了,我无法理解,阅读 swing 中的并发也没有让我弄清楚。

编辑:还没有“停止”命令,因为我现在不需要它。

最佳答案

您的代码有很多错误

它以字符串比较错误开始(参见here)。

但您的实际问题:您正在 sleep 事件调度程序线程(请参阅there)

因此,您使用事件监听器 sleep 的想法根本就是错误的方法。你不能这样做。你必须重新考虑你的方法。

这里真正的答案是:您缺乏基本 Java 知识。如果 Swing UI 编码,请考虑首先学习这些基本知识,然后再进一步增加自己的负担。

您的要求似乎是:在按下按钮之后 - 并且经过一定时间后,您想要在控制台上打印一些内容。你可以这样做:

public void actionPerformed(ActionEvent event) {
   String command = event.getActionCommand();
   command = event.getActionCommand();
   Runnable printChoice = new Runnable() {
     @Override
     public void run() {
      try{
        Thread.sleep(500);
        if ("choice1".equals(command)){
            System.out.println("choice1");
        }
        else if("choice2".equals(command)){
            System.out.println("choice2");
        }
        else{
            System.out.println("no choice");
        }
      }   
      catch(InterruptedException ex){
        Thread.currentThread().interrupt();
      }
    });
    new Thread(printChoice).start();
}

以上:

  • 创建一个 Runnable,它只是等待然后打印一些内容
  • 使用新线程(不是事件调度程序线程)来执行此操作

我没有通过编译器运行它 - 它是作为“伪代码”来为您提供一些如何解决问题的想法。

但进一步思考这一点 - 我认为这是朝着错误的方向发展的。当您开发合理的 UI 应用程序时,您不希望有任何东西在等待。

换句话说:你想到的是:

  • 单击按钮 A - 某些代码开始“循环”
  • 当用户做出另一个选择时,“循环”代码会注意到这一点并执行某些操作

错误的方法。相反,请按照以下思路工作:

  • 用户单击按钮,也许会启用其他 UI 组件
  • 用户使用其他 UI 组件执行某些操作 - 触发其他操作

示例:更改单选按钮会导致触发事件。您不应该有一个定期检查这些按钮的循环。相反,您定义用户必须遵循的清晰的操作/事件流程才能触发特定操作。

关于java - JFrame (swing) 用按钮切换重复 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46200570/

相关文章:

java - Java 中枚举的缺点是什么?

java - NetBeans:如何将 ScrollBar 添加到 JPanel

java - JFrame 重绘 () 问题 - Java

java - 如何使用java在jframe中退出前调用方法

java - 如何在 Android 上以编程方式备份​​应用程序

java - Bean验证: Complex message value resolution - but how?

java - 在 Android 中单击按钮时出现 NullPointerException

java - 计算 Swing 中物体之间可能的路径(可能是无穷大)的策略?

java - 将 JTextField 添加到 JToolBar

java - 点击JFrame的关闭按钮后如何删除MySql数据库记录?