代码中没有错误,但我似乎无法在窗口中看到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();
}
}
答案 0 :(得分:1)
您尚未将面板添加到框架中,这就是您无法看到任何组件的原因。在设置JFrame
可见之前添加它。
//Add the panel to the frame
this.add(panel)
setVisible(true);
答案 1 :(得分:0)
您也必须将面板添加到JFrame
。
panel.add(...);
add(panel); // <-- you forgot this
setVisible(true);