我想设置一个晚会抽奖的小程序,一共三等奖。100个随机数,三等5人,二等3人。一等1人,每次都必须不一样。。。怎么左
------解决方案--------------------------------------------------------
找到了一个以前写的程序改了改
楼主看下吧,功能都实现了,布局和外观你可以自己改改
- Java code
import java.awt.Container;import java.awt.Dimension;import java.awt.Font;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Random;import java.util.Timer;import java.util.TimerTask;import java.util.Vector;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JCheckBoxMenuItem;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JPanel;import javax.swing.SwingConstants;import javax.swing.UIManager;public class SimpleDraw extends JFrame{ private static final long serialVersionUID = 1L; private static final int FASTSPEED = 10;//10ms跳动一次 private static final int NORMALSPEED = 100;//100ms跳动一次 private static final int SLOWSPEED = 1000;//1000ms跳动一次 //以上参数可自行修改 private Container content = null; private JButton btnStart = null; private JLabel labNum = null; private JLabel labRank = null; private JPanel pnlMain = null; private JMenuBar menuBar = null; private JCheckBoxMenuItem fastItem = null; private JCheckBoxMenuItem normalItem = null; private JCheckBoxMenuItem slowItem = null; private Vector<String> waiting = null; private Vector<String> done = null; private Random random = null; private Timer timer = null; private TimerTask task = null; private int speed = FASTSPEED; private int drawNum = -1; private boolean running = false; public SimpleDraw() { setSize(800,600); setTitle("SimpleDraw"); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); initialize(); } private void initialize() { waiting = new Vector<String>(0); done = new Vector<String>(0); Font font = new Font("Dialog",Font.PLAIN,12); UIManager.put("MenuBar.font",font); UIManager.put("Menu.font",font); UIManager.put("CheckBoxMenuItem.font",font); content = getContentPane(); pnlMain = new JPanel(); btnStart = new JButton("开始"); labNum = new JLabel("0", SwingConstants.CENTER); labRank = new JLabel("三等奖", SwingConstants.CENTER); btnStart.setFont(new Font("宋体", Font.PLAIN, 50)); btnStart.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { if (waiting.size()>0) { if (running) { btnStart.setText("开始"); running = false; if (timer != null) { timer.cancel(); timer = null; } done.add(waiting.get(drawNum)); waiting.remove(drawNum); } else { btnStart.setText("停止"); running = true; if (timer == null) { timer = new Timer(); task = new TimerTask() { public void run(){ if (running && waiting.size()>0) { drawNum = random.nextInt(waiting.size()); labNum.setText(waiting.get(drawNum)); } } }; timer.schedule(task, 0, speed); } if (done.size() == 5) { labRank.setText("二等奖"); } else if (done.size() == 8) { labRank.setText("一等奖"); } } } else { labNum.setText("已经抽完!"); } } }); labNum.setFont(new Font("宋体", Font.PLAIN, 120)); labNum.setPreferredSize(new Dimension(670,120)); labNum.setMinimumSize(new Dimension(670,120)); labNum.setBorder(BorderFactory.createEtchedBorder()); labRank.setFont(new Font("宋体", Font.PLAIN, 120)); labRank.setPreferredSize(new Dimension(670,120)); labRank.setMinimumSize(new Dimension(670,120)); labRank.setBorder(BorderFactory.createEtchedBorder()); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); pnlMain.setLayout(gridbag); c.gridx = 0; c.weighty = 1.0; c.weightx = 1.0; gridbag.setConstraints(btnStart, c); pnlMain.add(btnStart); c.weighty = 5.0; c.gridy = 10; gridbag.setConstraints(labNum, c); pnlMain.add(labNum); c.gridy = 20; gridbag.setConstraints(labRank, c); pnlMain.add(labRank); content.add(pnlMain); makeMenuBar(); for (int i=1;i<101;i++) { waiting.add("" + i); } random = new Random(); } private void makeMenuBar() { if (menuBar == null) { menuBar = new JMenuBar(); menuBar.setBorder(null); setJMenuBar(menuBar); JMenu menu; menu = new JMenu("速度"); fastItem = new JCheckBoxMenuItem("快", true); normalItem = new JCheckBoxMenuItem("中", false); slowItem = new JCheckBoxMenuItem("慢", false); menu.add(fastItem); menu.add(normalItem); menu.add(slowItem); menuBar.add(menu); fastItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { speed = FASTSPEED; fastItem.setSelected(true); normalItem.setSelected(false); slowItem.setSelected(false); } }); normalItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { speed = NORMALSPEED; fastItem.setSelected(false); normalItem.setSelected(true); slowItem.setSelected(false); } }); slowItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { speed = SLOWSPEED; fastItem.setSelected(false); normalItem.setSelected(false); slowItem.setSelected(true); } }); } } public static void main(String arg[]) { SimpleDraw luckydraw = new SimpleDraw(); luckydraw.setVisible(true); }}