【发布时间】:2021-05-06 11:54:32
【问题描述】:
我有一个主类、一个 UI 类和一个接口处理从 UI 到主的回调:
主要:
package clascomms;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author stef
*/
public class ClassComms implements Handler{
private final ClassCommsUi ui;
public ClassComms() {
// private final ClassCommsUi ui;
ui = new ClassCommsUi(this,"Click1","Click2");
}
@Override
public void btn1Clicked(){
try {
// this code should run if button 1 is clicked
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(ClassComms.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("bt1Clicked");
}
@Override
public void btn2Clicked(){
// this code should run if button 2 is clicked
System.out.println("bt2Clicked");
}
public static void main(String[] args) {
ClassComms thisOne = new ClassComms();
}
}
用户界面:
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();
}
如您所见,主类中有一个 5 秒的 Thread.sleep()... 当我单击该按钮时,它将保持按下状态,直到睡眠结束 - UI 保持 100% 无响应。 是否有一种简单(此处为 NOOB...)的方式来让 UI 保持响应?
【问题讨论】:
-
好像你错误地粘贴了两次
ClassComms,而不是ClassCommsUI。 -
GUI 中的冻结是由事件调度线程 (EDT) 上的
Thread.sleep()引起的。这是负责 GUI 更新的线程,而您正在阻止它。对于此类任务,请使用javax.swing.Timer或SwingWorker。 -
@maloomeister:谢谢,你是对的 - 我已经编辑过了。
标签: java swing user-interface event-handling