这句话怎么错了,大家帮忙看看吧

dragonwan1113 2008-10-09 12:37:49
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("");


}

}
}
...全文
164 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
多多去看一下类成员呀,变量的访问规则先吧..
liujiaqwer 2008-10-11
  • 打赏
  • 举报
回复
沙发正解
nine_suns99 2008-10-11
  • 打赏
  • 举报
回复
改了两行

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

Chatclient chatclient = new Chatclient();
chatclient.inframe();
tf.addActionListener(chatclient.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("");

}

}
}
CandyOfJAVA 2008-10-10
  • 打赏
  • 举报
回复
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();


}
public void inframe() {
setLocation(400,400);
setSize(400,500);
tf.addActionListener(new Tflistener());//错误就是3楼所说的,你只要把监听添到inframe方法中来就行了
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("");


}

}
}
qinchuan32 2008-10-10
  • 打赏
  • 举报
回复
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("");

}

}
dragonwan1113 2008-10-09
  • 打赏
  • 举报
回复
tf.addActionListener(new Tflistener());放到inframe() 方法里去


为何啊
ZangXT 2008-10-09
  • 打赏
  • 举报
回复
tf.addActionListener(new Tflistener());放到inframe() 方法里去

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧