作业要求:做一个抽奖系统,要求可以设定参与抽奖的总人数和奖项的个数,获奖不可重复
import java.awt.Panel;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Random;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
public class Choujiang {
JTextArea t=new JTextArea();
public static void main(String[] args) {
// TODO Auto-generated method stub
new Choujiang().init();
}
public void init(){
Panel p=new Panel();
JFrame jf=new JFrame("抽奖系统");
Box h=Box.createHorizontalBox();
Box v=Box.createVerticalBox();
JButton b=new JButton(" 启动");
A(b);
p.add(new JLabel("开始抽奖"));
p.add(b);
v.add(p);
h.add(v);
ScrollPane sp=new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
sp.add(t);
h.add(sp);
jf.add(h);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setBounds(400,400,400,400);
}
public void A(final JButton b) {
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Random random = new Random();
Object[] values = new Object[6];
HashSet hashSet = new HashSet();
for(int i = 0;i < values.length;i++){
int number = random.nextInt(1000) + 1;
hashSet.add(number);
}
values = hashSet.toArray();
t.append("一等奖号"+values[0] + "
");
t.append("二等奖号"+values[1]+" " +values[2]+"
");
t.append("三等奖号"+values[3]+" "+values[4]+" "+values[5]+"
");
t.append("恭喜获奖者");
}
});
}
}