2013-10-09 55 views
1

好吧,这里就是谦逊。我使用Java Swing已经很长时间了,所以我知道这个问题有一些非常明显的解决方案。我想要做的是让所有这些不同的摆动元素出现在窗口中。当我运行代码时,没有任何反应。我什么都看不到。每当我回答Google的答案时,我都会得到关于各种复杂的JPanel问题的东西,而且我几乎是积极的,这不是一个难题。所以这是我的代码:Java Swing Window不会出现

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JProgressBar; 


public class LimoSysDriver extends JFrame implements ActionListener { 

    /** 
    * @param args 
    */ 
    JLabel title = new JLabel("Thread Test Application"); 

    JLabel numOne = new JLabel("1"); 
    JLabel numTwo = new JLabel("2"); 
    JLabel numThr = new JLabel("3"); 
    JLabel numFou = new JLabel("4"); 

    JProgressBar progOne = new JProgressBar(); 
    JProgressBar progTwo = new JProgressBar(); 
    JProgressBar progThr = new JProgressBar(); 
    JProgressBar progFou = new JProgressBar(); 

    JLabel counterOne = new JLabel(Integer.toString(progOne.getValue())); 
    JLabel counterTwo = new JLabel(Integer.toString(progTwo.getValue())); 
    JLabel counterThr = new JLabel(Integer.toString(progThr.getValue())); 
    JLabel counterFou = new JLabel(Integer.toString(progFou.getValue())); 

    JLabel numGrandTot = new JLabel("Grand Total"); 
    JLabel counterTot = new JLabel(); 

    JButton start = new JButton(); 
    JButton pause = new JButton(); 
    JButton resume = new JButton(); 


    public LimoSysDriver(){ 
     setSize(700,300); 
     JPanel pane = new JPanel(); 
     pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS)); 
     add(pane); 
     JPanel lowerPanel = new JPanel(); 
     lowerPanel.setLayout(new BoxLayout(lowerPanel, BoxLayout.LINE_AXIS)); 
     add(lowerPanel); 

     pane.add(title); 
     pane.add(numOne); 
     pane.add(progOne); 
     pane.add(counterOne); 

     pane.add(numTwo); 
     pane.add(progTwo); 
     pane.add(counterTwo); 

     pane.add(numThr); 
     pane.add(progThr); 
     pane.add(counterThr); 

     pane.add(numFou); 
     pane.add(progFou); 
     pane.add(counterFou); 


    } 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     LimoSysDriver window = new LimoSysDriver(); 

    } 

    @Override 
    public void actionPerformed(ActionEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

} 

问题是,窗口根本没有显示出来。一旦我能把它整理出来,我就可以解决其余的问题。在此先感谢大家。

回答

2

您需要将其设置为可见。用途:

setVisible(true) 
+0

确保您在将所有内容添加到要显示的窗格后结束操作 – Levenal

2

一些提示:

  • 需要,使您看到调用JFramesetVisible(true)

  • 而不是add(pane)可以使用setContentPane(pane)更换作为内容窗格中的默认容器。

  • 不要忘记调用pack()方法,当你完成添加组件,使您的JFrame可见之前..

  • Event Dispatch Thread创建GUI对象使用SwingUtilities.invokeLater()

  • 避免JFrame延长,除非你需要添加一些功能。如果不是这种情况,请改用JFrame变量或类成员。

+0

感谢您的回复。我曾被告知使用SwingUtilities。请问为什么?优点是什么?我相信你是对的。我只想更全面地理解。此外,这种方法是否允许您为每个对象指定特定的线程?我想为每个进度条都有一个线程,但是我一直无法使其工作。 - - - 没关系!我正在阅读API,我明白了。感谢您的帮助! – Rambo8000

+0

Swing组件应该始终在一个名为Event Dispatch Thread的单个特殊线程中创建更新。这是必要的,因为大多数Swing对象方法不是“线程安全的”。关于进度条我建议你使用[SwingWorker](http://www.javacreed.com/swing-worker-example/),它提供了在后台线程中执行长任务的方法,并更新了Swing组件(进度条)在美国东部时间。 – dic19

+0

太棒了。谢谢! – Rambo8000