如何在GUI中出现另一个JOptionPane时解除一个JOptionPane [英] How can I dismiss one JOptionPane upon emergence of another JOptionPane in the GUI

查看:95
本文介绍了如何在GUI中出现另一个JOptionPane时解除一个JOptionPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如你从我上面的主题中看到的那样,

我想知道如何解雇因为另一个JOptionPane而变得无关的JOptionPane并且因为用户由于某种原因没有解雇第一个单独按下确定按钮(例如)。



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



<$ p $ 。p> JOptionPane.getRootFrame()处理();

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

谢谢



编辑:

代码示例:

 包桂; 

import javax.swing.JOptionPane;
import java.awt。*;

/ **
*
* @author
* /
公共类JpanelMainView扩展javax.swing.JPanel {

/ **创建新表单JpanelMainView * /
public JpanelMainView(){
initComponents();
}

/ **此方法从构造函数中调用
*初始化表单。
*警告:请勿修改此代码。此方法的内容是
*,始终由表单编辑器重新生成。
* /
@SuppressWarnings(unchecked)
//< editor-fold defaultstate =collapseddesc =Generated Code>
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.setHorizo​​ntalGroup(
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))
);
} //< / editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
JOptionPane.showMessageDialog(这个,Board已发送验证检查\ n \\ n请等待.. 。,Board Sent,JOptionPane.INFORMATION_MESSAGE);

//请注意,这个OptionPane总是在第一个JoptionPane
之后进入我的真实程序//我希望如果我已经到达需要显示此代码的代码中的那一行第二个JoptionPane,我将首先关闭第一个JoptionPane
//否则你将关闭最后一个JoptionPane,然后是第一个,我不想给用户两个JoptionPane关闭,这很烦人。
JOptionPane.showMessageDialog(这个,现在请设置你的行动,游戏开始,JOptionPane.INFORMATION_MESSAGE);
}

//变量声明 - 不要修改
private javax.swing.JButton jButton1;
//变量结束声明

}


解决方案

我建议你




  • 用你的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已发送验证检查\ n请等待......,
JOptionPane.INFORMATION_MESSAGE);
final JDialog dialog = messagePane.createDialog(this,Board Sent);

新的SwingWorker< Void,Void>(){

@Override
protected Void doInBackground()抛出异常{
//进行后台处理这里
//例如在这里验证你的电路板

//模仿一个需要3秒的后台进程
//你当然会在你的实际程序中删除它
Thread.sleep(3000);

返回null;
}

//当上面的后台线程完成时调用。
protected void done(){
dialog.dispose();
};
} .execute();

dialog.setVisible(true);

JOptionPane.showMessageDialog(这是请立即行动,
游戏开始,JOptionPane.INFORMATION_MESSAGE);
}


As you have seen from my subject above,
I would like to know how can I dismiss a JOptionPane which became irrelevant because of another JOptionPane and because the user ,for some reason, didn't dismiss the first one by himself clicking ok button (for example).

I've seen some ware in other site similar question, and people suggested to do simply:

JOptionPane.getRootFrame().dispose();  

But how can I store a reference for each JOptionPane I have and to be able to dismiss only that wanted one.
Thanks

Edited:
Code example:

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")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    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))
        );
    }// </editor-fold>                        

    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                   

}

解决方案

I would recommend that you

  • create a JDialog with your JOptionPane (the JOptionPane API will show you how),
  • then use a SwingWorker for whatever background task you wish to do that should be done off of the main Swing thread, the EDT,
  • Then in the SwingWorker's done() method which is called when the background task is complete, dispose of the JDialog.
  • Then the next JOptionPane will be called immediately.

For example the following code uses a Thread.sleep(3000) for a 3 second sleep to mimic a background task that takes 3 seconds. It will then close the first dialog and show the second:

   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<Void, Void>() {

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

这篇关于如何在GUI中出现另一个JOptionPane时解除一个JOptionPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆