jdialog框打开但似乎是空白

时间:2014-12-04 18:58:20

标签: java swing jdialog

我已经编写了以下代码来生成请等待JDialog生成决策树,但它会打开并显示为空白

public JDialog pleasewait()
{
    JDialog dialog = new JDialog();
    JLabel label = new JLabel("Please wait...");
    label.setIcon(new javax.swing.ImageIcon(getClass().getResource("/decision_tree_runner/load.gif"))); // NOI18N
    dialog.setBackground(Color.BLACK);
    dialog.setLocationRelativeTo(null);
    dialog.setTitle("Please Wait...");
    dialog.add(label);
    dialog.pack();

    return dialog;

}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    JDialog dialog = pleasewait();
    dialog.repaint();
    dialog.setVisible(true);
    FypProject fyp_project = new FypProject();
    try {fyp_project.main_fypproject();} catch (SQLException ex) {}
    dialog.setVisible(false);

}                             

3 个答案:

答案 0 :(得分:1)

fyp_project.main_fypproject()很可能是一个长时间运行/阻塞调用,当从事件调度线程的上下文中调用时,它将阻止它处理新事件,包括重绘请求。 / p>

考虑使用类似SwingWorker的内容,首先打开对话框,执行工作程序以及调用它的done方法时,关闭对话框

请查看Concurrency in SwingWorker Threads and SwingWorker了解详情

答案 1 :(得分:0)

我认为这与JLabel添加对话框的位置有关。缺乏我们的布局经理使这很困难。

在添加JLabel之前尝试添加它:

dialog.setLayout(new GridLayout());

并删除:

dialog.pack();

答案 2 :(得分:0)

我做了以下步骤

  1. 为pleasewait创建了单独的表单
  2. 为fyp_project.main_fypproject()
  3. 创建了线程
  4. 将表单对象传递给线程类
  5. 在run way setvisible()选项中请等待为false

    public class thread_for_pleasewait implements Runnable{
    Thread t ;
    please_wait_form pwf;   
    decision_tree dt;
    FypProject fyp_project = new FypProject();
     @Override
    
     public void run()
    {
    String[] args = null;
     try {
        fyp_project.main_fypproject();
        pwf.setVisible(false);
        dt.setVisible(true);
     } catch (SQLException ex) {
        Logger.getLogger(thread_for_pleasewait.class.getName()).log(Level.SEVERE, null, ex);
     }
    }
      public void start(please_wait_form pwf,decision_tree dt)
      {
       t = new Thread(this);
       t.start();
       this.pwf=pwf;
       this.dt=dt;
      }  
    }