[求助]图形界面编程,退出对话框问题
想让图象界面点击退出的时候弹出对话框,选择是就退出,否就什么也不做该怎么写.下面是我的代码,该这么加,请帮忙解答下,谢谢! import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TestGUI
{
public static void main(String[] args)
{
frame f = new frame();
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
int i;
i=JOptionPane.showConfirmDialog(null,"are you ok");
if(i==0)
{
System.exit(0);
}
else
{
}
}
});
}
}
class panel extends JPanel
{
public panel()
{
makeButton("red",Color.RED);
makeButton("yellow",Color.yellow);
}
private void makeButton(String name,final Color c)
{
Button button=new Button(name);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setBackground(c);
}
}
);
add(button);
}
}
class frame extends JFrame
{
public frame()
{
setTitle("test");
setSize(300,200);
panel p= new panel();
add(p);
}
}