随机抽奖小程序_在线随机抽号小程序
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                随机抽奖小程序_在线随机抽号小程序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                
本實例使用隨機(jī)數(shù)字生成5位抽獎號碼,并顯示在窗體的5個文本框中。當(dāng)用戶單擊”開始”按鈕時,將啟動一個線程對象為5個文本框生成隨機(jī)數(shù)字。單擊”抽獎”按鈕時,線程對象停止運(yùn)行,并且將準(zhǔn)確的中獎號碼顯示在信息文本框中。
開發(fā)一個抽獎小工具的實例。
(1)自定義文本框組件,把5個生成隨機(jī)數(shù)的文本框的公共屬性抽象定義到該文本框。
package com.lzw;  
import java.awt.Font;  
import javax.swing.JTextField;  
import javax.swing.SwingConstants;  
//自定義的文本框組件  
public class NumField extends JTextField {  
    private static final Font numfont = new Font("", Font.BOLD, 48);//定義文本框使用的字體  
   public NumField() {  
        super();   //執(zhí)行父類構(gòu)造方法  
        setHorizontalAlignment(SwingConstants.CENTER);  //設(shè)置文本居中對齊  
        setFont(numfont);   //設(shè)置字體  
        setFocusable(false); //取消焦點  
    }  
} 
(2)編寫抽獎窗體。
public class Lottery extends JFrame { private JTextField infoField; //抽獎號碼確認(rèn)文本框  private NumField[] numFields; //隨機(jī)號碼文本框數(shù)組  private RandomNum randomThread=new RandomNum(); public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { try { Lottery frame = new Lottery(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } //創(chuàng)建窗體界面的構(gòu)造方法  public Lottery() { super(); final BorderLayout borderLayout_1 = new BorderLayout(); borderLayout_1.setVgap(10); getContentPane().setLayout(borderLayout_1); //設(shè)置布局管理器  setBounds(100, 100, 420, 256); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JPanel contentPanel = new JPanel(); //創(chuàng)建中間的內(nèi)容面板  final BorderLayout borderLayout = new BorderLayout(); borderLayout.setVgap(10); borderLayout.setHgap(10); contentPanel.setLayout(borderLayout); //設(shè)置內(nèi)容面板布局管理器  getContentPane().add(contentPanel); //添加內(nèi)容面板到窗體  final JPanel numPanel = new JPanel(); //創(chuàng)建顯示隨機(jī)數(shù)的面板  contentPanel.add(numPanel); //添加隨機(jī)數(shù)面板到內(nèi)容面板  final GridLayout gridLayout = new GridLayout(1, 0); gridLayout.setHgap(10); numPanel.setLayout(gridLayout); //設(shè)置隨機(jī)數(shù)面板布局管理器  numFields = new NumField[5]; //創(chuàng)建隨機(jī)數(shù)文本框數(shù)組  for(int i=0;i<numFields.length;i++){ //初始化隨機(jī)數(shù)文本框  numFields[i]=new NumField(); //初始化數(shù)組元素  numPanel.add(numFields[i]); //添加文本框到隨機(jī)數(shù)面板   } final JPanel infoPanel = new JPanel(); //創(chuàng)建顯示抽獎號碼的面板  infoPanel.setLayout(new BorderLayout()); //設(shè)置面板布局管理器  contentPanel.add(infoPanel, BorderLayout.SOUTH); //添加面板到窗體  final JLabel label_1 = new JLabel(); //布局抽獎號碼面板  label_1.setFont(new Font("", Font.BOLD, 20)); label_1.setText("隨機(jī)抽獎的中將號碼是:"); infoPanel.add(label_1, BorderLayout.WEST); infoField = new JTextField(); infoPanel.add(infoField); final JLabel logoLabel = new JLabel(); //布局LOGO標(biāo)簽  logoLabel.setFont(new Font("隸書", Font.PLAIN, 72)); logoLabel.setHorizontalAlignment(SwingConstants.CENTER); getContentPane().add(logoLabel, BorderLayout.NORTH); logoLabel.setText("隨機(jī)抽獎"); final JPanel controlPanel = new JPanel(); //創(chuàng)建控制按鈕面板  final FlowLayout flowLayout = new FlowLayout(); flowLayout.setHgap(25); controlPanel.setLayout(flowLayout); //設(shè)置面板布局  getContentPane().add(controlPanel, BorderLayout.SOUTH); //添加面板到窗體底部  final JButton startButton = new JButton(); //創(chuàng)建開始按鈕  startButton.addActionListener(new ActionListener() { //添加事件監(jiān)聽器  public void actionPerformed(final ActionEvent e) { do_startButton_actionPerformed(e); } }); startButton.setText("開始"); controlPanel.add(startButton); //添加按鈕到面板  final JButton lotteryButton = new JButton(); //創(chuàng)建抽獎按鈕  lotteryButton.addActionListener(new ActionListener() { //添加事件監(jiān)聽器  public void actionPerformed(final ActionEvent e) { do_lotteryButton_actionPerformed(e); } }); lotteryButton.setText("抽獎"); controlPanel.add(lotteryButton); final JButton exitButton = new JButton(); //創(chuàng)建退出按鈕  exitButton.addActionListener(new ActionListener() { //添加事件監(jiān)聽器  public void actionPerformed(final ActionEvent e) { do_exitButton_actionPerformed(e); } }); exitButton.setText("退出"); controlPanel.add(exitButton); } // 生成隨機(jī)數(shù)字的內(nèi)部線程類  class RandomNum extends Thread { private boolean stop=false; //線程狀態(tài)變量  public void run() { while (!stop) { for (final NumField nf : numFields) { try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } final int num = (int) (Math.random() * 10); //生成隨機(jī)數(shù)   EventQueue.invokeLater(new Runnable() { public void run() { nf.setText(num + ""); } }); } } } //停止線程的方法  public void stopLottery() { this.stop = true; } } // 開始按鈕的事件處理方法  protected void do_startButton_actionPerformed(final ActionEvent e) { if(randomThread!=null) //如果存在上一個線程對象  randomThread.stopLottery(); //停止它  randomThread=new RandomNum(); //創(chuàng)建新的線程對象  randomThread.start(); //啟動線程  } //抽獎按鈕的事件處理方法  protected void do_lotteryButton_actionPerformed(final ActionEvent e) { if(randomThread!=null) //如果存在線程對象  randomThread.stopLottery(); //停止它  try { randomThread.join(); //等抽獎線程結(jié)束  } catch (InterruptedException e1) { e1.printStackTrace(); } EventQueue.invokeLater(new Runnable() { //在事件隊列中更新抽獎信息  public void run() { String code = ""; //抽獎代碼字符串  for (final NumField nf : numFields) { //遍歷數(shù)字文本框  code += nf.getText(); //連接5個數(shù)字字符   } infoField.setText(code); //更新抽獎信息文本框   } }); } // 退出按鈕的事件處理方法  protected void do_exitButton_actionPerformed(final ActionEvent e) { System.exit(0); //退出程序   } }
                            總結(jié)
以上是生活随笔為你收集整理的随机抽奖小程序_在线随机抽号小程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 推荐三款免费可外链网络相册「建议收藏」(
- 下一篇: php中如何除法取整
