在Swing中如果有长时间运行的事件,当该事件运行时,其余的GUI是没有响应的。例如一个JButton按钮,这个按钮有一个事件,事件的处理时间比较长,那么在事件没有处理完之前,按钮是处于按下状态的,别的控件也没有反应。这个问题可以用多线程来解决:
private void jButtonTActionPerformed(ActionEvent evt) { //点击按钮触发的函数
runSigtest();
}
private void runSigtest(){ //开辟线程处理
new Thread(){
public void run(){
if (hDev == null) {
Object[] options = { "OK" };
JOptionPane.showOptionDialog(null, "Device is not open!",
"Message", JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
} else {
jButtonT.setText("Waiting..."); //按钮上显示"Waiting..."
jTextArea1.setText("Waiting...");
String Sig = null;
Sig = jComboBoxS.getSelectedItem().toString();
int SigmentNum = Integer.valueOf(Sig);
boolean SigT = CLibrary.Instance.CAPI_SigTest(hDev, SigmentNum);
if (SigT) {
jTextArea1.setText("Memory Sigment " + SigmentNum
+ " Testing Successful!");
} else {
jTextArea1.setText("Testing failed!");
}
jButtonT.setText("Test"); //事件处理完,按钮恢复"Test"
}
}
}.start();
}