我正在制作一个GUI,其中我有6个组合框,我从文本文件中读取数据到这些 组合框。我的文本文件有3行2列,所以当我读取数据时,我的前2个组合框中的数据填充了文本文件的第3行而不是第1行的值,其余的组合框仍然存在我的文本文件包含6个值,它应该显示在6个组合框中。请帮助
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Read extends JPanel {
public Read() {
JPanel buttonPanel = new JPanel();
add(buttonPanel);
buttonPanel.setLayout(new GridLayout(0, 2, 5, 5));
JComboBox comboBox1 = new JComboBox();
comboBox1.addItem("1");
comboBox1.addItem("2");
comboBox1.addItem("4");
JComboBox comboBox2 = new JComboBox();
comboBox2.addItem("1");
comboBox2.addItem("2");
comboBox2.addItem("4");
JComboBox comboBox3 = new JComboBox();
comboBox3.addItem("1");
comboBox3.addItem("2");
comboBox3.addItem("4");
JComboBox comboBox4 = new JComboBox();
comboBox4.addItem("1");
comboBox4.addItem("2");
comboBox4.addItem("4");
JComboBox comboBox5 = new JComboBox();
comboBox5.addItem("1");
comboBox5.addItem("2");
comboBox5.addItem("4");
JComboBox comboBox6 = new JComboBox();
comboBox6.addItem("1");
comboBox6.addItem("2");
comboBox6.addItem("4");
buttonPanel.add(comboBox1);
buttonPanel.add(comboBox2);
buttonPanel.add(comboBox3);
buttonPanel.add(comboBox4);
buttonPanel.add(comboBox5);
buttonPanel.add(comboBox6);
try{
InputStream ips=new FileInputStream("tl.txt");
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String line;
while ((line=br.readLine())!=null) {
String[] s = line.split(" ");
comboBox1.setSelectedItem(s[0]);
comboBox2.setSelectedItem(s[1]);
}
br.close();
}
catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
Read a = new Read();
JFrame f = new JFrame("");
f.getContentPane().add(a);
f.setSize(300,200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
文本文件
2 4
1 2
4 1
答案 0 :(得分:1)
请查看您的代码,您只将数据填充到组合框1和2,其他组合框中您没有填写任何数据。
如果您想要其他组合框中的数据,请在while循环中填充它们。
希望这有帮助。
感谢
public class Read extends JPanel{
String[] values=new String[6];
JCombobox<String>[] combos=new JCombobox<String>[6];
public Read() {
//Do your layout initialization operations here
this.initCombo();
//Put the logic to add the comboboxes to the UI here
}
public void intiCombo(){
try{
ArrayList<String> tmp=new ArrayList<String>();
InputStream ips=new FileInputStream("tl.txt");
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String line;
while ((line=br.readLine())!=null) {
String[] s = line.split(" ");
tmp.add(s[0]);
tmp.add(s[1]);
}
br.close();
}
catch (Exception e){
e.printStackTrace();
}
values=tmp.toArray(new String[1]);
for(int i=0;i<conbos.length;i++)combos[i]=new JCombobox(values);
}
//Your main method comes here
}
答案 1 :(得分:1)
声明一个JCombobox
数组并将所有JCombobox存储在数组中,如下所示,
JComboBox[] comboBoxs = new JComboBox[6];
comboBoxs[0] = comboBox1;
comboBoxs[1] = comboBox2;
comboBoxs[2] = comboBox3;
comboBoxs[3] = comboBox4;
comboBoxs[4] = comboBox5;
comboBoxs[5] = comboBox6;
声明Arraylist
并存储您从文件中读取的数据,如下所示
// Other code goes here.
ArrayList<String> list = new ArrayList<String>();
String line;
while ((line=br.readLine())!=null) {
String[] s = line.split(" ");
list.add(s[0]);
list.add(s[1]);
}
br.close();
最后循环遍历每个Arraylist
并填充Combobox。
for(int i = 0; i < comboBoxs.length; i++) {
comboBoxs[i].setSelectedItem(list.get(i));
}
这应该做得很好。