import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;@SuppressWarnings("serial")
public class ButtonFrame extends JFrame {
public ButtonFrame() {
buttonPanel = new JPanel();
setSize(500, 500);
makeButton(); add(buttonPanel);
}
public void makeButton() {
JButton buttonFather = new JButton("I'm father");
buttonFather.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
addButton(event);
}
});
buttonPanel.add(buttonFather);
}
private void addButton(ActionEvent event) {
JButton buttonSon = new JButton("I'm son");
buttonPanel.add(buttonSon);
this.validate();
}
private JPanel buttonPanel; public static void main(String[] args) {
new ButtonFrame().setVisible(true);
}
}
要强制进行重绘