gpt4 book ai didi

java - 如何在java中将对象的属性添加到JScrollPane

转载 作者:行者123 更新时间:2023-12-01 16:03:27 25 4
gpt4 key购买 nike

我已经声明了 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

我想要什么:

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);
}
}
<小时/>

感谢任何帮助...

最佳答案

您在控件中显示列表两次。另外,如果您想显示相关信息而不仅仅是对象类型和内存地址,则需要定义一个 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 添加到模型中)

关于java - 如何在java中将对象的属性添加到JScrollPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3221892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com