简体   繁体   English

Java Swing 窗口不会出现

[英]Java Swing Window will not appear

Ok guys, here comes the humility.好吧,伙计们,谦逊来了。 It's been a long time since I used Java Swing so I know there is some really obvious solution to this problem.自从我使用 Java Swing 已经很长时间了,所以我知道这个问题有一些非常明显的解决方案。 What I'm trying to do is get all of these various swing elements to appear in a window.我想要做的是让所有这些不同的秋千元素出现在一个窗口中。 When I run the code, nothing happens.当我运行代码时,没有任何反应。 I don't see anything at all.我什么都看不到。 Every time I google the answer I get stuff about various complicated JPanel problems and I'm almost positive this isn't a difficult issue.每次我用谷歌搜索答案时,我都会得到有关各种复杂 JPanel 问题的信息,而且我几乎肯定这不是一个困难的问题。 So here's my code:所以这是我的代码:

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

    }

}

The problem is, the window doesn't show up at all.问题是,窗口根本不显示。 Once I can get that sorted out, I'll be able to troubleshoot the rest of it.一旦我能解决这个问题,我就能解决剩下的问题。 Thanks in advance everybody.提前谢谢大家。

You need to set it to visible.您需要将其设置为可见。 Use:用:

setVisible(true)

Some tips:一些技巧:

  • Need to make visible your JFrame calling setVisible(true)需要使调用setVisible(true) 的JFrame可见

  • Instead of add(pane) you can use setContentPane(pane) replacing the default container used as content pane.您可以使用setContentPane(pane)代替用作内容窗格的默认容器来代替add(pane)

  • Don't forget to call pack() method when you finish adding components and before making your JFrame visible..在完成添加组件后和使JFrame可见之前,不要忘记调用pack()方法。

  • Create your GUI objects in the Event Dispatch Thread using SwingUtilities.invokeLater()使用SwingUtilities.invokeLater()事件调度线程中创建您的 GUI 对象

  • Avoid extend from JFrame unless you need to add some functionality.避免从JFrame扩展,除非您需要添加一些功能。 If that's not the case, use a JFrame variable or class member instead.如果不是这种情况,请改用JFrame变量或类成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM