用到Java swing,thread线程,File文件操作,事件处理。
package GIFT;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
public class Window implements ActionListener {
JFrame window;
JPanel jPanel1,jPanel2,jPanel3;//面板
JLabel jLabel1,jLabel2,jLabel3,jLabel4;
JTextField name,count;//文本框,用于显示名字,奖项人数
JButton gift,openfile,savefile;//抽奖按钮和导入文件,保存文件
JComboBox jComboBox;//一二三等奖选择
boolean ifStart = true;
ArrayList<String> arrayList = new ArrayList<>(); //定义一个字符串链表
File file = new File("award.txt"); //中奖名单
//判断文件是否存在
public boolean isexist(File file)
{
boolean flag=false;
if(!file.exists())
{
return flag;
}
else {
flag=true;
return flag;
}
}
//如果文件存在,则删除文件重新创建文件
public void create(boolean flag)
{
if(flag)
{
file.delete();
}
file=new File("award.txt");
}
//构造函数
public Window()
{
//设置窗口参数
window=new JFrame("抽奖");
jPanel1=new JPanel();
jPanel2=new JPanel();
jPanel3=new JPanel();
//面板1,用于显示抽奖过程名字以及抽奖按钮
name=new JTextField(10);
name.setPreferredSize(new Dimension(200,80));
name.setEditable(false);//不可编辑
name.setOpaque(true); //边框
jLabel1=new JLabel("名字");
jLabel2=new JLabel("抽奖");
gift=new JButton("抽奖");
jPanel1.add(jLabel1);
jPanel1.add(name);
jPanel1.add(jLabel2);
jPanel1.add(gift);
//奖项选择
jComboBox=new JComboBox();
jComboBox.addItem("一等奖");
jComboBox.addItem("二等奖");
jComboBox.addItem("三等奖");
jLabel3=new JLabel("奖项选择");
jLabel4=new JLabel("人数设置");
count=new JTextField(10);
jPanel2.add(jLabel3);
jPanel2.add(jComboBox);
jPanel2.add(jLabel4);
jPanel2.add(count);
//导入导出文件
openfile=new JButton("导入抽奖名单");
savefile=new JButton("导出等奖名单");
jPanel3.add(openfile);
jPanel3.add(savefile);
window.add(jPanel1);
window.add(jPanel2);
window.add(jPanel3);
window.setLayout(new GridLayout(3,1)); //m行n列,布局
window.setBounds(400,200,600,500);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
//添加监听
gift.addActionListener(this);
openfile.addActionListener(this);
savefile.addActionListener(this);
//创建一个新文件保存获奖的名单
boolean flag=false;
flag=isexist(file);//判断文件是否存在
create(flag);//新建文件
}
public void actionPerformed(ActionEvent e){
Object object = e.getSource();
if(object == openfile){
fileOpen(); //导入文件
}
if(object == gift){
ifStart = true; //标志开始
new Thread(new Runnable() {
@Override
public void run() {
int time = 500; //设置一个抽奖时长
int i = 30; //设置一个开始sleep时长
int j = Integer.parseInt(count.getText());
while(j>0) {
//人数大于0
i = 25;
while (true) {
try {
if (!ifStart) {
synchronized (this) {
this.wait();
}
}
Random random = new Random();
if (arrayList.size() == 0) {
//用于判断是否有名单在链表中
break;
}
int rd = random.nextInt(arrayList.size());
name.setText(arrayList.get(rd)); //将随机产生的数当作链表的下标
if (i < time) {
Thread.sleep(i);
i += 25;
} else {
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//把文本框1的内容写进award.txt中
try {
String name1 = name.getText();
if (name1.trim().length() != 0 && arrayList.contains(name1)) {
String price = (String) jComboBox.getSelectedItem(); //获取当前数据项
FileWriter fileWriter = new FileWriter(file, true);
fileWriter.write(name1 + " " + price);
fileWriter.write("\r\n");
fileWriter.close();
Thread.sleep(2000);
}
arrayList.remove(name1);
} catch (IOException e3) {
System.out.println(e3);
}catch (InterruptedException e2){
e2.printStackTrace();
}
j--;
}
}
}).start();
}
if(object == savefile){
//显示award.txt中的内容,并创建一个窗口显示名单
JFrame jFrame = new JFrame();
JTextArea jTextArea = new JTextArea();
jTextArea.setEditable(false);
JScrollPane jScrollPane = new JScrollPane(jTextArea);
jFrame.add(jScrollPane,BorderLayout.CENTER);
jFrame.setTitle("中奖名单");
jFrame.setSize(300,350);
jFrame.setVisible(true);
jFrame.setLocation(250,200);
try{
FileReader inOne = new FileReader(file);
BufferedReader inTwo = new BufferedReader(inOne);
String s = null;
while((s = inTwo.readLine())!=null){
s = s.trim();
jTextArea.append(s + '\n');
}
}catch (IOException e1){
System.out.println(e1);
}
}
}
private void fileOpen(){
JFileChooser jFileChooser = new JFileChooser(new File("C:"));
FileNameExtensionFilter filter = new FileNameExtensionFilter("txt", "txt");
jFileChooser.setFileFilter(filter);
jFileChooser.setMultiSelectionEnabled(true);
jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
jFileChooser.showDialog(jFileChooser,"确定");
File[] file = jFileChooser.getSelectedFiles();
for(File s:file){
System.out.println(s);
}
//读取listname文件内进arrayList中
try{
FileReader inOne = new FileReader(file[0]);
BufferedReader inTwo = new BufferedReader(inOne);
String name = null;
while((name = inTwo.readLine()) != null){
name = name.trim();
arrayList.add(name);
}
}catch (IOException e){
System.out.println(e);
}
}
}
public static void main(String []args)
{
Window window=new Window();
}