我做了个示例程序,你可作为参考: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);
}
}
}
}
}
解决方案 »
- 如何在jtable 绑定 JTextfield 实现动态同步更新
- 正则表达式\Z 与\z两个翻译理解是不是一样?怎么理解它?
- 欢迎来测试 System.out.println(????);
- Tomcat 报错,高手帮忙?
- java中equals equals()与==的区别?以及抽象类和接口在实际工作中的区别?
- 如何在Java中格式化货币??
- 关于空指针异常··求解决·在线等!
- 集思广益,力求完美实现"围棋"程序,1000分做项目基金,参与讨论者也有分
- 如何编译applet
- 在JB5里做实体BEAN,SQLSERVER2000的JDBC也已经填加上了,一路都很正常,但做客户端测试的时候,提示说不能在数据源中加载驱动程序,大家帮忙看看,是什么原因?
- 怎么在JAVA类中调用IE打开PDF文件?
- 简单问题,哪位朋友能帮看一些错误出在哪里?
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);
}