繁体   English   中英

将面板添加到JFrame

[英]Adding a panel to a JFrame

代码中没有错误,但我似乎无法在窗口中看到Jlabels。 我不确定是否添加了面板或是否将jlabel添加到了面板。

public class JDemoResistance extends JFrame{

    private final JButton button1;
    private JPanel panel;
    private final int WINDOW_WIDTH = 320;
    private final int WINDOW_HEIGHT = 320;

    public JDemoResistance() {
        super("JDemoResistance");

        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //JLabels Configs
        JLabel label1 = new JLabel("Too expensive");
        JLabel label2 = new JLabel("Bad reviews");
        JLabel label3 = new JLabel("Bad quality");
        JLabel label4 = new JLabel("Not worth it");
        JLabel label5 = new JLabel("Dosent work");

        //Button Configs
        button1 = new JButton("Button");
        button1.addActionListener(new ButtonListener());

        //Panel Configs
        panel = new JPanel();
        panel.add(label1);
        panel.add(label2);
        panel.add(label3);
        panel.add(label4);
        panel.add(label5);
        panel.add(button1);
        setVisible(true);
    }

      private class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e){

        }
    }      

    public static void main(String[] args) {
        JDemoResistance jdr = new JDemoResistance();

    }


}

您尚未将面板添加到框架,这就是为什么看不到任何组件的原因。 在将JFrame设置为可见之前添加它。

 //Add the panel to the frame
 this.add(panel)
 setVisible(true);

您还必须将面板添加到JFrame

panel.add(...);
add(panel); // <-- you forgot this
setVisible(true);

暂无
暂无

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

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