java 点击X按钮为什么没有 触发Dialog事件
importjava.awt.*;importjava.applet.*;importjava.awt.event.*;publicclassEx7_4extendsFr...
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Ex7_4 extends Frame
{
public Ex7_4()
{
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
this.addWindowListener(new HandleWin());
}
class HandleWin extends WindowAdapter
{
public void WindowClosing(WindowEvent e)
{
Frame f=(Frame)(e.getWindow());
ConfirmDlg mg=new ConfirmDlg(f);
if(mg.ans)
{
f.dispose();//释放窗体所占资源
System.exit(0);
}
}
}
}
//Dialog对话框
class ConfirmDlg implements ActionListener
{
Dialog dlg;
Label message=new Label("是否关闭窗口");
Button btn1=new Button("是");
Button btn2=new Button("否");
Panel p1=new Panel();
Panel p2=new Panel();
boolean ans;
ConfirmDlg(Frame own)
{
btn1.addActionListener(this);
btn2.addActionListener(this);
dlg=new Dialog(own,"确认对话框",true);
p1.add(message);
p2.add(btn1);
p2.add(btn2);
dlg.add(p1,BorderLayout.NORTH);
dlg.add(p2,BorderLayout.SOUTH);
dlg.setSize(200,100);
dlg.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
dlg.dispose();//无论按哪个按钮,先关闭对话框
if(e.getSource()==btn1)
{
ans=true;
}
else
{
ans=false;
}
}
} 展开
import java.applet.*;
import java.awt.event.*;
public class Ex7_4 extends Frame
{
public Ex7_4()
{
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
this.addWindowListener(new HandleWin());
}
class HandleWin extends WindowAdapter
{
public void WindowClosing(WindowEvent e)
{
Frame f=(Frame)(e.getWindow());
ConfirmDlg mg=new ConfirmDlg(f);
if(mg.ans)
{
f.dispose();//释放窗体所占资源
System.exit(0);
}
}
}
}
//Dialog对话框
class ConfirmDlg implements ActionListener
{
Dialog dlg;
Label message=new Label("是否关闭窗口");
Button btn1=new Button("是");
Button btn2=new Button("否");
Panel p1=new Panel();
Panel p2=new Panel();
boolean ans;
ConfirmDlg(Frame own)
{
btn1.addActionListener(this);
btn2.addActionListener(this);
dlg=new Dialog(own,"确认对话框",true);
p1.add(message);
p2.add(btn1);
p2.add(btn2);
dlg.add(p1,BorderLayout.NORTH);
dlg.add(p2,BorderLayout.SOUTH);
dlg.setSize(200,100);
dlg.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
dlg.dispose();//无论按哪个按钮,先关闭对话框
if(e.getSource()==btn1)
{
ans=true;
}
else
{
ans=false;
}
}
} 展开
展开全部
1. 你可以尝试使用JFrame这个对象替换Frame
2. 给Frame添加WindowListener,在用户点击关闭按钮的时候退出程序或者隐藏窗体
Frame frame = new Frame();
frame.setBounds(200, 200, 300, 300);
frame.setVisible(true);
frame.addWindowListener(new WindowListener() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
2. 给Frame添加WindowListener,在用户点击关闭按钮的时候退出程序或者隐藏窗体
Frame frame = new Frame();
frame.setBounds(200, 200, 300, 300);
frame.setVisible(true);
frame.addWindowListener(new WindowListener() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询