10天轻松学习javase第8天下,Java gui 图像界面编程之事件监听
直接运行例子,注释很详细
package javaseof10day.day8.pm.Listener;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class Demo1ActionListener extends JFrame {
private static final long serialVersionUID = 1L;
JPanel jPanelAll;
JButton button;
int clicks = 0;
public Demo1ActionListener() {
button = new JButton("我是普通按钮"); // 创建JButton对象
button.setFont(new Font("黑体", Font.BOLD, 16)); // 修改字体样式
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
button.setText("按钮被单击了 " + (clicks++) + " 次");
}
});
this.setTitle("Demo1ActionListener");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100, 100, 400, 200);
jPanelAll = new JPanel();
jPanelAll.setBorder(new EmptyBorder(5, 5, 5, 5));
jPanelAll.se