package _8;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class EventDemo extends JFrame{
JButton button = new JButton("点我!");
public EventDemo() {
setSize(300, 300);
setLocationRelativeTo(null); // 窗口居中显示
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 匿名内部类实现按钮点击事件
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 获取触发事件的按钮
JButton clickedButton = (JButton) e.getSource();
// 修改按钮文本
clickedButton.setText("我被点了!");
}
});
setLayout(new FlowLayout());
add(button);
}
public static void main(String[] args) {
EventDemo frame = new EventDemo();
frame.setVisible(true); // 显示窗口
}
}
2万+

被折叠的 条评论
为什么被折叠?



