简体   繁体   English

在ActionEvent期间更改标签

[英]Changing a label during ActionEvent

I am trying to enable/disable a label when a button is pressed and i want to do this during the event and not after it. 我试图在按下按钮时启用/禁用标签,但我想在活动期间而不是之后进行。 As you can see below, i try to enable/disable the two labels: lblKeyboard and lblGamepad. 如您在下面看到的,我尝试启用/禁用两个标签:lblKeyboard和lblGamepad。

They end up running after the "RemoteControl.run();" 它们最终在“ RemoteControl.run();”之后运行。 is executed but i want it to happen before that. 被执行,但我希望它能在那之前发生。 Any way i can do that? 有什么办法可以做到吗?

Thank you! 谢谢!

JButton btnGamepad = new JButton("Gamepad");
        btnGamepad.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                    if(cont_state == 0){
                        if(RemoteControl.findGamePad() == true){
                            cont_state = 1;
                            game_status = "on";
                        }
                        else{
                            game_status = "off";
                            key_status = "on";
                            JOptionPane.showMessageDialog(null, "Controller not found!");
                            cont_state = 0;
                        }
                    }

                    if(cont_state == 1){    

                        System.out.println("CONNECTED GAMEPAD!");
                        lblKeyboard.disable();
                        lblGamepad.enable();
                        frame.repaint();
                        RemoteControl.run();

                        cont_state = 0;
                    }

            }
        });

Code in an event listener executes on the Event Dispatch Thread (EDT) and the GUI can't repaint itself until all the code has finished executing. 事件侦听器中的代码在事件调度线程(EDT)上执行,并且在所有代码执行完毕之前,GUI无法重绘自身。 Read the section from the Swing tutorial on Concurrency for more information on the EDT. 阅读Swing 并发教程中有关EDT的更多信息。

Try wrapping your RemoteControl.run() code in a SwingUtilities.invokeLater(...). 尝试将您的RemoteControl.run()代码包装在SwingUtilities.invokeLater(...). This will place the code at the end of the EDT, which might give Swing a changes to repaint the state of the two labels. 这会将代码放在EDT的末尾,这可能会使Swing进行更改以重绘两个标签的状态。

SwingUtilities.invokeLater(new Runnable()
{
    public void run()
    {
        RemoteControl.run()
    }
});

This assumes your code updates the GUI. 假设您的代码更新了GUI。 If not, then just use a separate Thread. 如果没有,则只需使用单独的线程。

ActionEvents are run on the EDT which is also responsible for painting. ActionEvents在还负责绘画的EDT上运行。 Once you change the labels state, Swing issues a request for repaiting the Label . 更改标签状态后,Swing会发出重新设置Label的请求。 The thing is that this request is posted on a queue and will be executed once the EDT is free and, as you can see, the EDT is busy running your code so no repainting for you! 事实是,此请求被发布到队列中,并且在EDT空闲时将被执行,并且如您所见,EDT正忙于运行您的代码,因此您无需重绘! Depending on the nature of your code, you should consider using a SwingWorker or simply moving RemoteControl.run() to another Thread as in 根据代码的性质,您应该考虑使用SwingWorker或将RemoteControl.run()移至另一个线程,如下所示:

new Thread(new Runnable() {
     @override
     public void run() {
          RemoteControl.run();
     }
}).start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM