1

我目前正在开发一个程序,我之前曾问过涉及.

从那以后我已经取得了很大的进步,现在我准备开始客户端程序的 infoPanel 部分。我已经阅读了文档和其他问题,但我仍然有点困惑。我如何将面板设置为,你知道的,工作?如何将它与 JList 链接(如果甚至可以使用 JList 而不是组合框),然后如何确定哪些选择引用了哪张卡?

非常感谢您的帮助!

来源:

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

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


    public ClientApp()
    {
        this.setSize(320,380);
        this.setTitle("Honeydukes Candy Order");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());

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


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

        JScrollPane scroll = new JScrollPane(candyList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                             JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        invntryPanel.add(scroll);

        JPanel startCard = new JPanel();
        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 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();

        infoPanel.add(startCard);
        infoPanel.add(acidPopsCard);
        infoPanel.add(batsBloodSoupCard);
        infoPanel.add(bertieBottsCard);
        infoPanel.add(bloodPopsCard);
        infoPanel.add(cauldronCakesCard);
        infoPanel.add(charmChocCard);
        infoPanel.add(chocoballsCard);
        infoPanel.add(chocCauldronsCard);
        infoPanel.add(chocFrogsCard);
        infoPanel.add(chocSkeleCard);
        infoPanel.add(chocWands);
        infoPanel.add(chocolocoCard);
        infoPanel.add(roachClustersCard);
        infoPanel.add(nougatCard);
        infoPanel.add(crystalPineappleCard);
        infoPanel.add(droobleGumCard);
        infoPanel.add(explodeBonbonsCard);
        infoPanel.add(toffeesCard);
        infoPanel.add(fizzWhizCard);
        infoPanel.add(fudgeFliesCard);
        infoPanel.add(iceMiceCard);
        infoPanel.add(jellySlugsCard);
        infoPanel.add(liquorWandsCard);
        infoPanel.add(pepImpsCard);
        infoPanel.add(pinkCocoIceCard);
        infoPanel.add(pixiePuffsCard);
        infoPanel.add(pumpkFizzCard);
        infoPanel.add(saltTaffeyCard);
        infoPanel.add(shockChocCard);
        infoPanel.add(skeleSweetsCard);
        infoPanel.add(spindleSpidersCard);
        infoPanel.add(sugarQuillsCard);
        infoPanel.add(sugarWingsCard);
        infoPanel.add(flossMintsCard);
        infoPanel.add(splintMintsCard);
        infoPanel.add(treacleFudgeCard);
        infoPanel.add(chocolatesCard);
        infoPanel.add(wizochocCard);

        this.add(invntryPanel, BorderLayout.WEST);
        this.add(infoPanel, BorderLayout.EAST);
        this.setVisible(true);
    }
}
4

1 回答 1

2

我将向 JList 添加一个 ListSelectionListener,并在该侦听器内部更改 CardLayout 显示的卡片。

您确实需要先阅读有关如何使用 CardLayout 的教程,因为您会看到您的 add 方法是错误的。考虑将 JList 保存的字符串用作您在将组件添加到使用 CardLayout 的面板时使用的常量。

于 2012-07-09T23:06:35.763 回答