java 点击按钮关闭窗口
时间: 2023-10-14 15:11:48 浏览: 49
你可以在 Java 中使用以下代码来实现点击按钮关闭窗口:
```
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(button);
frame.dispose();
}
});
```
这会在按下按钮时获取窗口的 JFrame 对象,然后调用其 dispose() 方法来关闭窗口。
相关问题
java点击按钮关闭窗口
在 Java 中,可以使用 `javax.swing.JFrame` 类来创建窗口,并使用 `javax.swing.JButton` 类来创建按钮。要实现点击按钮关闭窗口的功能,你可以按照以下步骤进行操作:
1. 导入所需的类:
```java
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
```
2. 创建 `JFrame` 对象,并设置窗口的属性:
```java
JFrame frame = new JFrame("窗口标题");
frame.setSize(400, 300); // 设置窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口关闭操作
```
3. 创建 `JButton` 对象,并设置按钮的属性:
```java
JButton button = new JButton("关闭窗口");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose(); // 关闭窗口
}
});
```
4. 将按钮添加到窗口中:
```java
frame.add(button);
```
5. 显示窗口:
```java
frame.setVisible(true);
```
通过以上代码,你可以创建一个带有关闭窗口功能的 Java 窗口。当用户点击按钮时,窗口将会被关闭。请注意,`frame.dispose()` 方法用于释放与窗口关联的所有资源,并将其从屏幕上移除。如果你想要隐藏窗口而不是关闭窗口,可以使用 `frame.setVisible(false)` 方法。
java点击按钮关闭当前窗口
要在Java中点击按钮关闭当前窗口,可以使用以下代码:
```java
import java.awt.event.*;
import javax.swing.*;
public class CloseWindowButton extends JFrame {
private JButton closeButton;
public CloseWindowButton() {
super("Close Window Button");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建一个按钮
closeButton = new JButton("Close Window");
// 添加按钮的点击事件监听器
closeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 关闭当前窗口
dispose();
}
});
// 添加按钮到窗口中
add(closeButton);
// 设置窗口大小和位置
setSize(300, 200);
setLocationRelativeTo(null);
// 显示窗口
setVisible(true);
}
public static void main(String[] args) {
new CloseWindowButton();
}
}
```
在这个例子中,我们创建了一个名为 `CloseWindowButton` 的 `JFrame` 窗口,并添加了一个名为 `closeButton` 的按钮。当用户点击这个按钮时,会触发 `closeButton` 的点击事件监听器,调用 `dispose()` 方法关闭当前窗口。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)