gpt4 book ai didi

java - ArrayList 中的 JComboBox - 不工作 - Java Swing

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

我仍在使用 Java 和 swing(对这一切仍然很陌生)。我正在尝试使用 .txt 文件中的数据填充 JComboBox。我将数据拉入 ArrayList 并尝试使用 ArrayList 变量填充 JComboBox。然而,当我运行应用程序时,组合框是空白的。

这是数组代码:

private ArrayList<String> list = new ArrayList<String>();

文件读取器代码:

private void fileRead(){
try{
Scanner scan = new Scanner(new File("Examiner.txt"));
// ArrayList<String> list = new ArrayList<String>();
while(scan.hasNext()){
list.add(scan.next());
}
scan.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}

以及我用于组合框的困惑:

 private void comboBoxes(){                
panel.setBorder(new EmptyBorder(0, 5, 5, 10));
String[] comboBox1Array = list.toArray(new String[list.size()]);
JComboBox comboBox1 = new JComboBox(comboBox1Array);
panel.add(examinerLabel);
panel.add(comboBox1);
panel.add(viewTeachedCourses);
JComboBox comboBox2 = new JComboBox();
panel.add(courseLabel);
panel.add(comboBox2);
panel.add(viewPrograms);
add(panel, BorderLayout.LINE_START);

}

这个类的代码一团乱。

package messing with swing;

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


public class ReportGUI extends JFrame{
//Fields
private JButton viewAllReports = new JButton("View All Program Details");
private JButton viewPrograms = new JButton("View Programs and Majors Associated with this course");
private JButton viewTeachedCourses = new JButton("View Courses this Examiner Teaches");
private JLabel courseLabel = new JLabel("Select a Course: ");
private JLabel examinerLabel = new JLabel("Select an Examiner: ");
private JPanel panel = new JPanel(new GridLayout(6,2,4,4));
private ArrayList<String> list = new ArrayList<String>();
//private String storeAllString="";



public ReportGUI(){
reportInterface();
allReportsBtn();
comboBoxes();
fileRead();
}



private void fileRead(){
try{
Scanner scan = new Scanner(new File("Examiner.txt"));
// ArrayList<String> list = new ArrayList<String>();
while(scan.hasNext()){
list.add(scan.next());
}
scan.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}

private void reportInterface(){
setTitle("Choose Report Specifications");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(new FlowLayout());
add(panel, BorderLayout.CENTER);
setSize(650,200);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
}
private void allReportsBtn(){
JPanel panel = new JPanel(new GridLayout(1,1));
panel.setBorder(new EmptyBorder(70, 50, 70, 25));
panel.add(viewAllReports);
viewAllReports.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
JFrame AllDataGUI = new JFrame();
new AllDataGUI();
}
});
add(panel, BorderLayout.LINE_END);
}
private void comboBoxes(){
panel.setBorder(new EmptyBorder(0, 5, 5, 10));
String[] comboBox1Array = list.toArray(new String[list.size()]);
JComboBox comboBox1 = new JComboBox(comboBox1Array);
panel.add(examinerLabel);
panel.add(comboBox1);
panel.add(viewTeachedCourses);
JComboBox comboBox2 = new JComboBox();
panel.add(courseLabel);
panel.add(comboBox2);
panel.add(viewPrograms);
add(panel, BorderLayout.LINE_START);

}







}

有什么想法我哪里出错了吗?

最佳答案

 public ReportGUI()
{
reportInterface();
allReportsBtn();
comboBoxes();
fileRead();
}

正如 Obicere 所指出的:

  1. 您首先调用comboBoxes(),它创建组合框并使用空列表填充组合框。
  2. 然后调用 fileRead() 方法将数据添加到列表中,但这不会更新组合框的模型。

代码顺序需要颠倒:

fileRead();
comboBoxes();

关于java - ArrayList<String> 中的 JComboBox - 不工作 - Java Swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24073617/

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