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

我写了一个程序求救哪了出错了
package 第十九章.文字编辑器;
import java.awt.*;
import java.awt.event.*;
public class TextField {
  public static void main(String[] args) {
  new Text();
  }
  
}
class Text extends Frame
{  
  TextField tf;
  public Text(){
  tf=new TextField();
  add(tf);//在这有错
  tf.addActionListener(new Monitor());//这儿有错
  tf.setEchoChar('*');//这儿也有
  pack();
  setVisible(true);

  }
   

}
class Monitor implements ActionListener
{

  public void actionPerformed(ActionEvent e) {
  TextField tf=(TextField)e.getSource();
  System.out.println(tf.getText());//这儿有
  tf.setText("");//这儿也有
  }




}
到底怎么回事啊我实在不明白自己哪儿说了求救

------解决方案--------------------
add(tf);//在这有错 参数 要求一个 Component 而你传入的TextField 不是这个类型,更不是这个类型的子类,如果你是想用java.awt.TextField这个类的话,因为你的主类名也叫这个,所以请写全名。
其他地方 也是相似的错误 


System.out.println(tf.getText());// 这儿有
tf.setText("");// 这儿也有
这两个地方 如果你是用你自己定义的那个类,那这两个方法没定义,如果是要用我上边提到的那个类。那跟上边一个错误
------解决方案--------------------
import java.awt.*;
import java.awt.event.*;

public class sunknow1 extends Frame implements ActionListener {
public static void main(String[] args) {
new sunknow1();
}
Label prompt, output;
TextField input;
sunknow1(){
setBounds(300,300,400,300);
prompt = new Label("请输入一个年份:");
output = new Label(" ");
input = new TextField(6);
setLayout(new FlowLayout());
add(prompt);
add(input);
add(output);
input.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
});
setVisible(true);
}
public void actionPerformed(ActionEvent e){
int getInt = Integer.parseInt(input.getText());
if(getInt%4==0 && getInt%100 !=0 || getInt%400 ==0) {
output.setText(" " + getInt + "为闰年!");
}
else{
output.setText(" " + getInt + "不是闰年!");
}
}
}
参考下哈/
------解决方案--------------------
其实是楼主名的名字与JDK自带类冲突:
package event;

public class TextField11 {
public static void main(String[] args) {
new Text();
}
}


package event;

import java.awt.Frame;
import java.awt.TextField;

public class Text extends Frame {
private TextField tf;

public Text() {
tf = new TextField();
add(tf);
tf.addActionListener(new Monitor());
tf.setEchoChar('*');
pack();
setVisible(true);

}

}


package event;

import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Monitor implements ActionListener {

public Monitor() {

}

@Override
public void actionPerformed(ActionEvent e) {
TextField tf = (TextField) e.getSource();
System.out.println(tf.getText());
tf.setText("");
}

}
用我上面的,能跑通。。测试了