public class LabelText {
class myWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
public static void main(String args[]){
Frame frame = new Frame("Buttontest");
frame.setLayout(new FlowLayout());
Button b1 =new Button("Left");
Button b2 =new Button("Center");
Button b3 =new Button("Right");
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.addWindowListener(new myWindowAdapter());
frame.setSize(200,200);
frame.setVisible(true);
}
}
class myWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
public static void main(String args[]){
Frame frame = new Frame("Buttontest");
frame.setLayout(new FlowLayout());
Button b1 =new Button("Left");
Button b2 =new Button("Center");
Button b3 =new Button("Right");
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.addWindowListener(new myWindowAdapter());
frame.setSize(200,200);
frame.setVisible(true);
}
}
public static void main(String args[]){
Frame frame = new Frame("Buttontest");
frame.setLayout(new FlowLayout());
Button b1 =new Button("Left");
Button b2 =new Button("Center");
Button b3 =new Button("Right");
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.setSize(200,200);
frame.setVisible(true);
}
}
这样一来就没有问题了,怎么回事,是内部类和匿名类的用法错吗?
你上面那种可以写成 new LabelText.myWindowAdapter()
Frame frame = new Frame("Buttontest");
frame.setLayout(new FlowLayout());
Button b1 = new Button("Left");
Button b2 = new Button("Center");
Button b3 = new Button("Right");
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.addWindowListener(new MyWindowAdapter());
frame.setSize(200, 200);
frame.setVisible(true);
}
}class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}这样也可以!
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class LabelText {
class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public MyWindowAdapter getMyWindowAdapter(){
return new MyWindowAdapter();
} public static void main(String args[]) {
Frame frame = new Frame("Buttontest");
frame.setLayout(new FlowLayout());
Button b1 = new Button("Left");
Button b2 = new Button("Center");
Button b3 = new Button("Right");
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.addWindowListener(new LabelText().getMyWindowAdapter());
frame.setSize(200, 200);
frame.setVisible(true);
}
}
class myWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
public static void main(String args[]){
Frame frame = new Frame("Buttontest");
frame.setLayout(new FlowLayout());
Button b1 =new Button("Left");
Button b2 =new Button("Center");
Button b3 =new Button("Right");
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.addWindowListener(new LabelText().new myWindowAdapter());
frame.setSize(200,200);
frame.setVisible(true);
}
}
public class LabelText {
class myWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
这样就对了,其实和主函数里面调用本身的一个方法是一样的,你要是调用本身一个非静态的方法就必须要new 一下本身所在的类,如果在普通的类里面非晶态的方法你的就是对的。
public class LabelText {
class myWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
public static void main(String args[]){
Frame frame = new Frame("Buttontest");
frame.setLayout(new FlowLayout());
Button b1 =new Button("Left");
Button b2 =new Button("Center");
Button b3 =new Button("Right");
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.addWindowListener(new LabelText().new myWindowAdapter());
frame.setSize(200,200);
frame.setVisible(true);
}
}
我试试昂,等一下下
public class LabelText {
class myWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
public static void main(String args[]){
Frame frame = new Frame("Buttontest");
frame.setLayout(new FlowLayout());
Button b1 =new Button("Left");
Button b2 =new Button("Center");
Button b3 =new Button("Right");
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.addWindowListener(new LabelText().new myWindowAdapter()); //就是这里我刚才也这么写了,不过写错了我是这样写的(new LabelText().myWindowAdapter())我门的不一样我去有点迷惑了,指点下
frame.setSize(200,200);
frame.setVisible(true);
}
}
这样写肯定不对的啊。。哪有对象实例还来调用类名的啊。。
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
一般把这句加到构造函数里来处理窗口关闭事件的
(new LabelText().new myWindowAdapter()); 这为什么就对了呢?
因为myWindowAdapter是一个非静态的类使用的时候当然要实例化才能用啊,new LabelText().new myWindowAdapter()表示引用LabelText类里面的myWindowAdapter类,因为两个都是类所以两个都要实利化,除非你在LabelText里面已经实利化了myWindowAdapter不然不能你那么用!
上下文的