gpt4 book ai didi

java - 如何使用扫描仪将文本文件读取到 Swing 控制组件中?

转载 作者:行者123 更新时间:2023-12-02 12:39:05 24 4
gpt4 key购买 nike

在数组类中,我有以下代码,并希望使用要加载到 jList 中的姓名和分数变量。我该怎么做?

private static User uArr [] = new User [10];
private static int size = 0;
public UserArray () throws FileNotFoundException {

try
{
Scanner fcFile = new Scanner (new File ("user.txt"));
String line, name, surname;
int score;

while (fcFile.hasNext())
{
line = fcFile.nextLine();
Scanner cfFile = new Scanner (line);
name = cfFile.next();
surname = cfFile.next();
score = cfFile.nextInt();
cfFile.close();
uArr[size] = new User (name,surname,score);
size++;
}

fcFile.close();

}
catch (FileNotFoundException f)
{
System.out.println("File Not Found - Check File Name And Path Again.");
}

最佳答案

would like to use the name, surname and score variables to be loaded into a jList

JList用于存储单条数据。由于您想要显示包含 3 条数据的对象,因此您应该使用 JTable。阅读 Swing 教程中关于 How to Use Tables 的部分了解更多信息和示例以帮助您入门。

使用 JTable 时,您可以使用 DefaultTableModel 并将每条数据单独存储到 TablemModel 中,而无需使用 User 类。

或者,如果您想将 User 类添加到 JTable 中,那么您将需要创建一个自定义 TableModel。查看Row Table Model有关为对象创建自定义模型的分步示例。

关于java - 如何使用扫描仪将文本文件读取到 Swing 控制组件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45011262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com