📄 eventdemo3.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EventDemo3 extends JFrame implements ActionListener //主类实现ActionListener方法
{
private JButton button;
private Container container;
public EventDemo3()
{
super("EventDemo3");
setSize(300,200);
try
{ //系统外观
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}
//获取内容面板
container = getContentPane();
//创建按钮
button = new JButton("改变颜色");
button.setFont(new Font("Serif", Font.PLAIN, 14));
//注册监听器
button.addActionListener(this);
container.add(button, BorderLayout.SOUTH);
setVisible(true); //设置窗口可见
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event) //ActionListener接口中的,处理事件的方法
{
int red = (int)(Math.random() * 256);
int green = (int)(Math.random() * 256);
int blue = (int)(Math.random() * 256);
container.setBackground(new Color(red, green, blue));
}
public static void main(String[] arguments)
{
EventDemo3 application = new EventDemo3();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -