【发布时间】:2014-04-09 01:03:29
【问题描述】:
我正在尝试根据JList 中选择的值将文本字段的文本设置为字符串。
-
JList=list -
LinkedList<WordPair>=wordpair_list -
WordPair包含wordA和wordB
如果有人能向我解释为什么这不起作用,我将永远欠你的债。该程序中显然有更多代码,但stackoverflow 似乎认为我的文本与代码的比例不成比例。如果您个人想要其余代码,如果您愿意接受挑战,我很乐意将其发送给您。
public void showTranslation(){
int i = wordpair_list.indexOf(list.getSelectedValue());
textField.setText(wordpair_list.get(i).getWordB());
}
public Dictionary(Object o){
if (o instanceof String){
String filename = (String) o;
File file = new File(filename);
Scanner sc = null;
try {
sc = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (sc.hasNextLine()){
words.add(new WordPair(sc.nextLine()));
}
}
}
public WordPair(String arg0) {
arg0.trim();
int equalsIndex = arg0.indexOf("=");
this.wordA = arg0.substring(0, equalsIndex-1);
this.wordB = arg0.substring(equalsIndex+1);
}
【问题讨论】:
-
发布您的异常、完整的调用跟踪以及它指向的部分代码。
-
似乎
Map<String, String>比List<WordPair>更适合您的目的。 -
'code' public Dictionary(Object o){ if (o instanceof String){ String filename = (String) o;文件文件 = 新文件(文件名);扫描仪 sc = null;尝试 { sc = new Scanner(file); } catch (FileNotFoundException e) { e.printStackTrace(); } while (sc.hasNextLine()){ words.add(new WordPair(sc.nextLine())); } } } 和... public WordPair(String arg0){ arg0.trim(); int equalsIndex = arg0.indexOf("="); this.wordA = arg0.substring(0, equalsIndex-1); this.wordB = arg0.substring(equalsIndex+1); }
-
我似乎无法让它以代码格式发布代码,抱歉
标签: java swing indexoutofboundsexception