您的位置:首页 > 编程语言 > Java开发

用Java编写的一个抽奖游戏

2010-07-21 20:46 302 查看
import java.util.GregorianCalendar;

public class Service {
 // 获得当前时间
 public String getDate() {
  GregorianCalendar gc = new GregorianCalendar();
  String strDate = gc.toString();
  int hourStart = strDate.indexOf("HOUR_OF_DAY=") + 12;
  int hourEnd = strDate.indexOf("MINUTE=") - 1;
  String hours = strDate.substring(hourStart, hourEnd);
  int scondStart = strDate.indexOf("SECOND=") + 7;
  int scondEnd = strDate.indexOf("MILLISECOND=") - 1;
  String scond = strDate.substring(scondStart, scondEnd);
  int minuteStart = strDate.indexOf("MINUTE=") + 7;
  int minuteEnd = strDate.indexOf("SECOND=") - 1;
  String minute = strDate.substring(minuteStart, minuteEnd);
  String time = "                               " + hours + ":" + minute
    + ":" + scond + "                                     ";
  return time;

 }
}

 

 

 

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.ldci.javaStudy.service.Service;

public class ViewFrame {
 JFrame frame = new JFrame("幸运大抽奖");

 JButton gText = new JButton("");

 JButton sText = new JButton("");

 JButton bText = new JButton("");

 JButton startBut = new JButton("开始");

 JButton stopBut = new JButton("停止");

 public JLabel lucklyWord = new JLabel("祝你好运!");

 JLabel win = new JLabel("恭喜你中的一等奖!");

 JLabel timeLabel = new JLabel("");

 JPanel panel = new JPanel();;

 boolean bWin = false;

 boolean bLucklyWord = true;

 public  boolean bl ;

 public ViewFrame() {
  win.setVisible(false);
  Thread1 thread1 = new Thread1();
  Thread2 thread2 = new Thread2();
  thread2.start();
  thread1.start();

 }

 // 初始化界面
 public void initView() {
  panel.add(gText);
  panel.add(sText);
  panel.add(bText);
  panel.add(startBut);
  panel.add(stopBut);
  panel.add(timeLabel);
  panel.add(lucklyWord);
  panel.add(win);
  
  frame.add(panel);
  frame.setSize(300, 150);
  frame.setVisible(true);
  startButListener startLis = new startButListener();
  startBut.addActionListener(startLis);
  stopButListener stopLis = new stopButListener();
  stopBut.addActionListener(stopLis);
 }

 // 开始键的事件监听
 public class startButListener implements ActionListener {

  public void actionPerformed(ActionEvent e) {
   bl = true;
   win.setVisible(false);
   lucklyWord.setVisible(true);
   

  }

 }

 // 停止键的事件监听
 public class stopButListener implements ActionListener {

  public void actionPerformed(ActionEvent e) {
   bl = false;
   if (gText.getText().equals("7") && sText.getText().equals("7")
     && bText.getText().equals("7")) {
    win.setVisible(true);
    lucklyWord.setVisible(false);
   }
  }

 }

 // 随机数之间的转换
 class Thread2 extends Thread {
  public void run() {

   Random random = new Random();
   for (;;) {
    if(bl) {
     String g = random.nextInt(10) + "";
     String s = random.nextInt(10) + "";
     String b = random.nextInt(10) + "";
     gText.setText(g);
     sText.setText(s);
     bText.setText(b);
    }
    try {
     sleep(40);
    } catch (InterruptedException e) {

     e.printStackTrace();
    }

   }
  }
 }

 // 闪动字体的实现与时间变化的实现
 class Thread1 extends Thread {
  public void run() {
   Service service = new Service();

   int i = 10;

   boolean b = false;
   for (;;) {
    if (b) {
     i--;
     if (i == 10)
      b = false;
    } else {
     i++;
     if (i == 50)
      b = true;
    }
    if (i % 2 == 0)
     lucklyWord.setVisible(false);
    else
     lucklyWord.setVisible(true);
    Font font = new Font("宋体", Font.BOLD, i);
    lucklyWord.setFont(font);
    win.setFont(font);
    timeLabel.setText(service.getDate());
    try {
     sleep(400);
    } catch (InterruptedException e) {

     e.printStackTrace();
    }
   }
  }
 }
}

 

 

 

 

public class Main {
 public static void main(String args[]) {
  ViewFrame viewFrame = new ViewFrame();
  viewFrame.initView();
 }
}

 

 

 

 

 

 

 

 

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息