我是 SWING 的新手。我已经编写了一个框架代码,当单击一个新框架时会打开一个按钮。但我不知道新框架/窗口不包含我在代码中添加的按钮的代码有什么问题。
setTitle("Frame1");
setSize(250, 250);
setDefaultCloseOperation(1);
setVisible(true);
JPanel panel1= new JPanel();
getContentPane().add(panel1);
JButton button1= new JButton();
button1.setText("Click to open new window");
button1.setBounds(20, 15, 14, 18);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JFrame newFrame= new JFrame();
newFrame.setBounds(150, 150, 150, 150);
newFrame.setDefaultCloseOperation(1);
newFrame.setTitle("New Frame");
newFrame.setVisible(true);
JPanel panel2= new JPanel();
getContentPane().add(panel2);
JButton button2= new JButton();
button2.setText("QUIT");
button2.setBounds(10, 5, 4, 8);
panel2.add(button2);
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
}
});
panel1.add(button1);}
请帮忙!