我正在尝试创建一个汽车 GUI 项目,如果我单击列表中的汽车名称,它将在附近的文本框中填充汽车信息。我尝试使用 ListValueChanges 方法,但没有奏效。
package vehicle;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class VehiclesGUI extends javax.swing.JFrame implements ListSelectionListener{
public VehiclesGUI() {
initComponents();
Vehicle[] vehicle = new Vehicle[4];
vehicle[0] = new Altima();
vehicle[1] = new Model_S();
vehicle[2] = new Taurus();
vehicle[3] = new Volt();
vehicleList.setListData(vehicle);
vehicleList.setCellRenderer(new VehicleListCellRenderer());
}
private void vehicleListValueChanged(javax.swing.event.ListSelectionEvent evt) {
Vehicle vehicle = (Vehicle)vehicleList.getSelectedValue();
txtDisplay.setText(vehicle.getName());
vehicleList.addListSelectionListener( this );
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new VehiclesGUI().setVisible(true);
}
编辑: 我实现了 ListSelectionListener 和 assListSelectionListener 但文本框仍然显示 txtDisplay