Java JButtons和jlabel没有出现

Java JButtons和jlabel没有出现,java,swing,jbutton,Java,Swing,Jbutton,我的代码有什么问题?我的按钮和标签没有出现 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 HelloPanelLabe

我的代码有什么问题?我的按钮和标签没有出现

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

    }

}
您需要设置一个框架并将组件添加到框架中

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

就像前面提到的注释一样,您必须调用
pack()
。但是,如果您想定义更复杂的布局,则必须创建一个更复杂的布局。

没有显示
JButton
JLabel
的原因是您没有将包含这两个组件的
JPanel
添加到
JFrame
中。您只需要对代码进行一些修改。这是:

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

并删除程序中前面的
this.setVisible(true)
行。

如果要为组件使用
JPanel

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); 
    }
}
或者,您可以直接将它们添加到
JFrame
contentPane

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

删除第一个
this.setVisible(true)
call为什么不取消最后3行的注释,
add()
面板并调用
pack()以查看发生了什么。另请接受@Robin的建议。@Robin请删除此答案,是否正确……@mKorbel我忘了提及取消注释其他答案中已经提到的
add
语句感谢您在这方面的帮助,非常支持无需设置布局。因为默认情况下,
FlowLayout
。只需添加
JComponents
,并调用
pack()
@user2336315我想纠正我的说法,我错了,你必须添加布局!!!相反,在
JPanel
上,似乎没有必要。@user2336315不太需要。您将只看到最后一个组件,在本例中是
JButton
。你为什么不试试呢?谢谢你的帮助。这很有效。add(panel1)做什么?@user1156718在容器中添加组件时,使用
add()
方法,在添加容器时也是如此。根据差异,将其添加到顶级容器中。看这个。如果您找到了解决方案,请不要忘记选择答案