public class GoodInformation {
String[] goods={"数码相机","MP3","录音笔","PDA","电子词典","随声听","CD机","MD",
"数码产品配件","掌上电脑"};
public String returnGoods(int goodsSelectedInt){
String goodsSelectstr="";
int oneGoods;
for(int i=0;i<10;i++)
{
oneGoods=(int)Math.pow(2,i);
if((goodsSelectedInt&oneGoods)==oneGoods){
goodsSelectstr+=goods[i]+"\n";
}
}
return goodsSelectstr;
} /**
 * @param args
 */
}
/////////////////////////////////////////////////////////////
import java.awt.BorderLayout;
import javax.swing.JCheckBox;
import javax.swing.*;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.*;/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class shoppingFrame extends javax.swing.JFrame implements ItemListener{
private JPanel jPanel1;
GoodInformation goodsInformation=new GoodInformation();
private JCheckBox[] goodsCheckBox=new JCheckBox[10];
private JButton jButton2;
private JButton jButton1;
int goodsSelectedInt=0;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
shoppingFrame inst = new shoppingFrame();
inst.setVisible(true);

}

public shoppingFrame() {
super();
initGUI();
}

private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
pack();
this.setSize(470, 282);
this.setTitle("购物窗口");
this.setLayout(null);
{
jPanel1 = new JPanel();
GridLayout jPanel1Layout = new GridLayout(1, 1);
jPanel1Layout.setHgap(5);
jPanel1Layout.setVgap(5);
jPanel1Layout.setColumns(2);
jPanel1Layout.setRows(5);
getContentPane().add(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1.setBounds(0, 0, 462, 189);
jPanel1.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
}
{
jButton1 = new JButton();
FlowLayout jButton2Layout = new FlowLayout();
getContentPane().add(jButton1);
jButton1.setLayout(null);
jButton1.setText("\u63d0\u4ea4");
jButton1.setBounds(35, 217, 91, 28);
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String goodsSelectedStr=goodsInformation.returnGoods(goodsSelectedInt);
JOptionPane.showMessageDialog(null,"你所选择的商品列表如下所示:\n"+goodsSelectedStr);
System.out.println("jButton1.actionPerformed, event="
+ evt);
//TODO add your code for jButton1.actionPerformed
}
});
}
{
jButton2 = new JButton();
getContentPane().add(jButton2);
jButton2.setLayout(null);
jButton2.setText("\u9000\u51fa");
jButton2.setBounds(252, 217, 77, 28);
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.exit(0);
System.out.println("jButton2.actionPerformed, event="
+ evt);
//TODO add your code for jButton2.actionPerformed
}
});
}
for(int i=0;i<10;i++){
goodsCheckBox[i]=new JCheckBox();
goodsCheckBox[i].setText(goodsInformation.goods[i]);
goodsCheckBox[i].setActionCommand(String.valueOf((int)Math.pow(2,i)));
goodsCheckBox[i].addItemListener(this);
}
for(int i=0;i<10;++i){
jPanel1.add(goodsCheckBox[i]);
}
    
} catch (Exception e) {
e.printStackTrace();
}
}
public void itemStateChanged(ItemEvent e){
JCheckBox checkBox=(JCheckBox)e.getSource();
int oneGoods=Integer.parseInt(checkBox.getActionCommand());
if(checkBox.isSelected()){
goodsSelectedInt=goodsSelectedInt|oneGoods;
}else{
goodsSelectedInt=goodsSelectedInt^oneGoods;
}
}}