-2

我希望根据单击的 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"

4

1 回答 1

0

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

selection1 = (String)listAll.getSelectedValue();
于 2013-07-21T15:10:32.913 回答