java做抽奖系统怎么搞_JAVA 随机抽奖系统 实现教程及代码
基于JAVA GUI編寫一個簡單的隨機抽獎程序,具體實現功能如下:
1. 創建一個文本文件,其中存放若干條用戶信息。
2. 通過單擊“開始”按鈕實現從文本中讀取這些用戶信息,并將讀取的每一條用戶信息以滾動的方式逐條循環地顯示在圖形用戶界面上。
3. 在單擊“暫?!卑粹o時實現暫停功能,單擊“保存”按鈕實現將當前用戶界面上顯示的用戶信息寫入一個指定的文件中。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gui;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
/**
*
* @author ICHARM
*/
//按鈕狀態改變線程類
class ButtonChangeState extends Thread{
LuckyPerson one =null;
public ButtonChangeState(LuckyPerson tmp){
one = tmp;
}
@Override
public void run(){
one.ButtonChangeState();
}
}
//開始按鈕事件進程類
class StartButtonEvent extends Thread{
LuckyPerson one =null;
public StartButtonEvent(LuckyPerson tmp){
one = tmp;
}
@Override
public void run(){
one.StartButtonEvent();
}
}
//開始按鈕事件監聽器
class StartButtonEventListener implements ActionListener{
private LuckyPerson form = null;
public StartButtonEventListener(LuckyPerson One){
this.form = One;
}
@Override
public void actionPerformed(ActionEvent e){
ButtonChangeState obj = new ButtonChangeState(form);
obj.start();
StartButtonEvent obj1 = new StartButtonEvent(form);
obj1.start();
}
}
//停止按鈕事件監聽器
class StopButtonEventListener implements ActionListener{
private LuckyPerson form = null;
public StopButtonEventListener(LuckyPerson One){
this.form = One;
}
@Override
public void actionPerformed(ActionEvent e){
form.StopButtonEvent();
}
}
//保存按鈕事件監聽器
class SaveButtonEventListener implements ActionListener{
private LuckyPerson form = null;
public SaveButtonEventListener(LuckyPerson One){
this.form = One;
}
@Override
public void actionPerformed(ActionEvent e){
form.SaveButtonEvent();
}
}
public class LuckyPerson extends JFrame {
private JFrame frame;
private JLabel name;
public JTextArea area2;
private JButton startbutton;
private JButton stopbutton;
private JButton endbutton;
private JScrollPane AreaScrollPane;
private int count = 0;
private int i=0;
private int j=0;
public boolean stopflag = false;
String[] Name =null;
public LuckyPerson() {
frame=new JFrame("抽獎");
frame.setLayout(null);
name=new JLabel("中獎者");
name.setBounds(50, 50, 200, 20);
frame.getContentPane().add(name);
area2=new JTextArea();
area2.setBounds(200, 20, 300, 180);
area2.setEditable(false);
AreaScrollPane = new JScrollPane(area2);
AreaScrollPane.setBounds(200, 20, 300, 180);
AreaScrollPane.setVisible(true);
AreaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); //設置垂直滾動條總是顯示�
startbutton=new JButton("開始");
startbutton.setBounds(50, 220, 90, 30);
startbutton.addActionListener(new StartButtonEventListener(this)); //StatrtButton事件監聽器
frame.getContentPane().add(startbutton);
stopbutton=new JButton("暫停");
stopbutton.setBounds(220, 220, 90, 30);
stopbutton.addActionListener(new StopButtonEventListener(this)); //StopButton事件監聽器
stopbutton.setEnabled(false);
frame.getContentPane().add(stopbutton);
endbutton=new JButton("保存");
endbutton.setBounds(400, 220, 90, 30);
endbutton.setEnabled(false);
endbutton.addActionListener(new SaveButtonEventListener(this)); //SaveButton事件監聽器
frame.getContentPane().add(endbutton);
frame.setSize(555,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(AreaScrollPane, BorderLayout.EAST);
}
//按鈕狀態改變事件處理函數
public void ButtonChangeState(){
startbutton.setEnabled(false);
stopbutton.setEnabled(true);
endbutton.setEnabled(true);
}
//開始按鈕事件處理函數
public void StartButtonEvent(){
try{
FileReader fin = new FileReader("E://抽獎者名單.txt");
BufferedReader in = new BufferedReader(fin);
String line = in.readLine();
Name = new String[100];
while(line != null)
{
line = line.trim();
if(line.length()>0 && count<=100 )
{
Name[count] = line;
count++;
}
line = in.readLine();
}
area2.setText(Name[0]);
for(i = 1; i <= count+1; i++){
if(i == count){
i=0;
}
if(!stopflag){
area2.append("\n" + Name[i]);
name.setText("中獎者:\n"+Name[i]);
j=i;
area2.setSelectionStart(area2.getText().length()); //滾動條自動滾動到底端
area2.paintImmediately(area2.getBounds()); //刷新
}
}
in.close();
fin.close();
}
catch(IOException g)
{
System.out.println("IOException");
}
}
//停止按鈕事件處理函數
public void StopButtonEvent(){
stopflag = true;
}
//保存按鈕事件處理函數
public void SaveButtonEvent(){
if(stopflag){
try{
FileWriter fw = new FileWriter("E://tmp.txt",true);
BufferedWriter fww = new BufferedWriter(fw);
fww.write(Name[j]);
fww.close();
}
catch(IOException e){
System.out.println("IOException");
}
}
}
public static void main(String[] args) {
LuckyPerson application = new LuckyPerson();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
JAVA 隨機抽獎系統 實現教程及代碼
總結
以上是生活随笔為你收集整理的java做抽奖系统怎么搞_JAVA 随机抽奖系统 实现教程及代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: crtmpserver系列(二):搭建简
- 下一篇: 怎样刻录系统启动光盘