gpt4 book ai didi

java - 无法将文件中的数据读取到 JComboBox

转载 作者:行者123 更新时间:2023-12-01 12:25:37 25 4
gpt4 key购买 nike

我正在尝试制作一个用户界面,在其中我将文件从文本文件读取到 JComboBoxes 并显示结果为 JTextFields。只有第一个组合框从文件中更新,而其余部分未从文件中更新。

这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

public class Main1 extends JPanel
{
public Main1() {
JPanel buttonPanel = new JPanel();
add(buttonPanel);
buttonPanel.setLayout(new GridLayout(0, 4, 5, 5));


JTextField field1 = new JTextField(5);
field1.setEditable(false);
buttonPanel.add(field1);
JTextField field2 = new JTextField(5);
field2.setEditable(false);
buttonPanel.add(field2);
JTextField field3 = new JTextField(5);
field3.setEditable(false);
buttonPanel.add(field3);
JTextField field4 = new JTextField(5);
field4.setEditable(false);
buttonPanel.add(field4);

JComboBox comboBox = new JComboBox();
comboBox.setEditable(true);

comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {

JComboBox comboBox = (JComboBox) event.getSource();

field1.setText((String) comboBox.getSelectedItem());

}
});

JComboBox comboBox1 = new JComboBox();
comboBox1.addItem("1");
comboBox1.addItem("2");
comboBox1.addItem("4");


comboBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {

JComboBox comboBox1 = (JComboBox) event.getSource();

Object selected = comboBox1.getSelectedItem();
if(selected.toString().equals("1"))
field2.setText("3");
else if(selected.toString().equals("2"))
field2.setText("6");
else if(selected.toString().equals("4"))
field2.setText("12");


}
});
JComboBox comboBox2 = new JComboBox();
comboBox2.addItem("1");
comboBox2.addItem("2");
comboBox2.addItem("4");

comboBox2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {

JComboBox comboBox2 = (JComboBox) event.getSource();

Object selected = comboBox2.getSelectedItem();
if(selected.toString().equals("1"))
field3.setText("1");
else if(selected.toString().equals("2"))
field3.setText("2");
else if(selected.toString().equals("4"))
field3.setText("4");


}
});
JComboBox comboBox3 = new JComboBox();
comboBox3.addItem("1");
comboBox3.addItem("2");

comboBox3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {

JComboBox comboBox3 = (JComboBox) event.getSource();

Object selected = comboBox3.getSelectedItem();
if(selected.toString().equals("1"))
field4.setText("1");
else if(selected.toString().equals("2"))
field4.setText("2");
}
});

buttonPanel.add(comboBox);
buttonPanel.add(comboBox1);
buttonPanel.add(comboBox2);
buttonPanel.add(comboBox3);

try{
InputStream ips=new FileInputStream("test.txt");
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String line;
while ((line=br.readLine())!=null) {
String[] s = line.split(" ");
comboBox.setSelectedItem(s[0]);
comboBox1.setSelectedItem(s[1]);
comboBox2.setSelectedItem(s[2]);
comboBox3.setSelectedItem(s[3]);
}
br.close();
}
catch (Exception e){
e.printStackTrace();
}


}
public static void main(String[] args) {
Main1 a = new Main1();
JFrame f = new JFrame();
f.getContentPane().add(a);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}

文本文件:

14 54 89 56

最佳答案

文件列表中的任何值都不包含在组合框列表中,因此除了第一个值之外,不会出现任何值。发生这种情况是因为这是唯一可编辑的。来自 docs

If anObject is not in the list and the combo box is uneditable, it will not change the current selection

只需在应用程序启动时将所有预期项目添加到组合框模型中即可。

关于java - 无法将文件中的数据读取到 JComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26363223/

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