我收到第53行的警告: list = new JList(alCar.toArray());
警告说: 分配给UI绑定字段将覆盖UI Designer生成的字段。 此检查报告对绑定到UI Designer表单中的组件的字段的分配。此类分配将导致UI Designer为这些字段生成的组件设置代码被忽略。
package com.company;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.io.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class CarApp {
private JButton btnDelObj;
private JButton btnAddObj;
private DefaultListModel dlm;
private JList list;
private JPanel panelMain;
private ArrayList<Car> alCar;
private String sMake, sModel, newYear, fileName;
private int iYear;
public CarApp() throws IOException, ClassNotFoundException {
dlm = new DefaultListModel();
alCar = new ArrayList<>();
list = new JList();
list.setModel(dlm);
//Sorts the ArrayList Car objects by model in alphabetical order.
Collections.sort(alCar, new Comparator<Car>() {
public int compare(Car o1, Car o2) {
if (o1.model.compareToIgnoreCase(o2.model) == 0) {
return 0;
} else if (o1.model.compareToIgnoreCase(o2.model) < 0) {
return -1;
} else {
return 1;
}
}
});
//Loads the file specified by the user
loadFile();
//Adds a JList to the GUI with the ArrayList loaded from binary file
list = new JList(alCar.toArray());
list.setLayout(new FlowLayout());
list.setVisible(true);
list.setVisibleRowCount(10);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.add(new JScrollPane());
//JList selection event
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
list.getSelectedIndex();
}
});
//btnAddObj click event
btnAddObj.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sMake = JOptionPane.showInputDialog("Enter the maker of the car: ");
sModel = JOptionPane.showInputDialog("Enter the model of the car: ");
newYear = JOptionPane.showInputDialog("Enter the year of the car: ");
iYear = Integer.parseInt(newYear);
//Add new Car object to the ArrayList
alCar.add(new Car(sMake, sModel, iYear));
//This serializes a file
//saveFile();
//Notifies the ListDataListeners when changes occur in ArrayList<Car>
for (int i = 0; i < alCar.size(); i++) {
dlm.addElement(alCar.get(i));
}
list.setModel(dlm);
}
});
//btnDelObj Click Event
btnDelObj.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = (dlm.size() - 1); i >= 0; i--) {
dlm.remove(list.getSelectedIndex());
//alCar.remove(list.getSelectedIndex());
}
list.setModel(dlm);
}
});
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
JFrame frame = new JFrame("Cars");
CarApp myApp = new CarApp();
frame.setContentPane(myApp.panelMain);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
//This serializes a file
public void saveFile() {
try {
fileName = JOptionPane.showInputDialog("Enter the name of the file you want to save to: ");
FileOutputStream fileOut = new FileOutputStream(fileName);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(alCar);
out.close();
fileOut.close();
JOptionPane.showMessageDialog(null, "Serialized data is saved in " + fileName);
} catch (IOException i) {
i.printStackTrace();
JOptionPane.showMessageDialog(null, "A problem occurred with serialization." +
"Make sure that the file you're saving to is in the same directory as your project." +
"Hence, a peer to your src folder.");
}
}
//This deserializes a file. I wasn't able to get the sorting to work after deserialization. It was working prior to this.
public void loadFile() {
try {
fileName = JOptionPane.showInputDialog("Enter the file you want to open: ");
FileInputStream fileIn = new FileInputStream(fileName);
ObjectInputStream in = new ObjectInputStream(fileIn);
alCar = (ArrayList<Car>) in.readObject();
for (int index = 0; index < alCar.size(); index++) {
System.out.println(alCar.get(index).toString());
}
in.close();
fileIn.close();
} catch (IOException i) {
JOptionPane.showMessageDialog(null, "A problem occurred during deserialization.");
i.printStackTrace();
return;
} catch (ClassNotFoundException c) {
JOptionPane.showMessageDialog(null, "Car class not found.");
c.printStackTrace();
return;
}
}
}
//This class describes Car objects
class Car implements java.io.Serializable {
private String make;
String model;
private int year;
/**
* Constructor that accepts arguments from the Car object for make, model, and year.
*
* @param make The maker of the car
* @param model The model of the car
* @param year The year of the car
*/
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
//This method describes the state (data that is stored in the object's fields at any moment)
//of the Car object.
@Override
public String toString() {
return "(Make: " + make + ", Model: " + model + ", Year: " + year + ")";
}
}