【发布时间】:2016-01-20 22:07:20
【问题描述】:
我已经有了这个项目,但我遇到了更多问题。 SetMusicDialog 的对话框打开,但当我尝试退出时它不会关闭。我有一个 System.exit,但我不确定为什么窗口不会关闭。
import java.awt.*;
import java.io.*;
public class SetMusicDialog extends Dialog
{
public static String sng;
public SetMusicDialog()
{
super ((Dialog)null, "Set Music");
Panel mpanel;
Font l = new Font("Helvetica", Font.ITALIC, 12);
setFont(l);//sets font
setBackground(Color.cyan);
Panel f = new Panel();
f.add("West", new Button("Death Grips"));
f.add("East", new Button("Siren"));
add("South",f);
pack(); // make it just fit
resize(preferredSize());
move(200,200);
}
public boolean handleEvent1 (Event evt)
{
switch (evt.id)
{
case Event.WINDOW_DESTROY:
System.exit(0);
dispose();
return true;
case Event.ACTION_EVENT:
if("Death Grips".equals(evt.arg))
{
sng= "breakmirrors.wav";
}
else if("Siren".equals(evt.arg))
{
sng= "bip.wav";
}
dispose();
}
return false;
}
}
【问题讨论】:
-
使用 AWT,您需要使用
WindowListener,请参阅 How to Write Window Listeners。但是,如果可以的话,我强烈建议不要使用 AWT,而是使用 Swing,它为JFrame和JDialog提供了defaultCloseOperation属性 -
我会试试的,我以前没有使用 WindowListener 就做到了,但我似乎无法让它再次工作。