本文介绍了多个JComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Ok, there is two jcombobox displayed one is a list of cities a flight leaves from and another is a list of cities a flight goes to when a user selects an option from both combo boxes I want it to display you are fly from Paris to Belfast, I have got the following code but i dont know how to add another selection as at the moment it is just saying your a flying from Paris to .

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


推荐答案

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

I've kind of rewritten the entire script (sorry)...

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

这应该做你想要的。 :)

This should do what you want. :)

这篇关于多个JComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 23:25