点击button把值输入到TextField
时间: 2024-02-28 08:53:05 浏览: 21
在 Java AWT 中,可以在 ActionListener 的回调函数中获取 TextField 的实例并设置其文本值,以将按钮点击事件的值输入到 TextField 中。以下是一个示例:
```java
import java.awt.*;
import java.awt.event.*;
public class ButtonTextFieldExample {
public static void main(String[] args) {
Frame frame = new Frame("Button TextField Example");
TextField textField = new TextField();
textField.setBounds(50, 50, 150, 30);
Button button = new Button("Click me");
button.setBounds(50, 100, 80, 30);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("Button clicked");
}
});
frame.add(textField);
frame.add(button);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
}
```
在上面的示例中,我们创建了一个 TextField 和一个 Button,并为 Button 添加了一个 ActionListener,当按钮被点击时设置 TextField 的文本值为 "Button clicked"。你可以根据需要更改 TextField 和 Button 的位置和大小。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)