代码中没有错误,但我似乎看不到窗口中的Jlabels。我不确定是否添加了面板,或者是否将jlabels添加到面板中。
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);