使用java swing显示两个对话框

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

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

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

编辑:

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

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

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

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

您可以按如下方式实现此目的:

在此处输入图像描述

 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的,可以包含你想要的任何摆动组件。

从构造函数创建第二个对话框,该对话框将对话框和布尔值作为参数并解决问题。