如何根据复选框状态创建标签?

时间:2012-06-27 14:07:35

标签: java arrays dynamic checkbox labels

我正在编写一个小程序来购买音乐。基本上它有16首歌曲,song1,song2,song3,song4等作为复选框。 用户应该能够选择他/她想要购买的歌曲,然后单击添加到购物车以显示购物车。购物车应根据上一屏幕中的检查标签。

例如:如果选中了song1,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或网络浏览器打开小程序,选择每种类型下所需的歌曲,点击添加到购物车以显示购物车的框架。

抱歉复制这么多代码......

2 个答案:

答案 0 :(得分:0)

这样的事情怎么样:

制作歌曲:

ArrayList<String> songsSelection = new ArrayList();
ArrayList<Song> songs = new ArrayList();

//add all songs you want
songs.add( new Song(1, "Song 1")); 
songs.add( new Song(1, "Song 1")); 
songs.add( new Song(1, "Song 1")); 

创建一个动作以识别选择,在这里你可以根据复选框状态删除或添加歌曲

class SongAction implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        JCheckBox temp = (JCheckBox)e.getSource();
        if (temp.isSelected())
            songsSelection.add(temp.getName());
        else
            songsSelection.remove(temp.getName());
        updateCart();
    }
}

创建复选框

for(Song s : songs){
    JCheckBox cb = new JCheckBox(s.getName());
    cb.addActionListener(new SongAction());
    frame.add( cb );
}

现在您可以在选择后更新购物车

public void updateCart(){
    panel.removeAll(); // clean the labels container
    for(String name : songsSelection ){
        panel.add(new JLable(name))
    }
}

答案 1 :(得分:0)

在我看来,真正的问题是你是否需要使用标签?使用JTextArea来显示选择购买的歌曲不是那么有效且相当容易吗?