【发布时间】:2012-04-08 02:29:27
【问题描述】:
我正在开发一个从 csv 文件读取的项目,然后使用 StringTokenizer 将元素切碎并将它们放入 JLabels 中。到目前为止,我已经完成了那部分。我有按钮可以滚动浏览每个位置,但我不确定如何在字段中键入并添加到数组中?
到目前为止,这是我程序的主要部分
// program reads in csvfile.
private void loadCarList() {
try{
BufferedReader CSVFile = new BufferedReader(new FileReader("car.txt"));
String dataRow = CSVFile.readLine();
while(dataRow != null){
carList.add(dataRow);
dataRow = CSVFile.readLine();
}
}catch(Exception e){
System.out.println("Exception while reading csv file: " + e);
}
}
}
//this will click cycle through the elements in the Jlabels.
private void loadNextElement(){
try {
StringTokenizer st = new StringTokenizer((String)carList.get(position), ",");
while(st.hasMoreTokens() && position <= carList.size() -1) {
position ++;
String CarID = st.nextToken();
String DealerShipID = st.nextToken();
String ColorID = st.nextToken();
String Year = st.nextToken();
String Price = st.nextToken();
String Quantity = st.nextToken();
tCarID.setText(CarID);
tDealerShip.setText(DealerShipID);
tColor.setText(ColorID);
tYear.setText(Year);
tPrice.setText(Price);
tQuantity.setText(Quantity);
}
} catch(Exception e){
JOptionPane.showMessageDialog(null, "youve reached the end of the list");
}
}
有没有更简单的方法,我可以输入我已经布局的 jlabels 并添加到数组中?
在这一点上我有点迷茫,不知道如何走得更远。
【问题讨论】:
-
风格注释:不要将非类的名称大写。它让读者感到困惑(以及语法高亮)。
-
对不起,我不知道你在问什么。列表到底在哪里,你想添加什么?