0

我的代码遇到了一个我以前没有遇到过的问题,而且似乎无法修复。基本上,我正在尝试创建一个 GUI 应用程序,它允许用户搜索名称(例如“Ryan”)并将搜索 CSV 文件,将结果显示到 JScrollPane(JList)。下面是我到目前为止的代码,如图所示,它通过重复 Vector 的最后一个元素 n 次来填充列表(这是错误的,Vector 中的每个元素都应该打印一次)...

主要代码:

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

public class SeatingChart extends JPanel implements MouseListener {

static JLabel search = new JLabel("Search for: ");
static JTextField se = new JTextField("Cap 1st Letter Of Name", 15);
static JButton submit = new JButton("submit");

Vector<Employee> employees = new Vector<Employee>(5, 1); //Works fine with Vector< String >
JList results = new JList(employees); 
JScrollPane display = new JScrollPane(results);

public SeatingChart()
{
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());

    //Add things to Panels
    JPanel north = new JPanel();
    north.add(search);
    north.add(se);
    north.add(submit);

    JPanel west = new JPanel();
    west.add(display);

    //Add panels to Frame
    panel.add(north, BorderLayout.NORTH);
    panel.add(west, BorderLayout.WEST);

    add(panel);

    // Register listeners
    submit.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        search(se.getText()); //call search and sort method
        results.setListData(employees);
      }});
    results.addMouseListener(this);
    addMouseListener(this);

} //end constructor

public void search(String s)
{
    try{
        BufferedReader CSVFile = new BufferedReader(new FileReader("ADexport.csv"));
        String dataRow = CSVFile.readLine(); // Read first line.  

        // The while checks to see if the file is empty 
        while (dataRow != null) 
        {
            if(dataRow.contains(s)) { //Line by line
                String[] dataArray = dataRow.split(",");
                employees.addElement(new Employee(dataArray[0], dataArray[1], dataArray[2], dataArray[3])); //Add .toString() if Vector< String >
            } //end if
            dataRow = CSVFile.readLine(); /*Read next line*/
        }

        CSVFile.close();
    }
    catch (Exception e)
    {
        System.out.println("Unexpected error.");
        System.out.println("Error: " + e);
    }
} //end search method

public void mouseClicked(MouseEvent arg0) {
    int index = results.getSelectedIndex();
    Employee selected = (Employee) results.getSelectedValue();

    System.out.println("Clicked! " + index);
}

员工等级:

import java.util.Vector;

public class Employee {
public static String name;
public static String type;
public static String pos;
public static String ex;

public Employee(String n, String t, String p, String e)
{
    name = n;
    type = t;
    pos = p;
    ex = e;
}

public String toString()
{
    return this.name + " " + this.ex;
}

public String getName()
{
    return this.name;
}

public String getex()
{
    return this.ex;
}
}

这是我一直在使用的 CSV 文件中的信息:

Name    Type    Description Office
Ryan Cole   User    Manager Ext. 115
Ryan Ketricks   User    Staff   Ext. 116
Ryan James  User    Senior  Ext. 117
Luke Alexander  User    Staff   Ext. 118
Michael Kale    User    Senior  Ext. 119
Alyssa Win  User    Staff   Ext. 120
Kayla Bofort    User    Partner Ext. 121

问题是,当我使用 Vector < String > 时,一切正常,但我无法从 String 转换为 Employee。无论如何,我想保留 Employee 类型,因为我想在触发 MouseEvent 时再次使用该信息。本质上,我想知道为什么它适用于 String 而不是 Employee,以及如何解决。

4

0 回答 0