如何使关闭窗口操作时,先提示用户是否确认要关闭窗口

linyucao 2003-10-16 08:14:23
如何使关闭窗口操作时,先提示用户是否确认要关闭窗口
...全文
329 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
giardino 2003-10-20
  • 打赏
  • 举报
回复
study
kinzey34 2003-10-20
  • 打赏
  • 举报
回复
哦,回完贴才看到楼上已经有人做好了,而且代码比较精练,不过
他的程序可以改进是部分代码多余:
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
kinzey34 2003-10-20
  • 打赏
  • 举报
回复
根据你的要求处理右上角的关闭按钮,并且解决你上面刚提出的问题,我又写了一遍程序如下:

import java.awt.event.*;
import javax.swing.*;

class Test extends JFrame implements ActionListener
{
JButton buttonExit;

Test()
{
super("Exit test");

//我又改动了下面这句:
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

JPanel pane=new JPanel();
buttonExit=new JButton("退出");
buttonExit.addActionListener(this);
pane.add(buttonExit);
setContentPane(pane);
setSize(200,200);
show();
}

public static void main(String[] arguments)
{
Test test=new Test();
ExitWindow exit=new ExitWindow();
test.addWindowListener(exit);
}

public void actionPerformed(ActionEvent evt)
{
Object obj=evt.getSource();
if(obj instanceof JButton)
{
JButton source=(JButton)obj;

if(source==buttonExit)
{
int response;
response=JOptionPane.showConfirmDialog(null,"真的要退出吗?");
if(response==JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
}
}
}

//WindowAdapter实现了WindowListener接口
class ExitWindow extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
int response;
response=JOptionPane.showConfirmDialog(null,"真的要退出吗?");
if(response==JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
}
cbhyk 2003-10-20
  • 打赏
  • 举报
回复
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Temp
{
public static void main(String[] args)
{
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
int select = JOptionPane.showConfirmDialog(frame, "真的要退出吗?");
if (select == JOptionPane.YES_OPTION)
frame.dispose();
}

public void windowClosed(WindowEvent e)
{
System.exit(0);
}
});
frame.setSize(320, 240);
frame.setLocation(240, 180);
frame.setVisible(true);
}
}
linyucao 2003-10-20
  • 打赏
  • 举报
回复
up
linyucao 2003-10-17
  • 打赏
  • 举报
回复
我用下列代码来控制右上角的关闭按钮,但无论选择JOptionPane的拿一个选项,窗体都关闭
void this_windowClosing(WindowEvent e) {
int response;
response=JOptionPane.showConfirmDialog(null,"真的要退出吗?");
if(response==JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
zkjbeyond 2003-10-17
  • 打赏
  • 举报
回复
把JFrame放入窗体程序中。修改那个程序就OK了。

你不要在JFrame中写
if(response==JOptionPane.YES_OPTION)
{
System.exit(0);
}
而在他的父窗体中
j4sxw 2003-10-17
  • 打赏
  • 举报
回复
UP
linyucao 2003-10-17
  • 打赏
  • 举报
回复
up
linyucao 2003-10-16
  • 打赏
  • 举报
回复
jbuilder中控制关闭窗体的代码是什么,怎么找不到
happyegg 2003-10-16
  • 打赏
  • 举报
回复
我想任何窗体应该都有onClose()事件吧
linyucao 2003-10-16
  • 打赏
  • 举报
回复
如果用右上角的关闭按钮,则不会出现对话框
kinzey34 2003-10-16
  • 打赏
  • 举报
回复
我做了个示例程序,你可作为参考:

import java.awt.event.*;
import javax.swing.*;

class Test extends JFrame implements ActionListener
{
JButton buttonExit;

Test()
{
super("Exit test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel pane=new JPanel();
buttonExit=new JButton("退出");
buttonExit.addActionListener(this);
pane.add(buttonExit);
setContentPane(pane);
setSize(200,200);
show();
}

public static void main(String[] arguments)
{
Test test=new Test();
}

public void actionPerformed(ActionEvent evt)
{
Object obj=evt.getSource();
if(obj instanceof JButton)
{
JButton source=(JButton)obj;

//注意下面的代码可能是你想要的,实现询问对话框和处理
if(source==buttonExit)
{
int response;
response=JOptionPane.showConfirmDialog(null,"真的要退出吗?");
if(response==JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
}
}
}
hxzhappy 2003-10-16
  • 打赏
  • 举报
回复
将父窗口关闭就可以了!
parent.close();
LoveRose 2003-10-16
  • 打赏
  • 举报
回复
在弹出一个对话框!
要求用户确认,确定后
关闭整个应用程序了

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧