做了一个随机抽签的小Demo,(我们宿舍用它抽取谁打扫卫生);分享源码,JAVA基础班菜鸟一名,才刚刚入门,有不足之处请指出- import java.awt.BorderLayout;
- import java.awt.Button;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Frame;
- import java.awt.Label;
- import java.awt.Panel;
- import java.awt.TextArea;
- import java.awt.TextField;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.io.BufferedWriter;
- import java.io.ByteArrayOutputStream;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Random;
- import java.util.Scanner;
- public class Test1 extends Frame {
- private BufferedWriter bw; //带缓冲区的输出流
- Panel p = new Panel(); //Panel组件放置添加文本框和按钮
- private Button add; //添加按钮
- private Button start; //开始按钮
- private Button stop; //停止按钮
- private Boolean flag = false; //标识符
- private TextField tf; //添加文本框
- private Label l; //Label组件用来显示
-
- private Test1() throws IOException{
-
- init(); //初始化
- panel1(); //Panel组件放置添加文本框和按钮
- label(); //Label组件,用来显示
- listener(); //监听方法
- }
-
- private void listener() {
- this.addWindowListener(new WindowAdapter() { //关闭窗口
- @Override
- public void windowClosing(WindowEvent e) {
- // TODO Auto-generated method stub
- System.exit(0);
- }
- });
- add.addActionListener(new ActionListener() { //添加按钮监听
-
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- add();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
-
- }
- });
- start.addActionListener(new ActionListener() { //开始按钮监听
-
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- new start().start(); //实例并开始线程
- flag=true; //按下开始按钮,标识符为真
- } catch (Exception e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
-
- }
- });
- stop.addActionListener(new ActionListener() { //结束按钮监听
-
- @Override
- public void actionPerformed(ActionEvent e) {
- flag=false; //标识符为假
-
- }
- });
- }
-
- private void add() throws IOException {
- String str = tf.getText(); //获取TextField
- bw.write(str+",");
- bw.flush();
-
- }
- private class start extends Thread{
- public void run(){
- FileInputStream fis = null;
- try {
- fis = new FileInputStream("name.txt"); //打开输入流
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- ByteArrayOutputStream baos = new ByteArrayOutputStream(); //创建一个带缓冲区的字节输出流
-
- byte[] arr = new byte[1024] ;
-
- int len;
- try {
- while((len=fis.read(arr))!=-1){ //将字节读取并存在byte数组
- baos.write(arr); //将字节数组中的字节存到缓冲区
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- String str = baos.toString(); //将缓冲区中的字节转换成字符串,并定义一个字符串接收
- String[] name = str.split(","); //用","分割字符串并存在一个字符串数组里
- int index = 0; //定义一个索引
- Random r = new Random(); //实例随机数
- while(flag==true){ //While循环当标识符为真开始循环
- index=r.nextInt(name.length-1); //随机数在0~数组长度-1并赋值给索引
- l.setText(name[index]); //设置Label组件的文本
- }
- }
- }
- private void panel1() {
- Panel p=new Panel();
- tf = new TextField(15); //实例一个TextField文本框
- add = new Button("添加"); //实例一个添加按钮;
- start = new Button("开始");//实例开始按钮
- stop = new Button("停止");//实例停止按钮
- p.add(tf); //往Panel中添加控件
- p.add(start); //往Panel中添加控件
- p.add(stop); //往Panel中添加控件
- p.add(add); //往Panel中添加控件
- this.add(p,BorderLayout.SOUTH);
- }
- public void label() {
- l = new Label("谁是幸运星"); //Label组件
- l.setForeground(Color.RED);
- l.setBackground(Color.YELLOW);
- l.setFont(new Font("xxx", Font.PLAIN, 30));
- this.add(l);
- //p.add(view1,BorderLayout.CENTER);
- //p.add(view2,BorderLayout.SOUTH);
- //this.add(p);
-
- }
- public void init() throws IOException {//初始化
- this.setVisible(true);
- this.setSize(500, 600);
- this.setLocation(500, 100);
- bw = new BufferedWriter(new FileWriter("name.txt",true)); //打开输出流
- }
- public static void main(String[] args) throws IOException {
- new Test1();
- }
-
- }
复制代码
|
|