2015-05-26 33 views
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     
} 

我的问题是,尝试这一点,我不当第二窗口冻结”不知道如何解决这个问题。

+4

使用[模态对话框(https://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html)。 – kiheru

+0

谢谢@kiheru直到现在还不知道模态对话框 – Eagle

回答

0

感谢kiheru :)这是我自己的解决方案:

int result = -1; 
JDialog d = new JDialog(this, true); 

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {           
    //Creating buttons 
    JButton b1 = new JButton("Add"); 
    b1.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      result = 1; 
      d.dispose(); 
     } 
    }); 
    JButton b2 = new JButton("Del"); 
    b2.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      result = 2; 
      d.dispose(); 
     } 
    }); 

    JPanel mainPanel = new JPanel(new GridLayout(5, 1)); 
    mainPanel.add(b1, 0); 
    mainPanel.add(b2, 1); 

    d.setTitle("Choose a option:"); 
    d.add(mainPanel); 
    d.setSize(500, 500); 
    d.setLocationRelativeTo(this); 
    d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
    d.setVisible(true); 
    System.out.println(result); 

}