Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将内容归档到jList中_Java_File_Swing_Jlist - Fatal编程技术网

Java 将内容归档到jList中

Java 将内容归档到jList中,java,file,swing,jlist,Java,File,Swing,Jlist,有人能向我解释一下如何将文本文件的内容加载到jList中吗? 我已经设法在控制台上打印了文件的内容。 我已经完成了一个Read()函数,它逐行读取文件的内容,还有一个Load()函数,它应该通过调用函数Read()在jList中显示内容 装载代码: public void Load() { Read(); File archive = null; FileReader fr = null; BufferedReader br = null; try {

有人能向我解释一下如何将文本文件的内容加载到jList中吗? 我已经设法在控制台上打印了文件的内容。 我已经完成了一个Read()函数,它逐行读取文件的内容,还有一个Load()函数,它应该通过调用函数Read()在jList中显示内容

装载代码:

public void Load()
{

    Read();
    File archive = null;
    FileReader fr = null;
    BufferedReader br = null;

    try {
        archive = new File ("Cars.txt");
        fr = new FileReader (archive);
        br = new BufferedReader(fr);
         Vector<String> lines = new Vector<String>();

        String line;
        while((line=br.readLine())!=null) {
            System.out.println(line);
            lines.add(line);
        } 
    } catch(Exception e) {
        e.printStackTrace();
    } finally {
        try{
            if( null != fr ) {
                fr.close();
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
  }
public void Load()
{
Read();
档案档案=null;
FileReader fr=null;
BufferedReader br=null;
试一试{
存档=新文件(“Cars.txt”);
fr=新文件读取器(存档);
br=新的缓冲读取器(fr);
向量线=新向量();
弦线;
而((line=br.readLine())!=null){
系统输出打印项次(行);
行。添加(行);
} 
}捕获(例外e){
e、 printStackTrace();
}最后{
试一试{
如果(null!=fr){
fr.close();
}
}捕获(异常e2){
e2.printStackTrace();
}
}
}
代码如下所示:

public void Read()
{
    String name , type , distance, time;
    File file=new File("Cars.txt");
    FileInputStream fis=null;
    BufferedInputStream bis=null;
    DataInputStream dis=null;

    try {
        fis = new FileInputStream(file);
        bis = new BufferedInputStream(fis);
        dis = new DataInputStream(bis);

        while (dis.available() != 0)
        {
            String x=dis.readLine();
            int k1=x.length();
            char []m=x.toCharArray();
            int y1=0, y2=0, z=1;
            for(int i=1;i<k1;i++)
                if(m[i]==' ' && z==1)
                {
                    y1=i;
                    z++;
                }
                else if(z==2 && m[i]==' ') {y2=i; z++; }

            k1-=y2;
            name=x.copyValueOf(x.toCharArray(), 0, y1);
            type=x.copyValueOf(x.toCharArray(), 0, y2);
            distance=x.copyValueOf(x.toCharArray(), y1+1, y2-y1-1);
            time=x.copyValueOf(x.toCharArray(), y2+1, k1-1);
            int d=Nr(distance);
            int t=Nr(time);
           // System.out.println(name + " " +q + " " + n);

            list.Add(name, type , d, t);
        }

        fis.close();
        bis.close();
        dis.close();

    }
  catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}
}
public void Read()
{
字符串名称、类型、距离、时间;
File File=新文件(“Cars.txt”);
FileInputStream fis=null;
BufferedInputStream bis=null;
DataInputStream dis=null;
试一试{
fis=新文件输入流(文件);
bis=新的缓冲数据流(fis);
dis=新数据输入流(bis);
while(dis.available()!=0)
{
字符串x=dis.readLine();
int k1=x.长度();
char[]m=x.toCharArray();
inty1=0,y2=0,z=1;
对于(int i=1;i创建如下列表:

DefaultListModel model = new DefaultListModel();
JList list = new JList( model );
然后,当您读取每行数据时,您可以使用:

model.addElement(...);
创建如下列表:

DefaultListModel model = new DefaultListModel();
JList list = new JList( model );
然后,当您读取每行数据时,您可以使用:

model.addElement(...);

它不会在jlist中写入任何内容。创建(并发布)您的说明问题的内容。即使用jlist创建框架(在滚动窗格中)和一个JButton。单击该按钮,然后手动将一些项目添加到JList的模型中。一旦完成此操作,您将更改代码以读取文件并将每一行添加到模型中。我通过使用一些oracle java教程以某种方式管理了它。谢谢各位。尽管我已经用文件的内容填充了JList假设每一行都有一个对象,我如何从该文件中读取对象?有人能解释我如何读取吗?它不会在jlist中写入任何内容。创建(并发布)演示问题的内容。即创建一个带有jlist的框架(在滚动窗格中)和一个JButton。单击该按钮,然后手动将一些项目添加到JList的模型中。一旦完成此操作,您将更改代码以读取文件并将每一行添加到模型中。我通过使用一些oracle java教程以某种方式管理了它。谢谢各位。尽管我已经用文件的内容填充了JList假设每一行都有一个对象,我如何从该文件中读取对象?有人能解释我如何读取对象吗?可能的重复