童真模式启动 2017-06-02 14:29 采纳率: 0%
浏览 924
已采纳

JAVA中,下面一段关于“通过流完成数据文件读取与显示”的代码,为什么不能正常编译呢?

 import java.awt.*;
import java.awt.event.*;
import java.awt.Color.*;
import java.io.*;

class File extends Frame implements FocusListener,ActionListener
{
    TextArea ta=new TextArea("",10,30);
    TextField tf=new TextField(30);
    Button b=new Button("读取数据");


    File()
    {

        this.add(ta,"North");
        this.add(tf,"Center");
        this.add(b,"South");

        tf.addFocusListener(this);
        tf.setText("在此输入文件路径…");
        tf.setForeground(Color.gray);

        b.addActionListener(this);

        this.pack();
        this.setVisible(true);
    }

    public void focusGained(FocusEvent e)
    {
        if(e.getSource()==tf)
            {
                tf.setText("");
                tf.setForeground(Color.black);
            }

    }
    public void focusLost(FocusEvent e){}


    File f;
    FileInputStream fi;
    BufferedReader br;
    public void actionPerformed(ActionEvent e)
    {
        f=new File(tf.getText());
        fi=new FileInputStream(f);
        br=new BufferedReader(new InputStreamReader(fi));
        String strText=null;

        while((strText=br.readLine())!=null)
        {
            strText=strText+"\n";
        }

        ta.setText(strText);
    }


    public static void main(String[] args)
    {
        new File();
    }
}
  • 写回答

3条回答 默认 最新

  • warmcore 2017-06-03 08:42
    关注

    把你的类名改成FileFrame或者其它非File的类名,并修改构造函数名就好了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致