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

发布于 2024-09-09 03:18:07 字数 5517 浏览 5 评论 0原文

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

< img src="https://i.sstatic.net/lCt5u.jpg" alt="替代文字">

我想要什么:

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);
}
}

任何帮助表示赞赏......

I have declared an ArrayList and DefaultListModel

DefaultListModel model;

List<AddFlight> Flights = new ArrayList<AddFlight>();

To this list I add an object as an element

Flights.add(new AddFlight(txtFlightNo.getText(),
            (String)cmbMechanicalStatus.getSelectedItem(),
            (String)cmbMedicalStatus.getSelectedItem(),
            Float.valueOf((txtFuelLevel.getText()).trim()).floatValue(),
            (String)cmbWeatherCondition.getSelectedItem(),
            (String)cmbFrequency.getSelectedItem()));

In the action Performed (ActionEvent e) I want to print each object in one line in a understandable way on the JScrollPane.

model.add(0,Flights);

My output looks like this [When I add 2 objects to the ArrayList]:

alt text

WHAT I WANT:

1) I want each object in the ArrayList to appear in one line.

2) And I want it to appear like this in the JScrollPane:


Flight No: UL209, Mechanical Status:OK, Medical Status : Failure,Fuel Level : 12.0 //Line one

Flight No: UL210, Mechanical Status:OK, Medical Status : OK,Fuel Level : 22.0 // Line two


However, I managed to print out each element using the code below :

   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());

    }

MY FULL CODE


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);
}
}

Any Help is appreciated...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九厘米的零° 2024-09-16 03:18:07

您在控件中显示列表两次。此外,如果您想显示相关信息而不仅仅是对象类型和内存地址,您需要定义一个 toString 方法:

public String toString()
{
    return "Flight No: " + flightNo + ", Mechanical Status:" + mechStatus + ", Medical Status : " + medStatus + ",Fuel Level : " + fuel;
}

编辑:

看起来,为了显示每个 AddFlight< /code> 对象在一行上,您应该使用

model.add(0, Flights.get(Flights.size() - 1));

(将每个 AddFlight 对象分别添加到您的模型中)

而不是

model.add(0,Flights);

(每次添加时将整个 ArrayList 添加到您的模型中 )一个新元素)

You're displaying your list twice in your control. Additionally, if you want to display relevant information rather than simply the object type and memory address, you need to define a toString method:

public String toString()
{
    return "Flight No: " + flightNo + ", Mechanical Status:" + mechStatus + ", Medical Status : " + medStatus + ",Fuel Level : " + fuel;
}

EDIT:

It looks like, in order to display each AddFlight object on one line, you should be using

model.add(0, Flights.get(Flights.size() - 1));

(Add each AddFlight object to your model individually)

Instead of

model.add(0,Flights);

(Adding the entire ArrayList to your model each time you add a new element)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文