我目前正在制作一个我问过questions involving previously的程序。
从那以后我取得了很大的进步,现在我已经准备好开始客户端程序的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);
}
}
答案 0 :(得分:2)
我会将一个ListSelectionListener添加到JList中,并且该侦听器内部会更改CardLayout显示的卡片。
你真的需要首先阅读tutorial on how to use CardLayout,因为你会发现你的添加方法是错误的。考虑使用JList持有的字符串作为在向CardLayout使用面板添加组件时使用的常量。