再请大虾们指点一下,窗口不能被关闭的问题。

阿零 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);

}

}
...全文
78 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿零 2011-03-08
  • 打赏
  • 举报
回复
补充,html代码为:

<html>
<head>
</head>
<body bgcolor="000000">
<center>
<applet
code = "three.class"
width = "500"
height = "300"
>
</applet>
</center>
</body>
</html>
joknm 2011-03-08
  • 打赏
  • 举报
回复
EXT的关闭要自己来实现的。不实现的话是没有做任何事情的。
xiaoheixiadao 2011-03-08
  • 打赏
  • 举报
回复
重写方法名字错了..........windowClosing
或者是加入代码
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
VilenZYP 2011-03-08
  • 打赏
  • 举报
回复
加入代码
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
tpstu 2011-03-08
  • 打赏
  • 举报
回复
addWindowListener(new WindowAdapter()
{
public void WindowClosing(WindowEvent e)
{System.exit(0);}
});

把上面的WindowClosing改成windowClosing窗口关闭按钮就有效了!exit和close按钮我运行都是有效的,我用的是eclipse
colachens 2011-03-08
  • 打赏
  • 举报
回复
貌似就是想 关掉浏览器窗口
awusoft 2011-03-08
  • 打赏
  • 举报
回复
你想关掉浏览器窗口????

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧