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);
}
}

解决方案 »

  1.   

    但是只要把上面的代码该成public class LabelText {
    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);
    }
    }
    这样一来就没有问题了,怎么回事,是内部类和匿名类的用法错吗?
      

  2.   

    内部类不是这么调用的
    你上面那种可以写成 new LabelText.myWindowAdapter()
      

  3.   

    public class LabelText { 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);
    }
    }这样也可以!
      

  4.   

    如果用到内部类,好像要这样才行:
    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);
    }
    }
      

  5.   

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

    }
    这样就对了,其实和主函数里面调用本身的一个方法是一样的,你要是调用本身一个非静态的方法就必须要new 一下本身所在的类,如果在普通的类里面非晶态的方法你的就是对的。
      

  6.   

    多粘了一截不好意思
    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); 

    }
      

  7.   

    也就是说我的那段代码只要在main方法里new一个LabelText的对象调用内部类也是对的吗?
    我试试昂,等一下下
      

  8.   

    12楼的朋友谢谢了,你的正确了,呵呵
    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); 

    }
      

  9.   

    我想你是没有看错误提示,上面还是给了提示的,我是用eclipse。因为myWindowAdapter()根本不是一个方法当然不能直接调用了,myWindowAdapter是一个类,你要用的是这个类的实例他也不是静态的类所以不管你在什么地方用都是要new一下的
      

  10.   

    我想你是没有看错误提示,上面还是给了提示的,我是用eclipse。因为myWindowAdapter()根本不是一个方法当然不能直接调用了,myWindowAdapter是一个类,你要用的是这个类的实例他也不是静态的类所以不管你在什么地方用都是要new一下的
      

  11.   

    我想你是没有看错误提示,上面还是给了提示的,我是用eclipse。因为myWindowAdapter()根本不是一个方法当然不能直接调用了,myWindowAdapter是一个类,你要用的是这个类的实例他也不是静态的类所以不管你在什么地方用都是要new一下的
      

  12.   

    new LabelText().myWindowAdapter())
    这样写肯定不对的啊。。哪有对象实例还来调用类名的啊。。
    this.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    dispose();
    }
    });
    一般把这句加到构造函数里来处理窗口关闭事件的
      

  13.   

    那么
    (new LabelText().new myWindowAdapter()); 这为什么就对了呢?
      

  14.   

    晕倒!我的回复都被csdn屏蔽了,说我恶意刷屏!
    因为myWindowAdapter是一个非静态的类使用的时候当然要实例化才能用啊,new LabelText().new myWindowAdapter()表示引用LabelText类里面的myWindowAdapter类,因为两个都是类所以两个都要实利化,除非你在LabelText里面已经实利化了myWindowAdapter不然不能你那么用!
      

  15.   

    哦,谢谢了,对AWT只是初步的了解现在才开始学习以后多多指教昂
      

  16.   

    public static void main(String args[]){ }static 在 静态方法 里面 是不能调用 非静态
    上下文的