为了在等待服务器端响应时显示一个jDialog提示请等待,我参考一些代码写了下面的程序,但是不知道为什么当第二次点击button的时候,这时仅执行一行代码"jDialog.setVisible(true);",提示框居然也会自己跑出来,请帮忙找找原因,谢谢。用的Netbeans,一个用四个文件:
////////////////////////////////////////////
package javaapplication5;/**
 *
 * @author Sylvan
 */
public class Main {    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        new NewJFrame().setVisible(true);
    }}
////////////////////////////////////////////////////////
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *//*
 * NewJFrame.java
 *
 * Created on 2009-9-29, 13:54:19
 */package javaapplication5;/**
 *
 * @author Sylvan
 */
public class NewJFrame extends javax.swing.JFrame {    /** Creates new form NewJFrame */
    public NewJFrame() {
        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() {        jDialog = new javax.swing.JDialog();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        ClientHostname = new javax.swing.JTextField();
        ClientIP = new javax.swing.JTextField();
        ClientPassword = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jButton_backup = new javax.swing.JButton();        jDialog.setTitle("Add client");
        jDialog.setAlwaysOnTop(true);
        jDialog.setMinimumSize(new java.awt.Dimension(200, 150));
        jDialog.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jDialogComponentShown(evt);
            }
        });        jLabel4.setText("hostname:");        jLabel5.setText("ip:");        jLabel6.setText("password:");
        jLabel6.setToolTipText("please input amandabackup's password on this client");        ClientIP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClientIPActionPerformed(evt);
            }
        });        ClientPassword.setToolTipText("please input amandabackup's password on this client");        jButton1.setText("ok");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });        javax.swing.GroupLayout jDialogLayout = new javax.swing.GroupLayout(jDialog.getContentPane());
        jDialog.getContentPane().setLayout(jDialogLayout);
        jDialogLayout.setHorizontalGroup(
            jDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addGroup(jDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(ClientHostname)
                        .addComponent(ClientIP)
                        .addComponent(ClientPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)))
                .addContainerGap(37, Short.MAX_VALUE))
        );
        jDialogLayout.setVerticalGroup(
            jDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(ClientHostname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(ClientIP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(ClientPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(35, Short.MAX_VALUE))
        );        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        jButton_backup.setText("button");
        jButton_backup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_backupActionPerformed(evt);
            }
        });        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(84, 84, 84)
                .addComponent(jButton_backup)
                .addContainerGap(104, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addComponent(jButton_backup)
                .addContainerGap(102, Short.MAX_VALUE))
        );        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 289, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 16, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 16, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 211, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 8, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 7, Short.MAX_VALUE)))
        );        pack();
    }// </editor-fold>

解决方案 »

  1.   


        private void jButton_backupActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
            jDialog.setVisible(true);
            
    }    private void jDialogComponentShown(java.awt.event.ComponentEvent evt) {
            // TODO add your handling code here:
            System.out.println("jDialog ComponentShown");
    }    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
            try {            Thread_doCommand newThread = new Thread_doCommand(jDialog);//新生成一个处理事务线程
                newThread.start();//启动事务线程
                Thread_displayMessage displayMessageDialog = new Thread_displayMessage(jDialog, newThread,
                        "Executing command,please wait...");
                displayMessageDialog.start();//启动等待提示框线程
            } catch (Exception e) {
                e.printStackTrace();
            }
        }    private void ClientIPActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
        }       // Variables declaration - do not modify
        private javax.swing.JTextField ClientHostname;
        private javax.swing.JTextField ClientIP;
        private javax.swing.JPasswordField ClientPassword;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton_backup;
        private javax.swing.JDialog jDialog;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JLabel jLabel6;
        private javax.swing.JPanel jPanel1;
        // End of variables declaration}
    ////////////////////////////////////////////////////////
    package javaapplication5;/*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    /**
     *
     * @author Sylvan
     */
    import java.awt.*;
    import javax.swing.*;public class Thread_displayMessage extends Thread {    private Thread currentThread = null;//实际调用时就是TestThread事务处理线程 
        private String messages = "";//提示框的提示信息
        private JDialog parentDialog = null;//提示框的父窗体
        public static JDialog msgDialog = null;// "线程正在运行"提示框
        private Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize();
        private int width = dimensions.width / 4, height = 110;
        private int left = 0, top = 0;    public Thread_displayMessage(JDialog parentDialog, Thread currentThread, String messages) {
            this.parentDialog = parentDialog;
            this.currentThread = currentThread;
            this.messages = messages;
            initDiag();//初始化提示框
            System.out.println("初始化提示框");    }    private void msgDialogComponentShown(java.awt.event.ComponentEvent evt) {
            // TODO add your handling code here:
            System.out.println("msgDialogComponentShown");
        }    protected void initDiag() {        msgDialog = new JDialog(parentDialog, "Message", true);
            msgDialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
            msgDialog.setCursor(new Cursor(Cursor.WAIT_CURSOR));
            msgDialog.setAlwaysOnTop(true);
            msgDialog.addComponentListener(new java.awt.event.ComponentAdapter() {            public void componentShown(java.awt.event.ComponentEvent evt) {
                    msgDialogComponentShown(evt);
                }
            });        JPanel jp = new JPanel();
            JLabel msgLabel = new JLabel(messages);
            msgDialog.getContentPane().add(jp);
            jp.add(msgLabel);
            (new DisposeDiag()).start();//启动关闭提示框线程
        }    public void run() {
            //显示提示框
            int left = (dimensions.width - width) / 2;
            int top = (dimensions.height - height) / 2;
            msgDialog.setSize(new Dimension(width, height));
            msgDialog.setLocation(left, top);
            msgDialog.setVisible(true);
            msgDialog.setResizable(false);
            System.out.println(this.toString() + "!!!!!!!!!!!!!!!!!!!");
            return;
        }    class DisposeDiag extends Thread {        public void run() {            try {
                    currentThread.join();//等待事务处理线程结束
                    System.out.println(msgDialog.toString() + " stoped!  " );
                } catch (InterruptedException e) {
                    System.out.println("Exception:" + e);
                }            return;
            }
        }
    }
    ////////////////////////////////////////////////////////
    package javaapplication5;import java.awt.Component;/*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    /**
     *
     * @author Sylvan
     */
    public class Thread_doCommand extends Thread {    private Component component;    public Thread_doCommand(Component component) {
            this.component = component;    }    @Override
        public void run() {
            try {
                Thread.sleep(2000);
                component.setVisible(false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    ////////////////////////////////////////////////////////
      

  2.   

    jDialog.setVisible(true);不就是显示对话框么?
      

  3.   

    对的,但我不希望这个对话框一出现,另外一个告知“请等待”的jDialog也同时出现
      

  4.   

    就是msgDialog = new JDialog(parentDialog, "Message", true) 这个Dialog会同时出现
      

  5.   

    问题已解决
    1、class DisposeDiag extends Thread 中应该要加上这句:msgDialog.dispose();//关闭提示框,也就是事务线程结束后关闭msgDialog;
    但这样仍然是有一样的问题。2、真正的原因在于创建msgDialog时应选择parentWindow为NewJFrame,而不是JDialog!不过还是有点奇怪为什么选JDialog时有问题。
    Thread_displayMessage displayMessageDialog = new Thread_displayMessage(NewJFrame.this, newThread,
                        "Executing command,please wait...");
     
     msgDialog = new JDialog(parentWindow, "Message");
     msgDialog.setModal(true);