日期:2014-05-20  浏览次数:20654 次

这句话怎么错了,大家帮忙看看吧
import java.awt.*;
import java.awt.event.*;
public class Chatclient extends Frame {
  TextField tf = new TextField ();
  TextArea ta = new TextArea ();
   
   
public static void main(String[] args) {


new Chatclient ().inframe();
(提示这里有错误)tf.addActionListener(new Tflistener());
 
}
public void inframe() {
setLocation(400,400);
setSize(400,500);
this.add(tf,BorderLayout.SOUTH);
add(ta,BorderLayout.NORTH);
pack();
addWindowListener (new Mywinl());

setVisible(true);
}
  private class Mywinl extends WindowAdapter {
  public void windowClosing(WindowEvent e){
  System.exit(0);
  }
 
  }
  private class Tflistener implements ActionListener {

public void actionPerformed(ActionEvent e) {
String s = tf.getText();
ta.setText(s);
tf.setText("");


}
 
  }
}

------解决方案--------------------
public static void main(String[] args) {

new Chatclient().inframe();
tf.addActionListener(new Tflistener());

}//main方法为静态方法,tf在这里不为静态变量。静态方法不能对非静态变量进行引用。
此外,class Tflistener应写到class Chatclient外面去。
//
import java.awt.*;
import java.awt.event.*;

public class Chatclient extends Frame {
static TextField tf = new TextField();

TextArea ta = new TextArea();

public static void main(String[] args) {

new Chatclient().inframe();
tf.addActionListener(new Tflistener());

}

public void inframe() {
setLocation(400, 400);
setSize(400, 500);
this.add(tf, BorderLayout.SOUTH);
add(ta, BorderLayout.NORTH);
pack();
addWindowListener(new Mywinl());

setVisible(true);
}

private class Mywinl extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}

}


}
class Tflistener implements ActionListener {
TextField tf = new TextField();
TextArea ta = new TextArea();
public void actionPerformed(ActionEvent e) {
String s = tf.getText();
ta.setText(s);
tf.setText("");

}

}