用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();
}
}
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();
}
}
相关文章推荐
- 作业——java编写一个简单的移动方块游戏
- 猜数字游戏。编写一个Java应用程序,实现功能:
- 用java编写的一个简单的猜字母游戏
- 用java编写的一个小财富游戏
- java编写输出一个字符串中大写英文字母数、小写英文字母数以及非英文字母数
- java 做一个能在局域网内对战的游戏
- 【Java】编写一个方法,对字符串数组进行排序,将所有变位词排在相邻的位置
- 朋友写的一个中国象棋游戏,JAVA代码
- 编写一个JAVA的队列类
- JAVA与AS2制作一个2D多人游戏(3)
- 【好玩的棋盘游戏】编写一个玩家操作的函数和电脑能够操作的两人棋盘游戏,哪边有三个相连的胜出
- Java中编写一个完美的equals方法
- Step by Step,用JAVA做一个FLAPPYBIRD游戏(五)
- Java 游戏 连连看 语言编写 多媒体
- 用java编写一个程序,将浮点数转换成人民币读法字符串
- 编写一个程序求解字谜游戏
- 用java基础语言编写一个班级学生管理系统
- 一个使用JAVA编写的类似按键精灵的程序,支持脚本文件编写(含源代码)
- Java实现一个基于CLI的抽扑克牌游戏
- 用java编写一个简单的字符串加密解密程序,将字符串分成若干行,实现字符串以一列一列读取,并还原之前输入的字符串。这里实现4行输出。