使用Java swing在彼此之上显示两个对话框

我遇到的情况是我显示一个对话框,用户必须填写一些菜单,然后按OK.它工作正常,但是现在我在此对话框上有另一个按钮,如果用户想要添加某些值,我希望弹出另一个对话框,在该对话框中用户填写附加值,然后按“确定”,此对话框消失,用户回到主菜单.对话.

我已经尝试过了,但是每次我调用新对话框时,焦点都不会偏离主对话框,我该怎么做这样的任务.

有没有相关的例子,或者做这种事情的正确方法是什么?

编辑:

public static class EdgeMenu extends JPopupMenu {
        // private JFrame frame; 
        public MyMenu(final JFrame frame) {
            super("My Menu");
            // this.frame = frame;

            this.addSeparator();
            this.add(new EdgePropItem(frame));           
        }  
    }  



 //this shows the first dialog, another class because i have some other  
 //functions to be performed here  

    public static class EdgePropItem extends JMenuItem{
            //...       

            public  EdgePropItem(final JFrame frame) {  
                super("Edit Properties");
                this.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        EdgePropertyDialog dialog = new EdgePropertyDialog(frame, edge);
                        dialog.setVisible(true);
                    }

                });
            }

        }  

现在在其他对话框中,在按钮甚至监听器中,我试图调用另一个对话框:

 private void newDialogHandler(java.awt.event.ActionEvent evt) {
        MyNewDialog rdialog = new MyNewDialog(edge);
        rdialog.setVisible(true);
    }  

它看起来不错,但是上一个对话框没有离开焦点,并且只有当我在该对话框上按完成/完成时,它才会消失,我想要的是新对话框成为焦点,而在此处按“确定”时,焦点应该回到旧的主对话框,但这不起作用吗?

解决方法:

也许这段代码可以说明您的问题,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SuperConstructor extends JFrame {

    private static final long serialVersionUID = 1L;

    public SuperConstructor() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(300, 300));
        setTitle("Super constructor");
        Container cp = getContentPane();
        JButton b = new JButton("Show dialog");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                FirstDialog firstDialog = new FirstDialog(SuperConstructor.this);
            }
        });
        cp.add(b, BorderLayout.SOUTH);
        JButton bClose = new JButton("Close");
        bClose.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                System.exit(0);
            }
        });
        add(bClose, BorderLayout.NORTH);
        pack();
        setVisible(true);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                SuperConstructor superConstructor = new SuperConstructor();
            }
        });
    }

    private class FirstDialog extends JDialog {

        private static final long serialVersionUID = 1L;

        FirstDialog(final Frame parent) {
            super(parent, "FirstDialog");
            setPreferredSize(new Dimension(200, 200));
            setLocationRelativeTo(parent);
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
            JButton bNext = new JButton("Show next dialog");
            bNext.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    SecondDialog secondDialog = new SecondDialog(parent, false);
                }
            });
            add(bNext, BorderLayout.NORTH);
            JButton bClose = new JButton("Close");
            bClose.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    setVisible(false);
                }
            });
            add(bClose, BorderLayout.SOUTH);
            pack();
            setVisible(true);
        }
    }
    private int i;

    private class SecondDialog extends JDialog {

        private static final long serialVersionUID = 1L;

        SecondDialog(final Frame parent, boolean modal) {
            //super(parent); // < --- Makes this dialog 
            //unfocusable as long as FirstDialog is visible
            setPreferredSize(new Dimension(200, 200));
            setLocation(300, 50);
            setModal(modal);
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setTitle("SecondDialog " + (i++));
            JButton bClose = new JButton("Close");
            bClose.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    setVisible(false);
                }
            });
            add(bClose, BorderLayout.SOUTH);
            pack();
            setVisible(true);
        }
    }
}
上一篇:java-除一个组件外,阻止整个swing ui-“对话框样式”


下一篇:使Java Swing模态对话框的行为类似于Mac OSX对话框