自定义`JOptionPane.YES_NO_OPTION`

时间:2015-05-23 16:31:32

标签: java swing joptionpane custom-component

开发一个swing应用程序,如果他点击No button上的JoptionPane.showConfirmDialog,我需要将用户重定向到另一个JFrame。我写了这段代码:

private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
    int a=JOptionPane.showConfirmDialog(this, "Did you complete your task?");
    JOptionPane.showMessageDialog(null, a);
    if(a==1){
        RedirectedForm rf=new RedirectedForm();
        rf.setVisible(true);
    }
}

if(a==1){...}部分似乎不起作用。即使我创建了一个对我要重定向到的JFrame - RedirectedForm的引用,窗口也会始终处理掉。任何人都可以解释原因并提出解决方案吗?
感谢任何帮助,谢谢!

提供以下两个java类:

WindowClosing.java

import javax.swing.JOptionPane;

public class WindowClosing extends javax.swing.JFrame {

public WindowClosing() {
    initComponents();
}
private void initComponents() {
    window_close_label = new javax.swing.JLabel();
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent evt) {
            formWindowClosing(evt);
        }
    });

    window_close_label.setText("Window Close Form");
    pack();
}                        

private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
    int a=JOptionPane.showConfirmDialog(this, "Did you complete your task?");
    JOptionPane.showMessageDialog(null, a);
    if(a==1){
        RedirectedForm rf=new RedirectedForm();
        rf.setVisible(true);
    }
}                                  
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new WindowClose().setVisible(true);
        }
    });
}
private javax.swing.JLabel window_close_label;
}

RedirectedForm.java

public class RedirectedForm extends javax.swing.JFrame {
public RedirectedForm() {
    initComponents();
}

private void initComponents() {

    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("You have been redirected to this Form becuse you have closed the previous one");

    pack();
}

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new RedirectForm().setVisible(true);
        }
    });
}
private javax.swing.JLabel jLabel1;                  

}

我再次编辑了代码,希望这会更清楚地了解问题

1 个答案:

答案 0 :(得分:2)

您的主要问题可能是您设置了

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

这会导致整个应用程序在关闭框架时退出,无论您在对话框中选择了哪个选项。不过,您shouldn't use multiple JFrames,而是use CardLayout,如此处所示:

public class Example extends JFrame {

    private String redirectedForm = "RedirectedForm";
    private String windowClosing = "WindowClosing";

    Example() {

        CardLayout cl = new CardLayout();
        getContentPane().setLayout(cl);
        add(new WindowClosing(), windowClosing);
        add(new RedirectedForm(), redirectedForm);

        addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent evt) {

                int a = JOptionPane.showConfirmDialog(Example.this, "Did you complete your task?");
                if (a == JOptionPane.CANCEL_OPTION) {
                    return;
                }
                if (a == JOptionPane.NO_OPTION) {
//                  cl.next(frame.getContentPane()); // This to show the next card
                    cl.show(getContentPane(), redirectedForm); // This is to show a specified card
                }
                else {
                    dispose();
                }
            }
        });

        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE );
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String args[]) {

        new Example();
    }
}

class WindowClosing extends JPanel {

    public WindowClosing() {

        JLabel closeLabel = new JLabel("The WindowClosing panel");
        add(closeLabel);
    }
}

class RedirectedForm extends JPanel {

    public RedirectedForm() {

        JLabel label = new JLabel("You have been redirected to this Form becuse you have closed the previous one");
        add(label);
    }
}

通过设置DO_NOTHING_ON_CLOSEwindowClosing事件不会终止该程序。相反,您可以根据所选选项选择执行的操作:无,退出,或切换面板。

注意:

  • 请勿使用int等静态常量的JOptionPane.NO_OPTION值,而只需使用对它的引用。
  • 您可能希望为JOptionPane设置所有者。