JFrame(摆动)通过按钮切换重复动作 [英] JFrame (swing) switching repeating actions with buttons

查看:64
本文介绍了JFrame(摆动)通过按钮切换重复动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want a JFrame application with 2 buttons (eventually more) that I can use to switch between multiple repeating actions, ofr simplicity I'm just using a console print for now, though it will probably be calling a method instead later. Here is the framework for the 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的并发性也并没有为我清除它.

But after I click a button it keeps stuck on that print and I can't even interact with the buttons anymore. Am I missing something or do I need a completely different structure? I've examined a lot of different programs but they are too complicated for me to understand, reading the concurrency in swing also didn't clear it up for me.

尚无停止"命令,因为我暂时不需要它.

There is no "stop" command yet because I don't need it for now.

推荐答案

您的代码有很多错误的地方.

There are many things wrong with your code.

它以字符串的错误比较开始(请参见这里).

It starts with wrong comparison of strings ( see here ).

但是您的实际问题是:您正在睡觉在事件调度程序线程中(请参阅

But your actual problem: you are sleeping the event dispatcher thread ( see there )

因此,您想通过事件监听器睡觉的想法完全是错误的方法.您无法以这种方式这样做.您必须在那里重新考虑您的方法.

So your idea of sleeping with an event listener is simply the wrong approach. You can't do it this way. You have to re-think your approach there.

这里的 real 答案是:您缺乏Java的基本知识.考虑先学习这样的基础知识,然后再进行Swing UI编码,使自己负担沉重.

And the real answer here is: you are lacking basic knowledge of Java. Consider learning such basic things first before further overburdening yourself if Swing UI coding.

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

Your requirement seems to be: after that button was hit - and a certain amount of time passed you want to print something on the console. You can do that like:

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,只需等待然后打印一些内容
  • 使用新线程(不是事件调度程序线程)执行
  • creates a Runnable that simply waits to then print something
  • uses a new Thread (not the event dispatcher thread) to do that

我没有通过编译器运行它-它的意思是伪代码",为您提供一些解决问题的方法.

I didn't run this through the compiler - it is meant as "pseudo code" to give you some ideas how to solve your problem.

但是要进一步考虑-我认为这是朝着错误的方向进行的.当您开发合理的UI应用程序时,您不希望任何事情 正在等待.

But further thinking about this - I think this is going in the wrong direction. You don't want that anything is waiting when you develop a reasonable UI application.

换句话说:您想到了:

  • 单击按钮A-一些代码开始循环"
  • 当用户做出其他选择时,循环"代码会注意到并执行某些操作

方法错误.而是按照以下方式工作:

Wrong approach. Instead work along these lines:

  • 用户单击一个按钮,可能会启用其他 UI组件
  • 用户使用其他UI组件执行某些操作-触发 other 操作
  • user clicks a button, and maybe that enables other UI components
  • user does something with those other UI components - triggering other actions

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

Example: changing radiobuttons causes events to be fired. You should not have a loop that regularly keeps checking those buttons. Instead you define a clear flow of actions/events the user has to follow in order to trigger a certain action.

这篇关于JFrame(摆动)通过按钮切换重复动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆