【发布时间】:2014-07-27 05:44:15
【问题描述】:
我还在玩 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);
}
}
任何想法我哪里出错了?
【问题讨论】:
-
您确定
list包含任何内容吗?另外,panel使用的是什么布局管理器? -
我们又见面了 Mad:P 我会在上面发布整个课程。这很尴尬,我现在很尴尬,所以不要太用力了。
-
@Splunk 你在哪里将列表添加到组合框??!!
-
好吧,您还可以填充列表在它被添加到组合框之后。
-
@Splunk 您将元素复制到数组中,这是在读取文件之前完成的。在
ReportGUI构造函数中调用comboBoxes()之前调用fileRead()。