【发布时间】:2011-12-17 04:09:14
【问题描述】:
我有一个大学项目,我必须从文本文件中读取每一行的第一个单词,如下所示:
23123123213 Samuel classA
23423423423 Gina classC
23423423423 John classD
文本文件将通过 3 JTextField 进行更新,我可以弄清楚。
但现在我必须用所有行的第一个单词(23123123213,23423423423 和 23423423423)填充JCombobox。
我是 java 新手,我什至不知道该怎么做。 我知道如何读写文本文件。
请有人帮我做这件事吗?
目前我想出的代码如下:
import java.io.*;
public class FileRead
{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("RokFile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
String[] delims = strLine.split(" ");
String first = delims[0];
System.out.println("First word: "+first);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
在你们的帮助下,我成功地从每一行中提取了第一个字符串 但是现在我如何在 Jcombobox 中填充它,我的意思是我应该先将它保存在某个地方吗?
提前致谢
【问题讨论】:
-
填充 JComboBox:stackoverflow.com/questions/1291704/…
-
首先,欢迎来到 Stackoverflow。其次,如果您有两个问题,例如“我如何获得一行的第一个标记”和“我如何填充 JComboBox”,请考虑让它们成为两个不同的问题,而不是将它们打包在一起。这将使将来搜索有任何问题的人更容易。