如何调用JFrame的windowClosing方法 我在JFrame里有一个JPanel,在JPanel里有一个按钮,我希望当这个按钮被按下时能调用JFrame里的windowClosing方法,该怎么写代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 windowClosing好像是回调,不是需要你调用的,你调用close就行了。 System.exit(-1)就直接退出程序了,我是在windowClosing里有确认对话框,我希望是无论用户点面板中的按钮还是窗口栏上的关闭按钮,在程序退出前都能响应这个方法,在确认后再退出 mainFrame.addWindowListener(new WindowAdapter(){ @Override public void windowClosing(WindowEvent e) { int option = JOptionPane.showConfirmDialog(null, "确定要退出?", "警告", JOptionPane.YES_NO_OPTION); if(option==0){ DatabaseManager.closeConn(); System.exit(-1); }else{ return; } } }); mainFrame.addWindowListener(new WindowAdapter(){ @Override public void windowClosing(WindowEvent e) { int option = JOptionPane.showConfirmDialog(null, "确定要退出?", "警告", JOptionPane.YES_NO_OPTION); if(option==0){ DatabaseManager.closeConn(); System.exit(-1); }else{ return; } } }); 楼上的兄弟,你写的这个代码我已经实现了,但我说的问题你还没实现啊mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);JPanel pa = new JPanel()JButton btnExit = new JButton("Exit");pa.add(btnExit);mainFrame.add(pa);btnExit.addMouseListener(new MouseAdapter(){public void mouseClicked(MouseEvent e){ 这里该怎么写?直接写System.exit(0)的话就不会判断而直接退出程序了}}); //JFrame jf=new JFrame("我惨了!");.........//某些事件发生jf.dispose(); sorry 我上面的发放不行 我又找了下 应当发这个消息 jf.dispatchEvent(new WindowEvent((Window)jf,WindowEvent.WINDOW_CLOSING));ps:你重写了windowClosing 这个envent 系统注册的就是WINDOW_CLOSING (先开始我以为是WINDOW_CLOSED事件结果关不掉 害我浪费好长时间找问题) 呵呵 我简单的写了个例子 你看看吧 呵呵 import java.awt.Window;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;public class test { private JFrame frame; public static void main(String args[]) { try { test window = new test(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } public test() { initialize(); } private void initialize() { frame = new JFrame(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0) { int option = JOptionPane.showConfirmDialog(null, "确定要退出?", "警告", JOptionPane.YES_NO_OPTION); if(option==0){ System.exit(-1); }else{ return; } } }); frame.getContentPane().setLayout(null); frame.setBounds(100, 100, 500, 375); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton button = new JButton(); button.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent arg0) { frame.dispatchEvent(new WindowEvent((Window)frame,WindowEvent.WINDOW_CLOSING)); } }); button.setText("关闭"); button.setBounds(81, 117, 197, 67); frame.getContentPane().add(button); }} linux中如何用Java读一个EXE的版本号 大家来说说 eclipse断点问题 想问一个关于 jfree chart的问题 ? 问各位高人一个问题,有哪个容器类有方法直接知道元素重复的呢??? 安装完JDK后,在桌面上出现了一个Java Web Start图标,它是干什么用的? reference问题 请问:重载与多态的区别? 简单问题:如何点击Frame2的按钮,改变Frame1的jLabel1的显示文字?? 我有好多好的软件产品,但是不知道怎么去销售,请帮忙! 关于JFrame画图的问题 创新工厂笔试题:输入一个数组,生成一个二叉排序树。
@Override
public void windowClosing(WindowEvent e) {
int option = JOptionPane.showConfirmDialog(null, "确定要退出?", "警告", JOptionPane.YES_NO_OPTION);
if(option==0){
DatabaseManager.closeConn();
System.exit(-1);
}else{
return;
}
}
});
@Override
public void windowClosing(WindowEvent e) {
int option = JOptionPane.showConfirmDialog(null, "确定要退出?", "警告", JOptionPane.YES_NO_OPTION);
if(option==0){
DatabaseManager.closeConn();
System.exit(-1);
}else{
return;
}
}
});
mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
JPanel pa = new JPanel()
JButton btnExit = new JButton("Exit");
pa.add(btnExit);
mainFrame.add(pa);
btnExit.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
这里该怎么写?直接写System.exit(0)的话就不会判断而直接退出程序了
}
});
JFrame jf=new JFrame("我惨了!");
...
...
...
//某些事件发生
jf.dispose();
jf.dispatchEvent(new WindowEvent((Window)jf,WindowEvent.WINDOW_CLOSING));
ps:你重写了windowClosing 这个envent 系统注册的就是WINDOW_CLOSING (先开始我以为是WINDOW_CLOSED事件结果关不掉 害我浪费好长时间找问题) 呵呵
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;public class test { private JFrame frame; public static void main(String args[]) {
try {
test window = new test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} public test() {
initialize();
} private void initialize() {
frame = new JFrame();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
int option = JOptionPane.showConfirmDialog(null, "确定要退出?", "警告", JOptionPane.YES_NO_OPTION);
if(option==0){
System.exit(-1);
}else{
return;
}
}
});
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 500, 375);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton button = new JButton();
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
frame.dispatchEvent(new WindowEvent((Window)frame,WindowEvent.WINDOW_CLOSING));
}
});
button.setText("关闭");
button.setBounds(81, 117, 197, 67);
frame.getContentPane().add(button);
}}