【问题标题】:Getting result after a jframe is hidden隐藏jframe后获取结果
【发布时间】: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


【解决方案1】:

谢谢 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);

}         

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 2012-03-08
    • 1970-01-01
    相关资源
    最近更新 更多