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

アクション Performed (ActionEvent e) では、各オブジェクトをJScrollPane でわかりやすい方法で1 行に出力したいと考えています。

model.add(0,Flights);

私の出力は次のようになります[ ArrayListに2つのオブジェクトを追加すると]:

代替テキスト

私が欲しいもの:

1) ArrayList 内の各オブジェクトを 1 行に表示したい。

2)そして、JScrollPaneで次のように表示したい:


Flight No: UL209, Mechanical Status:OK, Medical Status: Failure, Fuel Level : 12.0 //ライン1

フライト番号: UL210、機械的ステータス:OK、医療ステータス: OK、燃料レベル: 22.0 // ライン 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());

    }

私の完全なコード


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

どんな助けでも大歓迎です...

4

1 に答える 1

1

コントロールにリストを 2 回表示しています。さらに、単にオブジェクト タイプとメモリ アドレスではなく関連情報を表示する場合は、toStringメソッドを定義する必要があります。

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

編集:

AddFlightオブジェクトを1行に表示するには、次を使用する必要があります

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

AddFlight(各オブジェクトを個別にモデルに追加します)

それ以外の

model.add(0,Flights);

ArrayList(新しい要素を追加するたびに全体をモデルに追加します)

于 2010-07-11T04:52:24.383 に答える