隐藏jframe后得到结果

时间:2015-05-26 12:26:29

标签: java multithreading swing jframe

我在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);
    }

}     

这是我的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                   
}

我的问题是第二个窗口在尝试时会冻结,我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分: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);

}