接着弹出一个窗口,但是,我点这个窗口上的关闭按钮,关闭不了
这是为什么?
是不是需要添加什么其他的代码啊?
请前辈们指点一下
import java.awt.*;
public class aa extends Frame
{
Menu m1;
Label l1;
public aa(String ss)
{
super(ss);
resize(700,700);
l1=new Label("now");
add(l1);
m1=new Menu("file");
m1.add(new MenuItem("new"));
MenuBar mb=new MenuBar();
mb.add(m1);
setMenuBar(mb);
show();
}
public static void main(String[] args)
{
new aa("菜单栏");
}
}
3 个解决方案
#1
关闭是需要自己去实现的。
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class BorderLayoutEx{
public static void main(String[] args){
Frame f ;
Button b1,b2,b3,b4,b5;
f = new Frame("BorderLayoutEx");
f.setLayout(new BorderLayout());
b1 = new Button("North");
b2 = new Button("South");
b3 = new Button("East");
b4 = new Button("West");
b5 = new Button("Center");
f.add("North",b1);
f.add("South",b2);
f.add("East",b3);
f.add("West",b4);
f.add("Center",b5);
f.setSize(200,200);
f.addWindowListener(new WindowAdapter(){ //使用内部类 用WindowAdapter这个类
public void windowClosing(WindowEvent e){ //使用windowClosing这个方法
System.exit(0);
}
});
f.setVisible(true);
}
}
#2
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class aa extends Frame {
Menu m1;
Label l1;
public aa(String ss)
{
super(ss);
resize(700,700);
l1=new Label("now");
add(l1);
m1=new Menu("file");
m1.add(new MenuItem("new"));
MenuBar mb=new MenuBar();
mb.add(m1);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setMenuBar(mb);
show();
}
public static void main(String[] args)
{
new aa("菜单栏");
}
}
这样就OK了,给Frame加一个窗体监听器
#3
感谢 楼上两位大哥的帮助 谢谢了
#1
关闭是需要自己去实现的。
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class BorderLayoutEx{
public static void main(String[] args){
Frame f ;
Button b1,b2,b3,b4,b5;
f = new Frame("BorderLayoutEx");
f.setLayout(new BorderLayout());
b1 = new Button("North");
b2 = new Button("South");
b3 = new Button("East");
b4 = new Button("West");
b5 = new Button("Center");
f.add("North",b1);
f.add("South",b2);
f.add("East",b3);
f.add("West",b4);
f.add("Center",b5);
f.setSize(200,200);
f.addWindowListener(new WindowAdapter(){ //使用内部类 用WindowAdapter这个类
public void windowClosing(WindowEvent e){ //使用windowClosing这个方法
System.exit(0);
}
});
f.setVisible(true);
}
}
#2
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class aa extends Frame {
Menu m1;
Label l1;
public aa(String ss)
{
super(ss);
resize(700,700);
l1=new Label("now");
add(l1);
m1=new Menu("file");
m1.add(new MenuItem("new"));
MenuBar mb=new MenuBar();
mb.add(m1);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setMenuBar(mb);
show();
}
public static void main(String[] args)
{
new aa("菜单栏");
}
}
这样就OK了,给Frame加一个窗体监听器
#3
感谢 楼上两位大哥的帮助 谢谢了