gpt4 book ai didi

java - 如何在 GUI 中出现另一个 JOptionPane 时关闭一个 JOptionPane

转载 作者:搜寻专家 更新时间:2023-10-31 08:17:59 29 4
gpt4 key购买 nike

正如你从我上面的主题中看到的,
我想知道我怎么能解雇一个 JOptionPane 因为另一个 JOptionPane 而变得无关紧要,因为用户出于某种原因没有通过自己点击确定按钮(例如)解雇第一个。

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

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")
// <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

}

最佳答案

我会推荐你​​

  • 用你的 JOptionPane 创建一个 JDialog(JOptionPane API 会告诉你怎么做),
  • 然后将 SwingWorker 用于您希望执行的任何后台任务,这些任务应该在 Swing 主线程 EDT 之外完成,
  • 然后在后台任务完成时调用的 SwingWorker 的 done() 方法中,处理 JDialog。
  • 然后将立即调用下一个 JOptionPane。

例如,以下代码使用 Thread.sleep(3000) 进行 3 秒 sleep 来模拟需要 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<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);
}

关于java - 如何在 GUI 中出现另一个 JOptionPane 时关闭一个 JOptionPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7787499/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com