2010-11-21 18 views
0

在程序中的某一點,它打開一個JDialog,顯示有關程序運行過程中發生的事情的信息。它有幾個標籤和一個進度條,但是當對話窗口打開時,它不顯示任何內容。我的自定義JDialog出現,但爲空

這裏的自定義對話框,它的構造:

public class DataMiner implements ActionListener 
{ 
    private Hashtable<Integer, GISNode> nodeTable; 
    private Hashtable<Integer, GISLink> linkTable; 
    private int numLinesIgnored; 
    private int numLinesProcessed; 
    private int numNodes; 
    private int numLinks; 
    private int numDuplicates; 
    private int numFailedGeoCodingRequests; 
    private boolean cancelled; 

// window objects 
    private JDialog window; 
    private JLabel LinesIgnored; 
    private JLabel LinesProcessed; 
    private JLabel Nodes; 
    private JLabel Links; 
    private JLabel Duplicates; // tracks the number of equivalent data entries found. 
    private JLabel FailedGeoCodingRequests; 
    private JProgressBar progressBar; 
    private JButton cancelButton; 

    public DataMiner(JFrame parentWindow) 
    { 
     nodeTable = new Hashtable<Integer, GISNode>(1000); 
     linkTable = new Hashtable<Integer, GISLink>(1000); 

     numLinesIgnored = 0; 
     numLinesProcessed = 0; 
     numNodes = 0; 
     numLinks = 0; 
     numDuplicates = 0; 
     numFailedGeoCodingRequests = 0; 
     cancelled = false; 

     LinesIgnored = new JLabel(); 
     LinesProcessed = new JLabel(); 
     Nodes = new JLabel(); 
     Links = new JLabel(); 
     Duplicates = new JLabel(); 
     FailedGeoCodingRequests = new JLabel(); 
     cancelButton = new JButton("Cancel"); 
     progressBar = new JProgressBar(); 

     updateLabels(); // assigns a formatted string to each JLabel 
     cancelButton.addActionListener(this); 

    // initialize window 
     window = new JDialog(parentWindow); 
     window.setResizable(false); 
     window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     Container content = window.getContentPane(); 
     content.setLayout(new GridLayout(7,1)); 
     content.add(LinesProcessed); 
     content.add(Nodes); 
     content.add(Links); 
     content.add(Duplicates); 
     content.add(LinesIgnored); 
     content.add(FailedGeoCodingRequests); 
     content.add(progressBar); 
     JPanel p1 = new JPanel(); 
     p1.add(new JLabel("")); // takes up space 
     p1.add(cancelButton); 
     content.add(p1); 
     window.pack(); 
     window.setLocationRelativeTo(parentWindow); 
     window.setVisible(true); 
    } 

    (rest of the class...) 
} 

一旦這個窗口打開時,該程序的其餘部分保持執行正常的,只是這個窗口是空白。我錯過了什麼?

+0

是否顯示對話框?如果沒有,請先嚐試使用JDialog的setSize()。 – 2010-11-21 22:32:45

+0

@pouncep:它顯示出來,只是沒有任何內容。 – Max 2010-11-21 22:38:54

+0

嘗試使用['invokeLater'](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)從事件派發線程調用'window.setVisible(true)'。 – casablanca 2010-11-21 22:49:09

回答

1

可能是因爲你卡住EventDispatchThread,對GUI不能重繪自己。有關更多信息,請參閱Concurrency的Swing教程部分。

既然你沒有提供一個合適的SSCCE我們不能再做任何事情。

+0

謝謝。這看起來像我需要的。 – Max 2010-11-21 23:10:01

+0

調試時,在整個代碼中調用SwingUtilities.isEventDispatchThread()調用會很有幫助。 – 2010-12-22 19:49:37

0

這真的很奇怪。這段代碼對我來說很好,意思是顯示一個空的主窗口(這是正確的)和「彈出窗口」的子窗口窗口(你所說的不適合你)。我使用的NetBeans 6.8和Java 1.6在Ubuntu下:

package javaapplication2; 

import java.awt.Container; 
import java.awt.GridLayout; 
import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JProgressBar; 

public class MainFrame extends javax.swing.JFrame { 

    private int numLinesIgnored; 
    private int numLinesProcessed; 
    private int numNodes; 
    private int numLinks; 
    private int numDuplicates; 
    private int numFailedGeoCodingRequests; 
    private boolean cancelled; 

// window objects 
    private JDialog window; 
    private JLabel LinesIgnored; 
    private JLabel LinesProcessed; 
    private JLabel Nodes; 
    private JLabel Links; 
    private JLabel Duplicates; // tracks the number of equivalent data entries found. 
    private JLabel FailedGeoCodingRequests; 
    private JProgressBar progressBar; 
    private JButton cancelButton; 

    /** Creates new form MainFrame */ 
    public MainFrame() { 
     initComponents(); 

     JFrame parentWindow = this; 


     numLinesIgnored = 0; 
     numLinesProcessed = 0; 
     numNodes = 0; 
     numLinks = 0; 
     numDuplicates = 0; 
     numFailedGeoCodingRequests = 0; 
     cancelled = false; 

     LinesIgnored = new JLabel(); 
     LinesProcessed = new JLabel(); 
     Nodes = new JLabel(); 
     Links = new JLabel(); 
     Duplicates = new JLabel(); 
     FailedGeoCodingRequests = new JLabel(); 
     cancelButton = new JButton("Cancel"); 
     progressBar = new JProgressBar(); 

    // initialize window 
     window = new JDialog(parentWindow); 
     window.setResizable(false); 
     window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     Container content = window.getContentPane(); 
     content.setLayout(new GridLayout(7,1)); 
     content.add(LinesProcessed); 
     content.add(Nodes); 
     content.add(Links); 
     content.add(Duplicates); 
     content.add(LinesIgnored); 
     content.add(FailedGeoCodingRequests); 
     content.add(progressBar); 
     JPanel p1 = new JPanel(); 
     p1.add(new JLabel("")); // takes up space 
     p1.add(cancelButton); 
     content.add(p1); 
     window.pack(); 
     window.setLocationRelativeTo(parentWindow); 
     window.setVisible(true); 
    } 

    /** 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() { 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 400, Short.MAX_VALUE) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 300, Short.MAX_VALUE) 
     ); 

     pack(); 
    }// </editor-fold> 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new MainFrame().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify 
    // End of variables declaration 

} 
+0

在預感上,我評論了對話框顯示後運行的程序的其餘部分,並且它正常工作。爲什麼會發生這種情況? – Max 2010-11-21 22:49:21

+0

我想到的第一個想法是,你以某種方式使用'內容'或'窗口'對象 – shybovycha 2010-11-21 22:55:29

相關問題