再请大虾们指点一下,窗口不能被关闭的问题。
阿零 2011-03-08 12:32:08 请看代码,菜单栏的exit按钮无效,close按钮无效,窗口的关闭按钮无效,why???
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
class mainwindow extends Frame implements ActionListener
{
TextField t1,t2;
Button b1,b2;
MenuBar mb1;Menu m1;
MenuItem mi;
mainwindow(String s,int a,int b)
{
super(s);
mb1 =new MenuBar();
m1=new Menu("file");
mi=new MenuItem("exit");
t1 =new TextField(10);
t2=new TextField(10);
b1 =new Button("ok");
b2 =new Button("close");
b1.addActionListener(this);
t2.setEditable(false);
setSize(a,b);
setVisible(true);
setBackground(Color.blue);
setLayout(new GridLayout(1,3));
mb1.add(m1);m1.add(mi);
mi.addActionListener(this);
add(t1);add(t2);add(b1);add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setMenuBar(mb1);
addWindowListener(new WindowAdapter()
{
public void WindowClosing(WindowEvent e)
{System.exit(0);}
});
pack();
}
public void actionPerformed(ActionEvent e)
{
if ((e.getSource()==b1) && (t1.getText().equals("pig")))
{t2.setText("猪");}
else if((e.getSource()==b1)&&(t1.getText().equals("dog")))
{t2.setText("狗");}
else if((e.getSource()==b1)&&(t1.getText().equals("cat")))
{t2.setText("猫");}
if(e.getSource()==b2)
{System.exit(0);}
if(e.getSource()==mi)
{System.exit(0);}
}
}
public class three extends Applet
{
mainwindow mywindow;
public void init()
{
mywindow =new mainwindow("mywindow",60,300);
}
}