📄 eventdemo.java
字号:
import java.awt.*;
import java.awt.event.*;
public class EventDemo
{
public static void main(String arg[])
{
Frame win = new Frame("my first GUI application");
Panel p1 = new Panel();
p1.setLayout(new BorderLayout());
final TextArea t = new TextArea("textfield");
t.setBackground(Color.red);
Button b = new Button("ok");
//用匿名类实现一个对Button的事件处理类对象
ActionListener action = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
t.append("\nHelloWorld");
}
};
//调用Button的方法添加按钮事件处理对象
b.addActionListener(action);
//用匿名类实现一个对Button的事件处理类对象
ActionListener actionClear = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
t.setText("");
}
};
Button c = new Button("clear");
//调用Button的方法添加按钮事件处理对象
c.addActionListener(actionClear);
//用匿名类实现一个对Window窗口的事件处理类对象
WindowListener l = new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
};
//添加窗口事件处理代理对象
win.addWindowListener(l);
win.add("Center",t);
win.add("East",b);
win.add("West",c);
win.pack();
win.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -