我创建了一个 java 方法,它从文本文件中提取信息,然后将其添加到 JTable。但是,我想从循环中删除 JTable,因为它循环了一个新表三次。我在执行此操作时遇到问题。
我想从循环中删除的代码显示为:
JTable table = new JTable(myVector,columnNames) {{
setOpaque(false);
setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {{
setOpaque(true);
}});
}};
JScrollPane scrollPane = new JScrollPane(table);
frame.add(new JScrollPane(table) {{
setOpaque(false);
getViewport().setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, this);
super.paintComponent(g);
}
});
这是完整的java方法:
Vector<Vector<String>> myVector = new Vector<Vector<String>>();
FileReader fReader = new FileReader("playerscores.txt");
BufferedReader inFile = new BufferedReader(fReader);
String input;
String[] temp;
while((input=inFile.readLine())!=null) {
Vector<String> v = new Vector<String>();
temp = input.split(",",6);
for(int i=0;i<temp.length;i++){
v.add(temp[i]);
System.out.println(temp[i]+" added");
}
System.out.println("V is "+v);
myVector.add(v);
System.out.println("End of Line");
Vector<String> columnNames = new Vector<String>();
columnNames.addElement("Player Name:");
columnNames.addElement("Score:");
JTable table = new JTable(myVector,columnNames) {{
setOpaque(false);
setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {{
setOpaque(true);
}});
}};
JScrollPane scrollPane = new JScrollPane(table);
frame.add(new JScrollPane(table) {{
setOpaque(false);
getViewport().setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, this);
super.paintComponent(g);
}
});
}