📄 buttonexample.java
字号:
import java.awt.*;
import java.awt.event.*;
class ButtonExample
extends WindowAdapter
implements ActionListener {
Frame f;
Button b;
TextField tf;
int tag = 0;
public static void main(String args[]) {
ButtonExample be = new ButtonExample( );
be.go( );
}
public void go( ) {
f = new Frame("ButtonExample");
b = new Button("Sample");
b.addActionListener(this);
f.add(b,"South");
tf = new TextField( );
f.add(tf,"Center");
f.addWindowListener(this);
f.setSize(300,150);
f.setVisible(true);
}
// 实现ActionListener接口中的actionPerformed( )方法
public void actionPerformed(ActionEvent e) {
String s1 = "You have pressed the Button!";
String s2 = "You do another time!";
if (tag==0) {
tf.setText(s1);
tag = 1;
} else {
tf.setText(s2);
tag = 0;
}
}
// 覆盖WindowAdapter类中的windowClosing( )方法
public void windowClosing(WindowEvent e){
// 结束程序运行
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -