如何在GUI中出现另一个JOptionPane时解除一个JOptionPane

正如你从上面的主题中看到的,
我想知道如何解雇因为另一个JOptionPane而变得无关的JOptionPane,并且因为某些原因,用户没有通过单击ok按钮(例如)解除第一个。

我在其他网站上看到过一些类似问题的软件,人们建议简单地做:

JOptionPane.getRootFrame().dispose(); 

但是我如何为每个JOptionPane存储一个引用,并且只能解雇那个想要的引用。
谢谢

编辑:
代码示例:

 package Gui; import javax.swing.JOptionPane; import java.awt.*; /** * * @author */ public class JpanelMainView extends javax.swing.JPanel { /** Creates new form JpanelMainView */ public JpanelMainView() { initComponents(); } /** 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") //  private void initComponents() { jButton1 = new javax.swing.JButton(); jButton1.setText("jButton1"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(149, 149, 149) .addComponent(jButton1) .addContainerGap(178, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(77, 77, 77) .addComponent(jButton1) .addContainerGap(200, Short.MAX_VALUE)) ); }//  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { JOptionPane.showMessageDialog(this, "Board was sent for validation check\n Please wait...","Board Sent",JOptionPane.INFORMATION_MESSAGE); //Please note that this OptionPane is coming in my real program always after the first JoptionPane //I want that if I've reached the line in the code that need to show this second JoptionPane, I will first close the first JoptionPane //Else you will dismiss the last JoptionPane and then the first one and I don't want to give the user two JoptionPane to close, it's annoying. JOptionPane.showMessageDialog(this, "Set your move now please","Game started",JOptionPane.INFORMATION_MESSAGE); } // Variables declaration - do not modify private javax.swing.JButton jButton1; // End of variables declaration } 

我会建议你

  • 用你的JOptionPane创建一个JDialog( JOptionPane API将告诉你如何),
  • 然后使用SwingWorker完成你想要做的任何后台任务,这应该在主Swing线程EDT上完成,
  • 然后在后台任务完成时调用的SwingWorker的done()方法中,处理JDialog。
  • 然后立即调用下一个JOptionPane。

例如,以下代码使用Thread.sleep(3000)进行3秒睡眠以模拟需要3秒的后台任务。 然后它将关闭第一个对话框并显示第二个对话框:

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { JOptionPane messagePane = new JOptionPane( "Board was sent for validation check\n Please wait...", JOptionPane.INFORMATION_MESSAGE); final JDialog dialog = messagePane.createDialog(this, "Board Sent"); new SwingWorker() { @Override protected Void doInBackground() throws Exception { // do your background processing here // for instance validate your board here // mimics a background process that takes 3 seconds // you would of course delete this in your actual progam Thread.sleep(3000); return null; } // this is called when background thread above has completed. protected void done() { dialog.dispose(); }; }.execute(); dialog.setVisible(true); JOptionPane.showMessageDialog(this, "Set your move now please", "Game started", JOptionPane.INFORMATION_MESSAGE); } 

简单的答案,这是不可能的,因为在当前时间只能存在一个JOptionPane ,如果你想解决你的问题,那么你必须创建JDialog#ModalityType(Aplications???)JWindow然后你就能够显示JOptionPane

 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ClosingFrame extends JFrame { private JMenuBar MenuBar = new JMenuBar(); private JFrame frame = new JFrame(); private static final long serialVersionUID = 1L; private JMenu File = new JMenu("File"); private JMenuItem Exit = new JMenuItem("Exit"); private JFrame frame1 = new JFrame(); public ClosingFrame() { File.add(Exit); MenuBar.add(File); Exit.addActionListener(new ExitListener()); WindowListener exitListener = new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { int confirm = JOptionPane.showOptionDialog(frame, "Are You Sure to Close this Application?", "Exit Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null); if (confirm == 0) { System.exit(1); } } }; frame.addWindowListener(exitListener); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setJMenuBar(MenuBar); frame.setPreferredSize(new Dimension(400, 300)); frame.setLocation(100, 100); frame.pack(); frame.setVisible(true); frame1.addWindowListener(exitListener); frame1.setDefaultCloseOperation(EXIT_ON_CLOSE); frame1.setPreferredSize(new Dimension(400, 300)); frame1.setLocation(500, 100); frame1.pack(); frame1.setVisible(true); } private class ExitListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { int confirm = JOptionPane.showOptionDialog(frame, "Are You Sure to Close this Application?", "Exit Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null); JOptionPane.showMessageDialog(null, "Whatever", "Whatever", JOptionPane.ERROR_MESSAGE); int confirm1 = JOptionPane.showOptionDialog(frame1, "Are You Sure to Close this Application?", "Exit Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null); if (confirm == 0) { //System.exit(1); } } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ClosingFrame cf = new ClosingFrame(); } }); } } 

对不起,我不太了解你的问题,你的意思是你想从另一个关闭一个JOptionPane吗?

 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class GUIProgram extends JFrame implements ActionListener { private JButton btn1, btn2, btn3; private static final String BUTTON1_COMMAND = "Press it!"; private static final String BUTTON2_COMMAND = "show new JOptionPane!"; private static final String BUTTON3_COMMAND = "close old JOptionPane!"; private JOptionPane pane1, pane2; public GUIProgram() { super("The title"); setDefaultCloseOperation(EXIT_ON_CLOSE); btn1 = new JButton(BUTTON1_COMMAND); btn1.addActionListener(this); btn1.setActionCommand(BUTTON1_COMMAND); btn2 = new JButton(BUTTON2_COMMAND); btn2.addActionListener(this); btn2.setActionCommand(BUTTON2_COMMAND); btn3 = new JButton(BUTTON3_COMMAND); btn3.addActionListener(this); btn3.setActionCommand(BUTTON3_COMMAND); pane1 = new JOptionPane(); pane2 = new JOptionPane(); getContentPane().add(btn1); setSize(200, 100); setVisible(true); } public static void main(String args[]) { new GUIProgram(); } @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals(BUTTON1_COMMAND)) { JPanel panel = new JPanel(); panel.add(btn2); pane1.showOptionDialog(null, panel, "JOptionPane #1", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[]{}, null); } else if(e.getActionCommand().equals(BUTTON2_COMMAND)) { JPanel panel = new JPanel(); panel.add(btn3); pane2.showOptionDialog(null, panel, "JOptionPane #2", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[]{}, null); } else if(e.getActionCommand().equals(BUTTON3_COMMAND)) { pane1.getRootFrame().dispose(); } } }