import java.awt.*;
import java.awt.event.ActionListener;
import java.util.EventListener;
import java.awt.ActiveEvent;
public class TestFrame implements ActionListener {

/**
 * Method main
 *
 *
 * @param args
 *
 */
 Frame f = new Frame("www.it315.org");
 
public static void main(String[] args) {
// TODO: Add your code here
Button but = new Button("退出");
TestFrame tf = new TestFrame();
tf.f.setSize(300,300);

tf.f.add(but);
but.addActionListener(this);
tf.f.setVisible(true);
//tf.f.addWindowListener(new MyWindowListener());
} /**
 * Method actionPerformed
 *
 *
 * @param e
 *
 */
public void actionPerformed(ActionEvent e) {
// TODO: Add your code here
f.dispose();
System.exit(0);
}
}
为什么通不过,我觉的不应该有什么问题。请各位帮忙。?

解决方案 »

  1.   

    but.addActionListener(this); 
    此处this 是一个 non-static variable
      

  2.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TestFrame extends JFrame implements ActionListener 
    {
     TestFrame()
     {
     this.setTitle("iori");
      Button but = new Button("退出");
      
      this.setSize(300,300);
      add(but);
      but.addActionListener(this);
      this.setVisible(true);
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     this.show();
     }
    public static void main(String[] args) 
     {    
    TestFrame f=new TestFrame();
         
     }
    public void actionPerformed(ActionEvent e) {this.dispose();
    System.exit(0);
    }
    }
      

  3.   

    我不懂,为什么系统会报this是non-static variable,
    这是在main()方法中引起的问题吗?
      

  4.   

    static的函数里面只能有static的变量
    这个是程序执行时间的问题
      

  5.   

    main方法是static的,里边得不到this引用
      

  6.   

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class TestFrame extends Frame implements ActionListener {
    public static void main(String[] args) 
    {
    new TestFrame();
    }
    public TestFrame(){
    Button but=new Button("退出");
    Frame f=new Frame("www.it315.org");
    f.add(but);
    but.addActionListener(this);
    f.setSize(300,300);
    f.setVisible(true);
    }public void actionPerformed(ActionEvent e) {
    System.exit(0);
    }
    }
      

  7.   

    静态方法里面不可以使用this..
      

  8.   

    《JAVA就业培训教程》里的程序?哈哈,是本好书!