学习心得 六

import java.awt.BorderLayout;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
 public class cj extends JFrame{
 public cj(){
 this.setTitle("抽奖系统");
 this.setSize(250, 180);
 this.setResizable(false);
 this.setLocationRelativeTo(this);
 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 this.setContentPane(createContentPanel());
 }
 private JPanel createCeterPanel(){
     JPanel panel=new JPanel();
     JButton start=new JButton("开始抽奖");
     panel.add(start);
     ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
     final JTextArea area = new JTextArea();
     sp.add(area);
     panel.add(sp,BorderLayout.SOUTH);
     start.addActionListener(new ActionListener() {
        
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
        
                double baseNum = 0.99;
                String product = "矿泉水,打火机,雨伞,指甲刀";
                String num = "2,2,2,1";
                String[] p = product.split(",");
                String[] n = num.split(",");
         
                java.util.Random ran = new java.util.Random();
                double base = ran.nextDouble();
         
                if (base > baseNum) {
                    System.err.println("没中奖");
                    return;
                }
         
                List<String> list = new ArrayList<String>();
                for (int i = 0; i < p.length; i++) {
                    for (int j = 0; j < Integer.parseInt(n[i]); j++) {
                        list.add(p[i]);
                    }
                }
                        //TODO 打乱奖池数据
                int l = ran.nextInt(list.size());
                area.append(list.get(l));
         
            }
   });
     return panel;
     }
 private JPanel createContentPanel(){
 JPanel panel=new JPanel(new BorderLayout());
 panel.add(BorderLayout.CENTER,createCeterPanel());
 return panel;
 }
public static void main(String[] args) {
    new cj().setVisible(true);
}

}

 

 

 

 

posted @ 2016-05-08 21:19  软三2014330309靳楠君  阅读(126)  评论(0编辑  收藏  举报