【问题标题】:JButtons and JLabels are not appearingJButtons 和 JLabels 没有出现
【发布时间】:2013-06-14 23:22:39
【问题描述】:

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

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

    }

}

【问题讨论】:

  • 删除第一个this.setVisible(true)调用
  • 为什么不取消注释最后 3 行,add() 面板并致电 pack(); 看看会发生什么。也接受@Robin 的建议。
  • @Robin 请有理由删除这个答案,是正确的......
  • @mKorbel 我忘了提取消注释已在其他答案中解决的add 声明
  • 感谢大家对此的帮助,大力支持

标签: java swing jbutton


【解决方案1】:

JButtonJLabel没有显示的原因是你没有将包含这两个组件的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) 行。

【讨论】:

    【解决方案2】:

    您需要设置layout 并将您的组件添加到框架中。

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

    如 cmets 所述,您必须致电 pack()。但是,如果要定义更复杂的布局,则必须创建更复杂的布局。

    【讨论】:

    • 不需要设置布局。因为FlowLayout是默认的。只需添加JComponents,然后致电pack()
    • @user2336315 我想纠正我的说法,我错了你必须添加一个布局!而不是JPanel 似乎没有必要。
    • @user2336315 不是真的。您只会看到最后一个组件,在本例中为 JButton。你为什么不测试一下
    【解决方案3】:

    如果你想为你的组件使用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); 
        }
    }
    

    您也可以将它们直接添加到contentPaneJFrame

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

    【讨论】:

    • 感谢大家的帮助。这工作得很好。 add(panel1) 有什么作用?
    • @user1156718 在容器中添加组件时,您使用add() 方法,与添加容器时类似。不同的是,您将其添加到顶级容器中。请参阅此docs.oracle.com/javase/tutorial/uiswing/components/…如果您找到解决方案,请不要忘记选择答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多