【发布时间】:2021-04-19 10:45:51
【问题描述】:
import java.awt.*;
import java.awt.event.*;
//1st step
public class ActionListenerExample implements ActionListener{
public static void main(String[] args) {
Frame f=new Frame("ActionListener Example");
final TextField tf=new TextField();
tf.setBounds(50,50, 150,20);
Button b=new Button("Click Here");
b.setBounds(50,100,60,30);
//2nd step
b.addActionListener(this);
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
//3rd step
public void actionPerformed(ActionEvent e){
tf.setText("Welcome to Javatpoint.");
}
}
在上面我想添加另一个按钮并添加 ActionListener 并定义 actionPerformed 方法,但该方法需要在终端中打印 hello。
我会为此做些什么?
【问题讨论】:
标签: java button awt actionlistener