我已经声明了一个ArrayList和DefaultListModel
DefaultListModel model;
List<AddFlight> Flights = new ArrayList<AddFlight>();
在此列表中,我添加了一个对象作为元素
Flights.add(new AddFlight(txtFlightNo.getText(),
(String)cmbMechanicalStatus.getSelectedItem(),
(String)cmbMedicalStatus.getSelectedItem(),
Float.valueOf((txtFuelLevel.getText()).trim()).floatValue(),
(String)cmbWeatherCondition.getSelectedItem(),
(String)cmbFrequency.getSelectedItem()));
在已执行的操作(ActionEvent e)中,我想在JScrollPane上以可理解的方式在一行中打印每个对象。
model.add(0,Flights);
我的输出如下[当我将 2个对象添加到ArrayList时]:
alt text http://i27.tinypic.com/apgojc.jpg
我想要什么:
1)我希望ArrayList中的每个对象都出现在一行中。
2)我希望它在JScrollPane中显示如下:
航班号:UL209,机械状态:正常,医疗状态:故障,燃油油位:12.0 //第一行
航班号:UL210,机械状态:正常,医疗状态:正常,燃油油位:22.0 //第二行
但是,我设法使用以下代码打印出每个元素:
for (AddFlight Flight : Flights) {
System.out.println("FLight No : " + Flight.getFlightNo());
System.out.println("Mechanical Status : " + Flight.getMechanicalStatus());
System.out.println("Medical Status : " + Flight.getMedicalStatus());
System.out.println("Fuel Level : " + Flight.getFuelLevel());
System.out.println("Weather Condition: " + Flight.getWeatherCondition());
System.out.println("Frequency : " + Flight.getFrequency());
}
我的完整代码
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Flight implements ActionListener {
//String array
String[] MechanicalStatus = {"Select", "Ok", "Failure"};
String[] MedicalStatus = {"Select", "Ok", "Failure"};
String[] WeatherCondition = {"Select", "Sunny", "Raining", "Thunder", "Hurricane"};
String[] Frequency = {"Will be AutoSet", "ATC 1", "ATC 2", "ATC 3"};
//JPanel
JPanel pnlInput = new JPanel(new GridLayout(7, 2, 20, 20));
//Add textfields here
JTextField txtFlightNo = new JTextField(8);
JComboBox cmbMechanicalStatus = new JComboBox(MechanicalStatus);
JComboBox cmbMedicalStatus = new JComboBox(MedicalStatus);
JTextField txtFuelLevel = new JTextField(8);
JComboBox cmbWeatherCondition = new JComboBox(WeatherCondition);
JComboBox cmbFrequency = new JComboBox(Frequency);
//Add labels here
JLabel lblFlightNo = new JLabel("Flight No : ");
JLabel lblMechanicalStatus = new JLabel("Mechanical Status : ");
JLabel lblMedicalStatus = new JLabel("Medical Status : ");
JLabel lblFuelLevel = new JLabel("Fuel Level (gallons) : ");
JLabel lblWeatherCondition = new JLabel("Weather Condition :");
JLabel lblFrequency = new JLabel("Frequency : ");
List<AddFlight> Flights = new ArrayList<AddFlight>();
DefaultListModel model;
public Flight(DefaultListModel model) {
this.model = model;
//Adding flightno to panel
pnlInput.add(lblFlightNo);
pnlInput.add(txtFlightNo);
//Adding mechanicalstatus to the panel
pnlInput.add(lblMechanicalStatus);
pnlInput.add(cmbMechanicalStatus);
//Adding medicalstatus to the panel
pnlInput.add(lblMedicalStatus);
pnlInput.add(cmbMedicalStatus);
//Adding fuellevel to the panel
pnlInput.add(lblFuelLevel);
pnlInput.add(txtFuelLevel);
//Adding weathercondition to the panel
pnlInput.add(lblWeatherCondition);
pnlInput.add(cmbWeatherCondition);
//Adding frequency to the panel
pnlInput.add(lblFrequency);
pnlInput.add(cmbFrequency);
}
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(null, pnlInput, "Flight Details",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.out.println("Plane [" + txtFlightNo.getText() + "] has arrived at the airport.\n");
System.out.println("Mechanical status " + cmbMechanicalStatus.getSelectedItem() + "\n");
}
Flights.add(new AddFlight(txtFlightNo.getText(),
(String) cmbMechanicalStatus.getSelectedItem(),
(String) cmbMedicalStatus.getSelectedItem(),
Float.valueOf((txtFuelLevel.getText()).trim()).floatValue(),
(String) cmbWeatherCondition.getSelectedItem(),
(String) cmbFrequency.getSelectedItem()));
//persons.add(new AddFlight("UL210", "FAILURE", "OK", 90, "Rainy", "ATC 2"));
for (AddFlight Flight : Flights) {
System.out.println("FLight No : " + Flight.getFlightNo());
System.out.println("Mechanical Status : " + Flight.getMechanicalStatus());
System.out.println("Medical Status : " + Flight.getMedicalStatus());
System.out.println("Fuel Level : " + Flight.getFuelLevel());
System.out.println("Weather Condition: " + Flight.getWeatherCondition());
System.out.println("Frequency : " + Flight.getFrequency());
}
model.add(0, Flights);
}
}
感谢任何帮助......
答案 0 :(得分:1)
您在控件中显示两次列表。此外,如果要显示相关信息而不仅仅是对象类型和内存地址,则需要定义toString
方法:
public String toString()
{
return "Flight No: " + flightNo + ", Mechanical Status:" + mechStatus + ", Medical Status : " + medStatus + ",Fuel Level : " + fuel;
}
编辑:
看起来,为了在一行上显示每个AddFlight
对象,你应该使用
model.add(0, Flights.get(Flights.size() - 1));
(将每个AddFlight
对象单独添加到模型中)
而不是
model.add(0,Flights);
(每次添加新元素时,将整个ArrayList
添加到模型中)