如何确定选择了哪个JList项?

时间:2013-07-21 15:00:13

标签: java swing user-interface listener jlist

我想根据点击的JList项目运行一些代码。

但是如何确定选择了哪个项目?

以下是代码:

public void launchFrame() {
        String selection1 = "";
        String[]mainContents = {"Vehicles","Bikes/Bicycles","Boats","Houses","Businesses","Objects","Jobs","Ranks","Licenses"};
        String[]selectionVehicles = {};
        String[]selectionBikesBicycles = {};
        String[]selectionBoats = {};
        String[]selectionHouses = {};
        String[]selectionBusinesses = {};
        String[]selectionObjects = {};
        String[]selectionJobs = {};
        String[]selectionRanks = {};
        String[]selectionLicenses = {};
        JFrame f = new JFrame("MTG SAMP Server Reference");
        JList listAll = new JList(mainContents);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.pack();
        f.setSize(1200, 800);
        f.setLocationRelativeTo(null);
        f.add(listAll);
        listAll.setSize(250, 250);
        listAll.setLocation(50, 50);
        switch (selection1) {
            case "Vehicles":
                JList listVehicles = new JList(mainContents);
                f.add(listVehicles);
                listVehicles.setSize(250, 250);
                listVehicles.setLocation(350, 50);
                f.repaint();
                f.revalidate();
                break;
            case "Bikes/Bicycles":
                JList listBikesBicycles = new JList(mainContents);
                f.add(listBikesBicycles);
                listBikesBicycles.setSize(250, 250);
                listBikesBicycles.setLocation(350, 50);
                f.repaint();
                f.revalidate();
                break;
            case "Boats":
                JList listBoats = new JList(mainContents);
                f.add(listBoats);
                listBoats.setSize(250, 250);
                listBoats.setLocation(350, 50);
                f.repaint();
                f.revalidate();
                break;
            case "Houses":
                JList listHouses = new JList(mainContents);
                f.add(listHouses);
                listHouses.setSize(250, 250);
                listHouses.setLocation(350, 50);
                f.repaint();
                f.revalidate();
                break;
            case "Businesses":
                JList listBusinesses = new JList(mainContents);
                f.add(listBusinesses);
                listBusinesses.setSize(250, 250);
                listBusinesses.setLocation(350, 50);
                f.repaint();
                f.revalidate();
                break;
            case "Objects":
                JList listObjects = new JList(mainContents);
                f.add(listObjects);
                listObjects.setSize(250, 250);
                listObjects.setLocation(350, 50);
                f.repaint();
                f.revalidate();
                break;
            case "Jobs":
                JList listJobs = new JList(mainContents);
                f.add(listJobs);
                listJobs.setSize(250, 250);
                listJobs.setLocation(350, 50);
                f.repaint();
                f.revalidate();
                break;
            case "Ranks":
                JList listRanks = new JList(mainContents);
                f.add(listRanks);
                listRanks.setSize(250, 250);
                listRanks.setLocation(350, 50);
                f.repaint();
                f.revalidate();
                break;
            case "Licenses":
                JList listLicenses = new JList(mainContents);
                f.add(listLicenses);
                listLicenses.setSize(250, 250);
                listLicenses.setLocation(350, 50);
                f.repaint();
                f.revalidate();
                break;
        }
            Container contentPaneMain = f.getContentPane();
            contentPaneMain.add(new MTGSAMPServerReference());
        }

引号中的每组文字代表一个单独的项目。

因此,如果点击“车辆”,如何设置selection1"Vehicles"

1 个答案:

答案 0 :(得分:0)

假设您使用的是Java 7(如果之前您将无法在switch语句中使用字符串):

selection1 = (String)listAll.getSelectedValue();