📄 testframe.java
字号:
import java.awt.*;
import java.awt.event.*;
public class TestFrame {
public static void main(String args[]) {
new MyFrame();
}
}
//创建容器类Frame的子类MyFrame,并在其中实现Action事件
class MyFrame
extends Frame implements ActionListener {
//创建组件btn1,btn2,LB;
Button btn1, btn2;
Label LB;
MyFrame() {
super("My Window"); //设置MyFrame的标题文本
//初始化组件
btn1 = new Button("close");
btn2 = new Button("Hello");
LB = new Label("Hello,how do you do");
//为MyFrame设置布局管理器
setLayout(new FlowLayout());
//将组件添加到MyFrame中。
add(btn1);
add(btn2);
add(LB);
//为按钮注册监听器
btn1.addActionListener(this);
btn2.addActionListener(this);
setSize(300, 200);
setVisible(true);
}
//事件处理程序,处理按钮上的单击事件
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Hello")) {
LB.setText("Hello,how are you?");
}
if (e.getActionCommand().equals("close")) {
dispose();
System.exit(0);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -