0
我在java中工作,我有一个JFrame,它打开另一个JFrame,用户单击按钮。当按钮被点击时,一个变量被设置为选择的选项,并在此之后隐藏JFrame。我正在使用CountDownLatch来停止运行主Jframe,直到单击按钮并且框架不可见。隐藏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);
}
}
这里是我的editMenu代码:
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
}
我的问题是,尝试这一点,我不当第二窗口冻结”不知道如何解决这个问题。
使用[模态对话框(https://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html)。 – kiheru
谢谢@kiheru直到现在还不知道模态对话框 – Eagle