我做了个示例程序,你可作为参考: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);
}
}
}
}
}
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);
}
}
}
}
}
if(response==JOptionPane.YES_OPTION)
{
System.exit(0);
}
而在他的父窗体中
void this_windowClosing(WindowEvent e) {
int response;
response=JOptionPane.showConfirmDialog(null,"真的要退出吗?");
if(response==JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
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);
}
}
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);
}
}
}
他的程序可以改进是部分代码多余:
public void windowClosed(WindowEvent e)
{
System.exit(0);
}