编写最简单的GUI程序:只有一个Frame,当点击关闭按钮时,结束应用程序。使用8种不同的事件处理方式。
时间: 2024-03-20 19:43:59 浏览: 122
搭建最简单的GUI界面框架
好的,下面为您提供8种不同的事件处理方式的代码示例。
方式一:使用匿名内部类实现ActionListener接口
```
import java.awt.*;
import java.awt.event.*;
public class CloseFrame1 {
public static void main(String[] args) {
Frame f = new Frame("关闭窗口");
f.setSize(300, 200);
f.setLocationRelativeTo(null);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setVisible(true);
}
}
```
方式二:使用lambda表达式实现ActionListener接口
```
import java.awt.*;
import java.awt.event.*;
public class CloseFrame2 {
public static void main(String[] args) {
Frame f = new Frame("关闭窗口");
f.setSize(300, 200);
f.setLocationRelativeTo(null);
f.addWindowListener((WindowListener) e -> {
System.exit(0);
});
f.setVisible(true);
}
}
```
方式三:使用内部类实现ActionListener接口
```
import java.awt.*;
import java.awt.event.*;
public class CloseFrame3 {
public static void main(String[] args) {
Frame f = new Frame("关闭窗口");
f.setSize(300, 200);
f.setLocationRelativeTo(null);
MyWindowListener listener = new MyWindowListener();
f.addWindowListener(listener);
f.setVisible(true);
}
static class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}
```
方式四:使用外部类实现ActionListener接口
```
import java.awt.*;
import java.awt.event.*;
public class CloseFrame4 {
public static void main(String[] args) {
Frame f = new Frame("关闭窗口");
f.setSize(300, 200);
f.setLocationRelativeTo(null);
MyWindowListener listener = new MyWindowListener();
f.addWindowListener(listener);
f.setVisible(true);
}
}
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
```
方式五:使用继承Frame类并重写processWindowEvent方法
```
import java.awt.*;
public class CloseFrame5 extends Frame {
public CloseFrame5() {
super("关闭窗口");
setSize(300, 200);
setLocationRelativeTo(null);
}
public void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
super.processWindowEvent(e);
}
public static void main(String[] args) {
CloseFrame5 f = new CloseFrame5();
f.setVisible(true);
}
}
```
方式六:使用内部类继承Frame类并重写processWindowEvent方法
```
import java.awt.*;
public class CloseFrame6 {
public static void main(String[] args) {
MyFrame f = new MyFrame();
f.setVisible(true);
}
static class MyFrame extends Frame {
public MyFrame() {
super("关闭窗口");
setSize(300, 200);
setLocationRelativeTo(null);
}
public void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
super.processWindowEvent(e);
}
}
}
```
方式七:使用匿名内部类继承Frame类并重写processWindowEvent方法
```
import java.awt.*;
public class CloseFrame7 {
public static void main(String[] args) {
Frame f = new Frame("关闭窗口") {
public void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
super.processWindowEvent(e);
}
};
f.setSize(300, 200);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
```
方式八:使用lambda表达式继承Frame类并重写processWindowEvent方法
```
import java.awt.*;
public class CloseFrame8 {
public static void main(String[] args) {
Frame f = new Frame("关闭窗口") {
public void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
super.processWindowEvent(e);
}
};
f.setSize(300, 200);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
```
阅读全文