使用java swing显示两个对话框

时间:2012-01-23 18:43:02

标签: java swing jdialog

我有一种情况,我显示一个对话框,用户必须填写一些菜单,然后按确定。它工作正常,但现在我在这个对话框上有另一个按钮,如果用户想要添加一些特定值,我想要弹出另一个对话框,用户填写附加值,按下确定,此对话框消失,用户返回主对话。

我试过这个,但是每当我调用新对话框时,焦点都不会离开主对话框,我该怎么做这样的任务。

是否有任何相关的例子或做这些事情的正确方法是什么。

编辑:

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

看起来很好,但是之前的对话框没有离开焦点,只有当我在该对话框上按完成/完成后它才会消失,我想要的是新对话框成为焦点,同时在这里按下确定,焦点应该回到旧的主对话框,但它不起作用?

3 个答案:

答案 0 :(得分:4)

也许这段代码可以证明你的问题,

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

答案 1 :(得分:2)

您可以按如下方式实现:

enter image description here

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class MultipleDialogs
{   
    public MultipleDialogs()
    {
        JButton btnOpen = new JButton("Open another dialog!");
        btnOpen.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                JOptionPane.showMessageDialog(null, "This is the second dialog!");
            }
        });
        Object[] options = {"OK", "Cancel", btnOpen};
        int selectedOption = JOptionPane.showOptionDialog(null,
                "This is the first dialog!", "The title",
                JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE,
                null, options, options[0]);

        if(selectedOption == 0) // Clicking "OK"
        {

        }
        else if(selectedOption == 1) // Clicking "Cancel"
        {

        }
    }

   public static void main(String[] args)
   {
      SwingUtilities.invokeLater(new Runnable()
      {
          @Override
          public void run()
          {
              new MultipleDialogs();    
          });
       }
    }
}

您可以将"This is the first dialog!""This is the second dialog!"替换为JPanel,其中包含您想要的任何挥杆组件。

答案 2 :(得分:0)

Create the second dialog from a constructor that takes dialog and boolean as parameters and that solves the problem.