好的,显示了两个jcombobox,一个是航班离开的城市列表,另一个是当用户从两个组合框中选择一个选项时航班飞往的城市列表,我希望它显示您从巴黎飞往贝尔法斯特,我有以下代码,但我不知道如何添加其他选择,因为这只是说您从巴黎飞往巴黎的航班。

        if(e.getSource() == ownerList )
        {
            JComboBox cb = (JComboBox)e.getSource();
            String ownerName = (String)cb.getSelectedItem();
             if(ownerName.equals("Paris"))
        {
            text9.setText(ownerName);
            int flag = 10;
            drawApp(flag);
        }
        }

        if(e.getSource() == cityList )
        {
            JComboBox cb = (JComboBox)e.getSource();
            String cityName = (String)cb.getSelectedItem();
             if(cityName.equals("Belfast"))
        {
            text10.setText(cityName);
            int flag = 10;
            drawApp(flag);
        }
        }

最佳答案

我有点重写了整个脚本(对不起)...

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class FlightBooker extends JFrame implements ActionListener {
    FlightBooker() {
        super("Book a Flight!");
        JLabel fromLabel = new JLabel("Current Location:");
        JComboBox fromLocations = new JComboBox();
        fromLocations.addItem("Paris");
        //fromLocations.addItem(someLocation);
        //...
        JLabel toLabel = new JLabel("Destination:");
        JComboBox destinations = new JComboBox();
        destinations.addItem("Belfast");
        //destinations.addItem(someLocation);
        //...
        JButton okButton = new JButton("OK");
        JLabel status = new JLabel("");
        add(fromLabel);
        add(fromLocations);
        add(toLabel);
        add(destinations);
        okButton.addActionListener(this);
        add(okButton);
        add(status);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }

    public void actionPerformed(ActionEvent event) {
        Object from = fromLocations.getSelectedItem();
        String FROM = from.toString();
        Object to = destinations.getSelectedItem();
        String TO = to.toString();
        status.setText("You're flying from " + FROM + "to " + TO + ".");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() { new FlightBooker().setVisible(true); }
        });
    }
}


这应该做您想要的。 :)

10-01 14:18