我正在编写一个小程序来购买音乐。基本上它有 16 首歌曲,song1、song2、song3、song4 等作为复选框。用户应该能够选择他/她想要购买的歌曲,然后单击添加到购物车以显示购物车。根据在前一屏幕中所做的检查,购物车应该有标签。
例如:如果选中歌曲 1、3 和 5,购物车应显示三个标签 - 歌曲 1、3 和 5。
那么,如何在检查的基础上创建标签,而不必事先创建 16 个标签,从而弄得一团糟?我应该以某种方式使用数组吗?
这有点复杂,因为我已经在卡片布局中创建了复选框。我写了一个小程序(使用普通的旧记事本),带有单选按钮来选择一种流派。选择一个单选按钮会显示 4 首歌曲(这是我使用卡片的地方)。
content_cat.setLayout(new BoxLayout(content_cat,
BoxLayout.Y_AXIS));
content_cat.add(Box.createRigidArea(new Dimension(0,30)));
category_label = new JLabel("Select the category");
category_label.setAlignmentX(Component.CENTER_ALIGNMENT);
content_cat.add(category_label);
content_cat.add(Box.createRigidArea(new Dimension(0,30)));
pop = new JRadioButton("Pop");
pop.setAlignmentX(Component.LEFT_ALIGNMENT);
content_cat.add(pop);
content_cat.add(Box.createRigidArea(new Dimension(0,30)));
rock = new JRadioButton("Rock");
rock.setAlignmentX(Component.LEFT_ALIGNMENT);
content_cat.add(rock);
content_cat.add(Box.createRigidArea(new Dimension(0,30)));
jazz = new JRadioButton("Jazz");
jazz.setAlignmentX(Component.LEFT_ALIGNMENT);
content_cat.add(jazz);
content_cat.add(Box.createRigidArea(new Dimension(0,30)));
hiphop = new JRadioButton("Hip hop");
hiphop.setAlignmentX(Component.LEFT_ALIGNMENT);
content_cat.add(hiphop);
content_cat.add(Box.createRigidArea(new Dimension(0,30)));
group = new ButtonGroup();
group.add(pop);
group.add(rock);
group.add(jazz);
group.add(hiphop);
pop.addItemListener(this);
rock.addItemListener(this);
jazz.addItemListener(this);
hiphop.addItemListener(this);
//List Panel
content_list = new Panel();
gbc_content.gridx = 1;
gbc_content.gridy = 0;
gbc_content.weightx = 3;
gbc_content.weighty = 1;
gbc_content.fill = GridBagConstraints.BOTH;
content.add(content_list,gbc_content);
content_list.setLayout(new GridBagLayout());
//List Title Panel
list_title = new Panel();
gbc_list.gridx = 0;
gbc_list.gridy = 0;
gbc_list.weightx = 1;
gbc_list.weighty = 1;
gbc_list.fill = GridBagConstraints.BOTH;
content_list.add(list_title,gbc_list);
list_label = new JLabel("List of CD's Available");
list_title.add(list_label);
//List Cards Panel
list_card = new Panel();
list_card.setLayout(new CardLayout());
gbc_list.gridx = 0;
gbc_list.gridy = 1;
gbc_list.weightx = 1;
gbc_list.weighty = 4;
gbc_list.fill = GridBagConstraints.BOTH;
content_list.add(list_card,gbc_list);
//Pop card
card_pop = new Panel();
card_pop.setLayout(new BoxLayout(card_pop,
BoxLayout.Y_AXIS));
card_pop.add(Box.createRigidArea(new Dimension(0,30)));
pop1 = new JCheckBox("21 - Adele");
pop1.setAlignmentX(Component.LEFT_ALIGNMENT);
card_pop.add(pop1);
card_pop.add(Box.createRigidArea(new Dimension(0,30)));
pop2 = new JCheckBox("D'elles - Celine Dion");
pop2.setAlignmentX(Component.LEFT_ALIGNMENT);
card_pop.add(pop2);
card_pop.add(Box.createRigidArea(new Dimension(0,30)));
pop3 = new JCheckBox("Invincible - Michael Jackson");
pop3.setAlignmentX(Component.LEFT_ALIGNMENT);
card_pop.add(pop3);
card_pop.add(Box.createRigidArea(new Dimension(0,30)));
pop4 = new JCheckBox("Speak Now - Taylor Swift");
pop4.setAlignmentX(Component.LEFT_ALIGNMENT);
card_pop.add(pop4);
card_pop.add(Box.createRigidArea(new Dimension(0,30)));
//Rock card
card_rock = new Panel();
card_rock.setLayout(new BoxLayout(card_rock,
BoxLayout.Y_AXIS));
card_rock.add(Box.createRigidArea(new Dimension(0,30)));
rock1 = new JCheckBox("Let It Be - Beatles");
rock1.setAlignmentX(Component.LEFT_ALIGNMENT);
card_rock.add(rock1);
card_rock.add(Box.createRigidArea(new Dimension(0,30)));
rock2 = new JCheckBox("Viva la Vida - Coldplay");
rock2.setAlignmentX(Component.LEFT_ALIGNMENT);
card_rock.add(rock2);
card_rock.add(Box.createRigidArea(new Dimension(0,30)));
rock3 = new JCheckBox("Meteora - Linkin Park");
rock3.setAlignmentX(Component.LEFT_ALIGNMENT);
card_rock.add(rock3);
card_rock.add(Box.createRigidArea(new Dimension(0,30)));
rock4 = new JCheckBox("Dark Horse - Nickelback");
rock4.setAlignmentX(Component.LEFT_ALIGNMENT);
card_rock.add(rock4);
//Jazz card
card_jazz = new Panel();
card_jazz.setLayout(new BoxLayout(card_jazz,
BoxLayout.Y_AXIS));
card_jazz.add(Box.createRigidArea(new Dimension(0,30)));
jazz1 = new JCheckBox("Come Sing With Me - Frank Sinatra");
jazz1.setAlignmentX(Component.LEFT_ALIGNMENT);
card_jazz.add(jazz1);
card_jazz.add(Box.createRigidArea(new Dimension(0,30)));
jazz2 = new JCheckBox("Closer - Josh Groban");
jazz2.setAlignmentX(Component.LEFT_ALIGNMENT);
card_jazz.add(jazz2);
card_jazz.add(Box.createRigidArea(new Dimension(0,30)));
jazz3 = new JCheckBox("Little Broken Hearts - Norah Jones");
jazz3.setAlignmentX(Component.LEFT_ALIGNMENT);
card_jazz.add(jazz3);
card_jazz.add(Box.createRigidArea(new Dimension(0,30)));
jazz4 = new JCheckBox("Genius and Friends - Ray Charles");
jazz4.setAlignmentX(Component.LEFT_ALIGNMENT);
card_jazz.add(jazz4);
card_jazz.add(Box.createRigidArea(new Dimension(0,30)));
//Hiphop card
card_hiphop = new Panel();
card_hiphop.setLayout(new BoxLayout(card_hiphop,
BoxLayout.Y_AXIS));
card_hiphop.add(Box.createRigidArea(new Dimension(0,30)));
hiphop1 = new JCheckBox("Curtis - 50 Cent");
hiphop1.setAlignmentX(Component.LEFT_ALIGNMENT);
card_hiphop.add(hiphop1);
card_hiphop.add(Box.createRigidArea(new Dimension(0,30)));
hiphop2 = new JCheckBox("Freedom - Akon");
hiphop2.setAlignmentX(Component.LEFT_ALIGNMENT);
card_hiphop.add(hiphop2);
card_hiphop.add(Box.createRigidArea(new Dimension(0,30)));
hiphop3 = new JCheckBox("Relapse - Eminem");
hiphop3.setAlignmentX(Component.LEFT_ALIGNMENT);
card_hiphop.add(hiphop3);
card_hiphop.add(Box.createRigidArea(new Dimension(0,30)));
hiphop4 = new JCheckBox("Takin' Over");
hiphop4.setAlignmentX(Component.LEFT_ALIGNMENT);
card_hiphop.add(hiphop4);
card_hiphop.add(Box.createRigidArea(new Dimension(0,30)));
pop1.addItemListener(this);
pop2.addItemListener(this);
pop3.addItemListener(this);
pop4.addItemListener(this);
rock1.addItemListener(this);
rock2.addItemListener(this);
rock3.addItemListener(this);
rock4.addItemListener(this);
jazz1.addItemListener(this);
jazz2.addItemListener(this);
jazz3.addItemListener(this);
jazz4.addItemListener(this);
hiphop1.addItemListener(this);
hiphop2.addItemListener(this);
hiphop3.addItemListener(this);
hiphop4.addItemListener(this);
list_card.add(card_pop,POP);
list_card.add(card_rock,ROCK);
list_card.add(card_jazz, JAZZ);
list_card.add(card_hiphop, HIPHOP);
底部有一个按钮可以“添加到购物车”。单击它时,它会打开一个带有购物车的新框架,这就是我要显示所选歌曲的地方。
这是事件处理代码
public void itemStateChanged(ItemEvent e)
{
CardLayout cl = (CardLayout)(list_card.getLayout());
if(e.getSource() == pop)
cl.show(list_card, POP);
if(e.getSource() == rock)
cl.show(list_card,ROCK);
if(e.getSource() == jazz)
cl.show(list_card, JAZZ);
if(e.getSource() == hiphop)
cl.show(list_card, HIPHOP);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == atc) //add to cart button
{
cart = new JFrame("Cart");
cart.setVisible(true);
}
}
所以基本上,使用 appletviewer 或网络浏览器打开小程序,选择每个流派下需要的歌曲,单击添加到购物车以显示购物车的框架。
抱歉复制粘贴这么多代码...