Java 具有响应用户界面的Swing
我有一个主类、一个UI类和一个处理从UI到主UI的回调的接口: 主要内容: 用户界面: 如您所见,主类中有一个5秒的Thread.sleep()。。。 当我点击按钮时,它将一直被按下,直到睡眠结束——UI保持100%无响应。 是否有一种简单的方法让UI保持响应?您正在阻止EDT(事件调度线程)。所有与Swing相关的代码都在EDT上运行,其中包括您的Java 具有响应用户界面的Swing,java,swing,user-interface,event-handling,Java,Swing,User Interface,Event Handling,我有一个主类、一个UI类和一个处理从UI到主UI的回调的接口: 主要内容: 用户界面: 如您所见,主类中有一个5秒的Thread.sleep()。。。 当我点击按钮时,它将一直被按下,直到睡眠结束——UI保持100%无响应。 是否有一种简单的方法让UI保持响应?您正在阻止EDT(事件调度线程)。所有与Swing相关的代码都在EDT上运行,其中包括您的actionPerformed。尝试在背景线程上睡觉 @Override public void btn1Clicked(){ new Swi
actionPerformed
。尝试在背景线程上睡觉
@Override
public void btn1Clicked(){
new SwingWorker<Void, Void>() {
@Override
protected void doInBackground() throws Exception {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(ClassComms.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("bt1Clicked");
}
}().execute();
}
@覆盖
公共无效btn1Clicked(){
新SwingWorker(){
@凌驾
受保护的void doInBackground()引发异常{
试一试{
睡眠(5000);
}捕获(中断异常例外){
Logger.getLogger(ClassComms.class.getName()).log(Level.SEVERE,null,ex);
}
System.out.println(“bt1Clicked”);
}
}()执行();
}
似乎是您错误地粘贴了两次ClassComms
,而不是ClassCommsUI
。GUI中的冻结是由事件调度线程(EDT)上的线程.sleep()
引起的。这是负责GUI更新的线程,您正在阻止它。使用a或a来完成这类任务。@maloomeister:谢谢,你说得对-我已经编辑过了。好吧,在().execute()之前都可以正常工作;部分-我有一个错误,我无法通过。。。Netbeans告诉我创建一个名为execute的方法。我缺少一些导入?请发布错误SwingWorker
有一个方法名execute()
。既然我是在脑子里写的,我可能会打字,或者你没有正确地执行。
package clascomms;
/**
*
* @author stef
*/
public class ClassCommsUi extends javax.swing.JFrame {
private Handler mainClass;
public ClassCommsUi(Handler mainClass, String btn1, String btn2) {
this.mainClass = mainClass;
initComponents();
jButton1.setText(btn1);
jButton2.setText(btn2);
this.setVisible(true);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("jButton2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jButton1)
.addGap(27, 27, 27)
.addComponent(jButton2)
.addContainerGap(33, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(28, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// If this action happens, method btn1Clicked() in main Class should be called
mainClass.btn1Clicked();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// If this action happens, method btn2Clicked() in main Class should be called
mainClass.btn2Clicked();
}
public void quit(){}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
// End of variables declaration
}
package clascomms;
/**
*
* @author stef
*/
public interface Handler {
public void btn1Clicked();
public void btn2Clicked();
}
@Override
public void btn1Clicked(){
new SwingWorker<Void, Void>() {
@Override
protected void doInBackground() throws Exception {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(ClassComms.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("bt1Clicked");
}
}().execute();
}