java随机抽题系统_JAVA程序设计题六:随机抽奖系统

给一同学写的实践周作业,发现好久没有写博客了,于是就分享出来,造福学弟学妹。

课题描述

基于JAVA GUI编写一个简单的随机抽奖程序,具体实现功能如下:

1. 创建一个文本文件,其中存放若干条用户信息。

2. 通过单击“开始”按钮实现从文本中读取这些用户信息,并将读取的每一条用户信息以滚动的方式逐条循环地显示在图形用户界面上。

3. 在单击“暂停”按钮时实现暂停功能,单击“保存”按钮实现将当前用户界面上显示的用户信息写入一个指定的文件中。

实现代码/*

* 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);

}

}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值