2015-05-26 47 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); 

}