我的自定义 JDialog 出现,但为空

发布于 2024-10-03 19:48:26 字数 2549 浏览 5 评论 0原文

在我的程序中的某个时刻,它会打开一个 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...)
}

一旦此窗口打开,程序的其余部分将继续正常执行,只是此窗口是空白的。我错过了什么吗?

At a point in my program, it opens a JDialog that displays information about what is going on as the program runs. It has several labels and a progress bar, but when the dialog window opens, it doesn't display anything.

Here's the custom dialog and its constructor:

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...)
}

Once this window opens, the rest of the program keeps executing as normal, just this window is blank. Am I missing anything?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁的新欢旧爱 2024-10-10 19:48:26

可能是因为您阻塞了 EventDispatchThread,GUI 无法重新绘制自身。有关详细信息,请阅读 Swing 教程中有关并发的部分。

由于您没有提供正确的 SSCCE,我们只能猜测。

Probably because you are blocking the EventDispatchThread, to the GUI can't repaint itself. Read the section from the Swing tutorial on Concurrency for more information.

Since you didn't provide a proper SSCCE we can't do any more than guess.

小…楫夜泊 2024-10-10 19:48:26

这真的很奇怪。这段代码对我来说工作得很好,意思是显示一个空的主窗口(这是正确的)和“弹出窗口”子框架窗口(你说这不适合你)。我在 Ubuntu 下使用了 NetBeans 6.8 和 Java 1.6:

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

}

That's really strange. This code works just fine for me meaning shows an empty main window (which is right) and "pop-ups" children frame window (which you are saying do not works for you). I've used NetBeans 6.8 and Java 1.6 under 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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文