我是Java新手。我正在尝试将jLabel添加到我的Jframe中,但未显示。我也尝试添加JButton,但它似乎不起作用。过去几天我尝试了很多事情。我的代码有什么问题吗?谢谢。
public class F2 extends JPanel implements ActionListener {
JFrame frame = new JFrame();
JLabel label = new JLabel();
int j = 2;
ArrayList<Person> people = new ArrayList<>();
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
F2 f = new F2();
}
public F2() {
j++;
frame = new JFrame();
frame.setSize(1380, 728);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Timer t = new Timer(20, this);
t.restart();
label.setText("Test");
frame.add(label);
frame.pack();
frame.add(this);
frame.setVisible(true);
for (int i = 0; i < 100; i++) {
people.add(new Person(0));
}
}
public void paint(Graphics g) {
super.paintComponent(g);
Places p1 = new Places(g);
for (Person p : people) {
p.paint(g); //recall that each Person object has a paint method. We're passing g as the argument
}
}
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}
复制亚历克斯·鲁登科的答案:
您应该将标签添加到面板,然后将面板添加到框架。现在,您的空白面板将替换标签。