我在windowClosing事件里加了一个 消息框,点击窗体右上X按钮时,出现消息框,当用户按YES时退出程序,按NO时不退出,请问如何写?主要我不会写不退出的时候 怎么办?
addWindowListener(new WindowAdapter() {
public void windowClosing(final WindowEvent arg0) {
int re = JOptionPane.showConfirmDialog(null, "您确定要退出学生学籍管理系统?",
"确认退出", JOptionPane.YES_NO_OPTION);
if (re == JOptionPane.YES_OPTION) {
System.exit(0);
} }
});
addWindowListener(new WindowAdapter() {
public void windowClosing(final WindowEvent arg0) {
int re = JOptionPane.showConfirmDialog(null, "您确定要退出学生学籍管理系统?",
"确认退出", JOptionPane.YES_NO_OPTION);
if (re == JOptionPane.YES_OPTION) {
System.exit(0);
} }
});
public void windowClosing(final WindowEvent arg0) {
int re = JOptionPane.showConfirmDialog(null, "您确定要退出学生学籍管理系统?",
"确认退出", JOptionPane.YES_NO_OPTION,Yes_Option);
好象记得是这样的,做太久了,都忘记了。..
int returnValue;
returnValue = JOptionPane.showConfirmDialog(null, "确实要退出程序吗?", "退出程序", JOptionPane.YES_NO_OPTION);
if (returnValue == JOptionPane.YES_OPTION)
System.exit(0);
}
想想应该是这样>>>>package demokeymouse;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class frameDemo {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
// "javax.swing.plaf.metal.MetalLookAndFeel");
// "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
// "com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
}
catch(Exception e) {
e.printStackTrace();
}
FrameDemoFrame frame = new
FrameDemoFrame();
frame.setTitle("FrameDemo - 框架功能演示");
frame.setIconImage(new ImageIcon("mainbtn.jpg").getImage());
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new FrameDemoFrame_WindowListener_Closing(frame));
frame.addMenu();
frame.addContent();
frame.pack();
frame.setVisible(true); }
}class FrameDemoFrame extends JFrame {
public void windowClosing(WindowEvent e) {
int returnValue;
returnValue = JOptionPane.showConfirmDialog(null, "确实要退出程序吗?", "退出程序", JOptionPane.YES_NO_OPTION);
if (returnValue == JOptionPane.YES_OPTION)
System.exit(0);
}
public void addMenu() {
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar); JMenu menu;
JMenuItem menuItem;
menu = new JMenu("文件");
menuBar.add(menu);
menuItem = new JMenuItem("新建");
menu.add(menuItem);
menuItem = new JMenuItem("打开");
menu.add(menuItem);
menuItem = new JMenuItem("关闭");
menu.add(menuItem);
menu.addSeparator();
menuItem = new JMenuItem("退出");
menu.add(menuItem);
menu = new JMenu("帮助");
menuBar.add(menu);
menuItem = new JMenuItem("帮助主题");
menu.add(menuItem);
menu.addSeparator();
menuItem = new JMenuItem("关于...");
menu.add(menuItem);
}
public void addContent() {
JPanel panel1 = new JPanel();
panel1.add(new JLabel("请点击右侧的按钮:"), BorderLayout.SOUTH);
panel1.add(new JButton("显示对话框"), BorderLayout.SOUTH);
getContentPane().add(panel1);
}}class FrameDemoFrame_WindowListener_Closing extends WindowAdapter {
FrameDemoFrame adaptee;
FrameDemoFrame_WindowListener_Closing(FrameDemoFrame adaptee) {
this.adaptee = adaptee;
}
public void windowClosing(WindowEvent e) {
adaptee.windowClosing(e);
}
}
小代码,给你参考下...
不过还要加上
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
这样点no就不会关闭了
增加这行代码,怎么不行了?
这个不是设置透明的吗?怎么不可以啊?