简体   繁体   中英

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

  • Instead of add(pane) you can use setContentPane(pane) replacing the default container used as content pane.

  • Don't forget to call pack() method when you finish adding components and before making your JFrame visible..

  • Create your GUI objects in the Event Dispatch Thread using SwingUtilities.invokeLater()

  • Avoid extend from JFrame unless you need to add some functionality. If that's not the case, use a JFrame variable or class member instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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