简体   繁体   English

单击 Java 小程序后,JButton“保持按下状态”

[英]JButton “stay pressed” after click in Java Applet

I have a JButton in my Java Applet.我的 Java 小程序中有一个 JButton。 After pressing it, ActionListener have to make huge amount of actions.按下它后,ActionListener 必须做出大量的动作。 So, because of it, when user clicks the button, it "stay pressed" for a while (sometimes even 5 minuts) instead of disable itself immidiately (it disable itself after these 5 minuts).因此,因此,当用户单击按钮时,它会“保持按下”一段时间(有时甚至 5 分钟),而不是立即禁用自身(在这 5 分钟后禁用自身)。

public void actionPerformed(ActionEvent e) {
  JButton.setEnabled(false);
  //...
}

I don't want user to see that.我不希望用户看到。 I would like all these action execute in the background.我希望所有这些动作都在后台执行。 What can I do to achive it?我能做些什么来实现它?

You should do such intensive tasks in another thread, not the dispatcher thread.您应该在另一个线程中执行此类密集任务,而不是调度程序线程。

Some useful reading:Worker Threads and SwingWorker一些有用的阅读:工作线程和 SwingWorker

The problem is that the GUI-Thread is busy and will not repaint the component until processing has finsihed问题是 GUI 线程很忙,在处理完成之前不会重新绘制组件

You could do the activities in a backgroud thread.您可以在背景线程中进行活动。

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

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