创建java.awt.Frame类的子类,并添加两个按钮“确定”和“退出”和一个文本框,点击“确定”按钮,在文本框显示“Hello World!”,点击退出则关闭窗口,退出程序。
时间: 2024-03-21 20:42:11 浏览: 12
好的,我来给你编写一个简单的示例代码:
```java
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame {
private TextField textField;
public MyFrame() {
super("My Frame");
setLayout(new FlowLayout());
textField = new TextField(20);
add(textField);
Button okButton = new Button("确定");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("Hello World!");
}
});
add(okButton);
Button exitButton = new Button("退出");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
add(exitButton);
pack();
setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
}
```
这个代码创建了一个名为 `MyFrame` 的 `Frame` 类的子类,添加了一个文本框和两个按钮。点击“确定”按钮会在文本框中显示“Hello World!”,点击“退出”按钮则关闭窗口并退出程序。希望这个示例能够帮助到你!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)