如何用java实现“点击一个按钮,出现另一个按钮”的功能?

2024-12-02 05:38:32
推荐回答(2个)
回答1:

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);
}
}

回答2:

要强制进行重绘