关于jframe窗口关闭确认对话框的问题
以下的程序可以在关闭窗口时弹出一个确认对话框,但是其中的窗口是用AWT的Frame,如果用swing的JFrame弹出一个确认对话框后,无论点击确定还是取消都会关闭窗口,...
以下的程序可以在关闭窗口时弹出一个确认对话框,但是其中的窗口是用AWT的Frame,如果用swing的JFrame弹出一个确认对话框后,无论点击确定还是取消都会关闭窗口,有没有什么方法可以让窗口在点击取消后不关闭。
请高手指点指点啊!!!!
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
class MyFrame extends Frame {
boolean w;
public MyFrame() {
setTitle("test");
setSize(300, 200);
Panel panel = new Panel();
add(panel);
addWindowListener(new MyWindowAdapter());
}
private class MyWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent wevent){//实现窗口关闭
int i = JOptionPane.showConfirmDialog(null, "确定停止服务器?", "系统提示",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if(i == JOptionPane.YES_OPTION)System.exit(0);
else return;
}
}
}
public class CloseConfirm{
public static void main(String[] args) {
MyFrame frm = new MyFrame();
frm.show();
}
} 展开
请高手指点指点啊!!!!
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
class MyFrame extends Frame {
boolean w;
public MyFrame() {
setTitle("test");
setSize(300, 200);
Panel panel = new Panel();
add(panel);
addWindowListener(new MyWindowAdapter());
}
private class MyWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent wevent){//实现窗口关闭
int i = JOptionPane.showConfirmDialog(null, "确定停止服务器?", "系统提示",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if(i == JOptionPane.YES_OPTION)System.exit(0);
else return;
}
}
}
public class CloseConfirm{
public static void main(String[] args) {
MyFrame frm = new MyFrame();
frm.show();
}
} 展开
3个回答
展开全部
在窗体的构造函数里加一句
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class MyFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public MyFrame() {
setTitle("test");
setSize(300, 200);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
Panel panel = new Panel();
add(panel);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent wevent) {//实现窗口关闭
int i = JOptionPane.showConfirmDialog(null, "确定停止服务器?", "系统提示",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if(i == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
}
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class MyFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public MyFrame() {
setTitle("test");
setSize(300, 200);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
Panel panel = new Panel();
add(panel);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent wevent) {//实现窗口关闭
int i = JOptionPane.showConfirmDialog(null, "确定停止服务器?", "系统提示",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if(i == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询