如何用java实现“点击一个按钮,出现另一个按钮”的功能?
2个回答
2013-08-04
展开全部
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);
}
}
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);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-04
展开全部
要强制进行重绘
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询