当前正在Swing应用程序上工作,我需要使用ListSelectionListener来获取JList中当前选择的值。我知道如何将其添加到JList本身,但是无论我实现什么,编译器都找不到该符号。有什么建议么?

资源:

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

public class ClientApp extends JFrame
{
    public static void main(String[] args)
    {
        new ClientApp();
    }


    public ClientApp()
    {
        this.setSize(750,380);
        this.setTitle("Honeydukes Muggle Ordering System");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel infoPanel = new JPanel(new CardLayout());
        JPanel invntryPanel = new JPanel();


        //Creating the array for the invntryPanel Jlist

        String[] candy = {"Acid Pops", "Bat's Blood Soup",
                          "Bertie Bott's Every Flavour Beans",
                          "Blood-flavoured Lollipops",
                          "Cauldron Cakes", "Charm Choc",
                          "Chocoballs", "Chocolate Cauldrons",
                          "Chocolate Frogs", "Chocolate Skeletons",
                          "Chocolate Wands", "Choco-Loco", "Cockroach Clusters",
                          "Nougat", "Crystallised Pineapple",
                          "Drooble's Best Blowing Gum", "Exploding Bonbons",
                          "Toffees", "Fizzing Whizzbees",
                          "Fudge Flies", "Ice Mice",
                          "Jelly Slugs", "Liquourice Wands",
                          "Pepper Imps", "Peppermint Toads",
                          "Pink Coconut Ice", "Pixie Puffs",
                          "Pumpkin Fizz", "Salt Water Taffy",
                          "Shock-o-Choc", "Skeletal Sweets",
                          "Splindle's Lick'O'Rish Spiders",
                          "Sugar Quills", "Sugared Butterfly Wings",
                          "Toothflossing Stringmints", "Tooth-Splintering Strongmints",
                          "Treacle Fudge", "Chocolates", "Wizochoc"};
        JList candyList = new JList(candy);
        candyList.setVisibleRowCount(18);
        candyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        ListSelectionListener sl = new ListSelectionListener() {
           public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting() == false) {
                String currentCard = (String)candyList.getSelectedValue();
            }
           }
        };

        candyList.addListSelectionListener(sl);

        //Creating a scrollpane for the JList
        JScrollPane scroll = new JScrollPane(candyList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                             JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        invntryPanel.add(scroll);


        //Creating the cards
        JPanel startCard = new JPanel(new BorderLayout());
        JPanel acidPopsCard = new JPanel();
        JPanel batsBloodSoupCard = new JPanel();
        JPanel bertieBottsCard = new JPanel();
        JPanel bloodPopsCard = new JPanel();
        JPanel cauldronCakesCard = new JPanel();
        JPanel charmChocCard = new JPanel();
        JPanel chocoballsCard = new JPanel();
        JPanel chocCauldronsCard = new JPanel();
        JPanel chocFrogsCard = new JPanel();
        JPanel chocSkeleCard = new JPanel();
        JPanel chocWands = new JPanel();
        JPanel chocolocoCard = new JPanel();
        JPanel roachClustersCard = new JPanel();
        JPanel nougatCard = new JPanel();
        JPanel crystalPineappleCard = new JPanel();
        JPanel droobleGumCard = new JPanel();
        JPanel explodeBonbonsCard = new JPanel();
        JPanel toffeesCard = new JPanel();
        JPanel fizzWhizCard = new JPanel();
        JPanel fudgeFliesCard = new JPanel();
        JPanel iceMiceCard = new JPanel();
        JPanel jellySlugsCard = new JPanel();
        JPanel liquorWandsCard = new JPanel();
        JPanel pepImpsCard = new JPanel();
        JPanel pepToadsCard = new JPanel();
        JPanel pinkCocoIceCard = new JPanel();
        JPanel pixiePuffsCard = new JPanel();
        JPanel pumpkFizzCard = new JPanel();
        JPanel saltTaffeyCard = new JPanel();
        JPanel shockChocCard = new JPanel();
        JPanel skeleSweetsCard = new JPanel();
        JPanel spindleSpidersCard = new JPanel();
        JPanel sugarQuillsCard = new JPanel();
        JPanel sugarWingsCard = new JPanel();
        JPanel flossMintsCard = new JPanel();
        JPanel splintMintsCard = new JPanel();
        JPanel treacleFudgeCard = new JPanel();
        JPanel chocolatesCard = new JPanel();
        JPanel wizochocCard = new JPanel();

        //Adding the cards to the infoPanel
        infoPanel.add(startCard, "Start");
        infoPanel.add(acidPopsCard, "Acid Pops");
        infoPanel.add(batsBloodSoupCard, "Bat's Blood Soup");
        infoPanel.add(bertieBottsCard, "Bertie Bott's Every Flavour Beans");
        infoPanel.add(bloodPopsCard, "Blood-flavoured Lollipops");
        infoPanel.add(cauldronCakesCard, "Cauldron Cakes");
        infoPanel.add(charmChocCard, "Charm Choc");
        infoPanel.add(chocoballsCard, "Chocoballs");
        infoPanel.add(chocCauldronsCard, "Chocolate Cauldrons");
        infoPanel.add(chocFrogsCard, "Chocolate Frogs");
        infoPanel.add(chocSkeleCard, "Chocolate Skeletons");
        infoPanel.add(chocWands, "Chocolate Wands");
        infoPanel.add(chocolocoCard, "Choco-Loco");
        infoPanel.add(roachClustersCard, "Cockroach Clusters");
        infoPanel.add(nougatCard, "Nougat");
        infoPanel.add(crystalPineappleCard, "Crystallised Pineapple");
        infoPanel.add(droobleGumCard, "Drooble's Best Blowing Gum");
        infoPanel.add(explodeBonbonsCard, "Exploding Bonbons");
        infoPanel.add(toffeesCard, "Toffees");
        infoPanel.add(fizzWhizCard, "Fizzing Whizzbees");
        infoPanel.add(fudgeFliesCard, "Fudge Flies");
        infoPanel.add(iceMiceCard, "Ice Mice");
        infoPanel.add(jellySlugsCard, "Jelly Slugs");
        infoPanel.add(liquorWandsCard, "Liquourice Wands");
        infoPanel.add(pepImpsCard, "Pepper Imps");
        infoPanel.add(pepToadsCard, "Peppermint Toads");
        infoPanel.add(pinkCocoIceCard, "Pink Coconut Ice");
        infoPanel.add(pixiePuffsCard, "Pixie Puffs");
        infoPanel.add(pumpkFizzCard, "Pumpkin Fizz");
        infoPanel.add(saltTaffeyCard, "Salt Water Taffy");
        infoPanel.add(shockChocCard, "Shock-o-Choc");
        infoPanel.add(skeleSweetsCard, "Skeletal Sweets");
        infoPanel.add(spindleSpidersCard, "Splindle's Lick'O'Rish Spiders");
        infoPanel.add(sugarQuillsCard, "Sugar Quills");
        infoPanel.add(sugarWingsCard, "Sugared Butterfly Wings");
        infoPanel.add(flossMintsCard, "Toothflossing Stringmints");
        infoPanel.add(splintMintsCard, "Tooth-Splintering Strongmints");
        infoPanel.add(treacleFudgeCard, "Treacle Fudge");
        infoPanel.add(chocolatesCard, "Chocolates");
        infoPanel.add(wizochocCard, "Wizochoc");

        //startCard building
        JLabel startLbl = new JLabel("<html><center>Welcome to the Honeydukes Muggle Ordering System!<br />Please select from one of our products to the left to begin!</center></html>");
        startCard.add(startLbl, BorderLayout.CENTER);

        this.add(invntryPanel, BorderLayout.LINE_START);
        this.add(infoPanel, BorderLayout.CENTER);
        this.setVisible(true);
    }
}


错误:

ClientApp.java:54: error: local variable candyList is accessed from within inner
 class; needs to be declared final
                                String currentCard = (String)candyList.getSelect
edValue();
                                                             ^
Note: ClientApp.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

最佳答案

ListSelectionListener是一个接口,您不能仅通过使用构造函数来创建一个接口。您必须仅实例化一个具体的类,可能为此实例一个抽象的内部类。这样做:

ListSelectionListener sl = new ListSelectionListener() {
   // place the necessary method(s) here as dictated by the interface
};


由于这是家庭作业,因此我将留给您确定所需的方法,但是使用JList和tutorial on writing a ListSelectionListener的教程可以为您提供帮助。

编辑
另外,您是否正在导入所需的所有类?包括ListSelectionListener类?


import javax.swing.event.ListSelectionListener;

关于java - 设置一个ListSelectionListener,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11405843/

10-10 18:26