【发布时间】:2015-05-26 12:26:29
【问题描述】:
我在 java 中工作,我有一个 JFrame,它打开另一个 JFrame,用户单击一个按钮。单击按钮时,变量被设置为所选选项,然后 JFrame 隐藏。我正在使用 CountDownLatch 来阻止主 Jframe 运行,直到单击按钮并且框架不可见。
这里是我称呼另一个 Jfame 的地方:
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
try {
CountDownLatch signal = new CountDownLatch(1);
EditMenu em = new EditMenu(signal);
em.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
em.setVisible(true);
int result;
signal.await();
result = em.getOption();
System.out.println(result);
if (result == 1) {
System.out.println("Add state");
} else if (result == 2) {
System.out.println("Del state");
}
} catch (InterruptedException ex) {
Logger.getLogger(UIMain.class.getName()).log(Level.SEVERE, null, ex);
}
}
这是我的编辑菜单代码:
public class EditMenu extends javax.swing.JFrame{
/**
* Creates new form EditMenu
*/
int option = -1;
CountDownLatch cdl;
public EditMenu(CountDownLatch cdl) {
initComponents();
this.setTitle("Edit menu");
this.cdl = cdl;
}
public int getOption(){
return option;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
.....
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
option = 1;
this.setVisible(false);
cdl.countDown();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
option = 2;
this.setVisible(false);
cdl.countDown();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
// End of variables declaration
}
我的问题是尝试此操作时第二个窗口冻结,我不知道如何解决。
【问题讨论】:
-
使用modal dialog。
-
谢谢@kiheru 直到现在才知道模态对话框
标签: java multithreading swing jframe