简体   繁体   中英

JButtons and JLabels are not appearing

What is wrong with my code? My buttons and the labels are not appearing.

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

public class HelloPanelLabel extends JFrame {

    public static void main(String[] args) {
        new HelloPanelLabel(); // creates an instance of frame class
    }

    public HelloPanelLabel() {

        this.setSize(200, 100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Hello World!");
        this.setVisible(true);

        Toolkit tk=Toolkit.getDefaultToolkit();
        Dimension d= tk.getScreenSize();
        int x=(d.height/2);
        int y=(d.width/2);
        this.setLocation(x, y);
        //JPanel panel1 = new JPanel();
        JLabel label1 = new JLabel("hello, world");
        //panel1.add(label1);
        JButton button1 = new JButton("Click me!");
        //panel1.add(button1);
        this.setVisible(true);

    }

}

You need to set a layout and add your components to the frame.

setLayout(new FlowLayout());
//JPanel panel1 = new JPanel();
JLabel label1 = new JLabel("hello, world");
add(label1);
//panel1.add(label1);
JButton button1 = new JButton("Click me!");
add(button1);
//panel1.add(button1);
 this.setVisible(true);

Like the comments stated, you have to call pack() . However, if you want to define more complex layouts, you will have to create a more complex.

The reason for not showing of JButton and JLabel is that you have not added the JPanel containing these two components to the JFrame .You just need a little modification in your code. Here is that:

panel1.add(label1);
JButton button1 = new JButton("Click me!");
panel1.add(button1);
getContentPane().add(panel1);//Add to ContentPane of JFrame
this.setVisible(true);

And remove the the previous this.setVisible(true) line in your programe.

If you want to use a JPanel for your components

public class HelloPanelLabel extends JFrame {

    public static void main(String[] args) {
        new HelloPanelLabel().setVisible(true);
    }

    public HelloPanelLabel() {
        //The same as setTitle.
        super("Hello World!");

        JPanel panel1 = new JPanel();
        JLabel label1 = new JLabel("hello, world");
        panel1.add(label1);
        JButton button1 = new JButton("Click me!");
        panel1.add(button1);
        add(panel1);
        //Size the frame to fit the components
        pack();

        //Center the frame.
        setLocationRelativeTo(null);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    }
}

alternatively you can add them directly to the contentPane of the JFrame .

    setLayout(new FlowLayout());

    JLabel label1 = new JLabel("hello, world");
    add(label1);
    JButton button1 = new JButton("Click me!");
    add(button1);

    Toolkit theKit = getToolkit();
    Dimension wndSize = theKit.getScreenSize();

    setSize(wndSize.width / 8, wndSize.height / 12);

    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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