随机数产生的程序

来源:互联网 发布:淘宝卖家提现怎样收费 编辑:程序博客网 时间:2024/04/25 03:30

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//继承JFrame类,并实现对应监听器接口

public class GoodLucky extends JFrame implements ActionListener{
 
 JTextField tf = new JTextField();
 JButton b1 = new JButton("开始");
 JButton b2 = new JButton("停止");
 boolean isGo = false;
//构造方法实现抽奖机基本图形界面
 public GoodLucky(){
  b1.setActionCommand("start");  
  JPanel p = new JPanel();
  p.add(b1);
  p.add(b2);
    
  b1.addActionListener(this);//注册监听器
  b2.addActionListener(this);//注册监听器
  b2.setEnabled(false);
//设置窗口关闭功能及鼠标指针形状等窗口的初始效果
  this.getContentPane().add(tf,"North");
  this.getContentPane().add(p,"South");
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setSize(300,200);
  this.setLocation(300,300);
  Cursor cu = new Cursor(Cursor.HAND_CURSOR);
  this.setCursor(cu);
  this.setVisible(true);
  tf.setText("welcome you!                  ");
  this.go();
  
 }
//实现36选7的循环抽奖方法 
 public void go(){
  while(true){
   if(isGo == true){         //标志位产生
    String s = "";
    for(int j = 1; j <= 7;j++){
     int i = (int)(Math.random() * 36) + 1;
     if(i < 10){
      s = s + "  0" + i;
     }else{
      s = s + "  " + i;
     }
     
    }
    tf.setText(s);
   }
//设置线程休眠,从而控制抽奖滚动速度   
   try{
    Thread.sleep(10);
   }catch(java.lang.InterruptedException e){
    e.printStackTrace();
   }
   
  }
  
 } 
 
//事件监听处理方法,使按钮单击生效  
 public void actionPerformed(ActionEvent e){
  String s = e.getActionCommand();  //获取与事件相关信息,以确定事件源
//控制开始随机滚动抽取数字
  if(s.equals("start")){
   isGo = true;  //控制标志位
   b1.setEnabled(false);
   b2.setEnabled(true);
  }
//控制停止滚动抽取数字  
  else{
   isGo = false;
   b2.setEnabled(false);
   b1.setEnabled(true);
  }
  
 }
 
//程序入口,main方法  
 public static void main(String[] args){
  new GoodLucky();  
 }
}

                      程序运行结果图

 

原创粉丝点击