我是java新手。java中点击关闭窗口,然后怎样才能出现提示对话框呢?点击确定才真正退出的那种。麻烦啦
java中关闭窗口用privateclassWindowCloserextendsWindowAdapter{publicvoidwindowClosing(Window...
java中关闭窗口用private class WindowCloser extends WindowAdapter
{public void windowClosing(WindowEvent e)
{dispose(); }可是我想点击关闭窗口时会出现“是否确定退出”的对话框,点击确定才退出的那种,点击取消就不会退出程序。请问要怎样弄才能搞定呢? 展开
{public void windowClosing(WindowEvent e)
{dispose(); }可是我想点击关闭窗口时会出现“是否确定退出”的对话框,点击确定才退出的那种,点击取消就不会退出程序。请问要怎样弄才能搞定呢? 展开
3个回答
展开全部
关键:
1 调用JFrame的setDefaultCloseOperation(),设置关闭模式为DO_NOTHING_ON_CLOSE(就是默认关闭效用减半,仅触发WindowListener中的windowClosing方法)
2 弹对话框,确认关闭后,添加JFrame的dispose()
代码:
package gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class MainFrame extends JFrame {
//退出按钮
JButton exitBtn;
//保留本框架实例的指针
JFrame thisFrame;
public static void main(String[] args) {
MainFrame mainFrame=new MainFrame();
mainFrame.pack();
mainFrame.setVisible(true);
}
public MainFrame(){
thisFrame=this;
initGUI();
}
private void initGUI(){
exitBtn=new JButton("退出");
exitBtn.setBounds(0, 0, 30, 20);
exitBtn.addActionListener(new exitBtnListener());
thisFrame.getContentPane().add(exitBtn);
thisFrame.addWindowListener(new windowListener());
//关键!框架将不会真正关闭
thisFrame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
}
//确认关闭
private void sureExit(){
int result=JOptionPane.showConfirmDialog(this, "你确定要退出本窗口不?", "Information", JOptionPane.YES_NO_OPTION);
if(result==JOptionPane.NO_OPTION){
return;
}
//真正执行关闭的语句
thisFrame.dispose();
}
//实现接口
class windowListener implements WindowListener{
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
//在关闭时调用“确认关闭”方法
sureExit();
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}
//实现接口
class exitBtnListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//调用“确认关闭”方法
sureExit();
}
}
}
1 调用JFrame的setDefaultCloseOperation(),设置关闭模式为DO_NOTHING_ON_CLOSE(就是默认关闭效用减半,仅触发WindowListener中的windowClosing方法)
2 弹对话框,确认关闭后,添加JFrame的dispose()
代码:
package gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class MainFrame extends JFrame {
//退出按钮
JButton exitBtn;
//保留本框架实例的指针
JFrame thisFrame;
public static void main(String[] args) {
MainFrame mainFrame=new MainFrame();
mainFrame.pack();
mainFrame.setVisible(true);
}
public MainFrame(){
thisFrame=this;
initGUI();
}
private void initGUI(){
exitBtn=new JButton("退出");
exitBtn.setBounds(0, 0, 30, 20);
exitBtn.addActionListener(new exitBtnListener());
thisFrame.getContentPane().add(exitBtn);
thisFrame.addWindowListener(new windowListener());
//关键!框架将不会真正关闭
thisFrame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
}
//确认关闭
private void sureExit(){
int result=JOptionPane.showConfirmDialog(this, "你确定要退出本窗口不?", "Information", JOptionPane.YES_NO_OPTION);
if(result==JOptionPane.NO_OPTION){
return;
}
//真正执行关闭的语句
thisFrame.dispose();
}
//实现接口
class windowListener implements WindowListener{
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
//在关闭时调用“确认关闭”方法
sureExit();
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}
//实现接口
class exitBtnListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//调用“确认关闭”方法
sureExit();
}
}
}
参考资料: http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/WindowConstants.html
展开全部
JOptionPane pane = new JOptionPane();
if(pane.showConfirmDialog(null, "是否确定退出??") == JOptionPane.OK_OPTION)
{
System.out.println("dd");
}
if(pane.showConfirmDialog(null, "是否确定退出??") == JOptionPane.OK_OPTION)
{
System.out.println("dd");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
推荐2楼的。
注意两点:
1.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
2. 监听windowClosing事件
注意两点:
1.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
2. 监听windowClosing事件
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询