功能简述:
1.可以选择本机上的名单进行抽奖,(名单需为txt文件,且文件中的名字需用空格隔开)
2.手动模式:手动控制开始与结束
3.自动模式:可以选择一/二/三等奖(人数可以自行设置)
4.可保存抽奖结果
界面:
//ExtractInterface.java 主界面
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class ExtractInterface extends JFrame{
JButton startButton;//开始
JButton endButton;//停止
JButton chooseButton;//选择文件
JButton setButton;//设置获奖人数
JLabel fileName;//文件名标签
JLabel peopleNum;//人数
JPanel showPriceJp;
JTextField showField;//显示姓名的文本框
JTextField AshowPrice1;//(自动)显示一等奖获奖名单
JTextField AshowPrice2;//(自动)显示二等奖获奖名单
JTextField AshowPrice3;//(自动)显示三等奖获奖名单
JTextField MshowPrice1;//(手动)显示获奖名单
JTextField MshowPrice2;
JTextField MshowPrice3;
JTextField showFile;//显示文件名的文本框
JTextField numField;//显示人数
JComboBox<String> modeBox;//模式选择
JMenuBar menuBar;//菜单栏
JMenu menu;//菜单
JMenuItem about;//关于
JMenuItem restart;//重新开始
JMenuItem savefile;//保存中奖名单
JRadioButton firstPrice;//一等奖
JRadioButton secondPrice;//二等奖
JRadioButton thirdPrice;//三等奖
JFileChooser fileDialog;//文件对话框
String[] nameData1;//存放名单的数组(手动)
String[] nameData2;
StringBuffer Mawardlist=new StringBuffer("");//存放中奖名单(手动)
StringBuffer Aawardlist1=new StringBuffer("");//存放一等奖名单(自动)
StringBuffer Aawardlist2=new StringBuffer("");//存放二等奖名单(自动)
StringBuffer Aawardlist3=new StringBuffer("");//存放三等奖名单(自动)
boolean isstart=false;//判断开始按钮是否已经按下
boolean AUTO_OR_MAN=false;//判断是什么模式 true:手动 false:自动
boolean isload=false;//判断是否已经载入文件
boolean isfp=false;//判断一等奖是否已经抽出
boolean issp=false;//判断二等奖是否已经抽出
boolean istp=false;//判断三等奖是否已经抽出
int restlength;//有效抽奖人数(手动)
int restlength2;//有效抽奖人数(自动)
int luckycount=0;//已中奖人数(手动)
Color co=new Color(245,219,175);//颜色
static int fnum=1;//一等奖人数
static int snum=2;//二等奖人数
static int tnum=3;//三等奖人数
File Afile=new File("list/AutoList.txt");//保存抽奖名单的文件(自动)
File Mfile=new File("list/ManList.txt");//保存抽奖名单的文件(手动)
BufferedWriter bw=null;
ExtractInterface(){
setLayout(null);//设置布局
Font f=new Font("宋体",Font.BOLD,40);//字体
Font f2=new Font("宋体",Font.BOLD,20);
Font f3=new Font("宋体",Font.BOLD,15);
//设置背景
BackGroundPanel bgp=new BackGroundPanel(new ImageIcon("images/background.png").getImage());
bgp.setBounds(0,0,700,800);
//菜单栏
menuBar=new JMenuBar();
menu=new JMenu("菜单");
restart=new JMenuItem("重新开始");
about=new JMenuItem("关于");
savefile=new JMenuItem("保存抽奖结果");
menu.add(restart);
menu.add(savefile);
menu.add(about);
menuBar.add(menu);
setJMenuBar(menuBar);
//模式
modeBox=new JComboBox<String>(new String[] {"自动抽奖","手动抽奖"});
modeBox.setBounds(20,20,100,30);
modeBox.setBackground(co);
bgp.add(modeBox);
//显示姓名的文本框
showField=new JTextField("");
showField.setBounds(218,105,297,179);//设置位置大小
showField.setEditable(false);//设置不可编辑
showField.setHorizontalAlignment(JTextField.CENTER);//文本居中
showField.setFont(f);
showField.setOpaque(false);
bgp.add(showField);
//获奖名单
AshowPrice1=new JTextField();
AshowPrice1.setBounds(76,629,594,44);
AshowPrice1.setEditable(false);//设置不可编辑
AshowPrice1.setHorizontalAlignment(JTextField.CENTER);//文本居中
AshowPrice1.setFont(f2);
AshowPrice1.setOpaque(false);
AshowPrice1.setBorder(BorderFactory.createEmptyBorder());//隐藏边框
AshowPrice2=new JTextField();
AshowPrice2.setBounds(76,673,594,43);
AshowPrice2.setEditable(false);//设置不可编辑
AshowPrice2.setHorizontalAlignment(JTextField.CENTER);//文本居中
AshowPrice2.setFont(f2);
AshowPrice2.setOpaque(false);
AshowPrice2.setBorder(BorderFactory.createEmptyBorder());//隐藏边框
AshowPrice3=new JTextField();
AshowPrice3.setBounds(76,716,594,46);
AshowPrice3.setEditable(false);//设置不可编辑
AshowPrice3.setHorizontalAlignment(JTextField.CENTER);//文本居中
AshowPrice3.setFont(f2);
AshowPrice3.setOpaque(false);
AshowPrice3.setBorder(BorderFactory.createEmptyBorder());//隐藏边框
MshowPrice1=new JTextField();
MshowPrice1.setBounds(76,629,594,44);
MshowPrice1.setEditable(false);//设置不可编辑
MshowPrice1.setHorizontalAlignment(JTextField.CENTER);//文本居中
MshowPrice1.setFont(f2);
MshowPrice1.setOpaque(false);
MshowPrice1.setBorder(BorderFactory.createEmptyBorder());//隐藏边框
MshowPrice2=new JTextField();
MshowPrice2.setBounds(76,673,594,43);
MshowPrice2.setEditable(false);//设置不可编辑
MshowPrice2.setHorizontalAlignment(JTextField.CENTER);//文本居中
MshowPrice2.setFont(f2);
MshowPrice2.setOpaque(false);
MshowPrice2.setBorder(BorderFactory.createEmptyBorder());//隐藏边框
MshowPrice3=new JTextField();
MshowPrice3.setBounds(76,716,594,46);
MshowPrice3.setEditable(false);//设置不可编辑
MshowPrice3.setHorizontalAlignment(JTextField.CENTER);//文本居中
MshowPrice3.setFont(f2);
MshowPrice3.setOpaque(false);
MshowPrice3.setBorder(BorderFactory.createEmptyBorder());//隐藏边框
bgp.add(AshowPrice1);
bgp.add(AshowPrice2);
bgp.add(AshowPrice3);
//从本机中读取文件
fileName=new JLabel("文件名:");
showFile=new JTextField();
chooseButton=new JButton("选择文件");
startButton=new JButton("开始");
endButton=new JButton("结束");
peopleNum=new JLabel("人数:");
numField=new JTextField();
fileName.setBounds(110,425,100,35);
showFile.setBounds(175,425,200,35);
chooseButton.setBounds(390,425,100,35);
peopleNum.setBounds(500,425,60,35);
numField.setBounds(540,425,60,35);
startButton.setBounds(333,530,100,50);
endButton.setBounds(400,508,100,50);
fileName.setFont(f3);
showFile.setFont(f3);
numField.setFont(f3);
chooseButton.setFont(f3);
startButton.setFont(f3);
endButton.setFont(f3);
startButton.setBackground(co);
endButton.setBackground(co);
chooseButton.setBackground(co);
//设置按钮
setButton=new JButton("设置人数");
setButton.setBounds(20,65,100,30);
setButton.setFont(f3);
setButton.setBackground(co);
bgp.add(setButton);
showFile.setEditable(false);//设置不可编辑
numField.setEditable(false);//设置不可编辑
bgp.add(fileName);
bgp.add(showFile);
bgp.add(chooseButton);
bgp.add(peopleNum);
bgp.add(numField);
bgp.add(startButton);
//奖项选择
firstPrice=new JRadioButton("一等奖");
secondPrice=new JRadioButton("二等奖");
thirdPrice=new JRadioButton("三等奖");
firstPrice.setBounds(233,470,100,50);
secondPrice.setBounds(333,470,100,50);
thirdPrice.setBounds(433,470,100,50);
firstPrice.setOpaque(false);//设置背景透明
secondPrice.setOpaque(false);
thirdPrice.setOpaque(false);
ButtonGroup group=new ButtonGroup();//使单选框互斥
group.add(firstPrice);
group.add(secondPrice);
group.add(thirdPrice);
bgp.add(firstPrice);
bgp.add(secondPrice);
bgp.add(thirdPrice);
//添加事件
restart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
showField.setText("");
AshowPrice1.setText("");
AshowPrice2.setText("");
AshowPrice3.setText("");
MshowPrice1.setText("");
MshowPrice2.setText("");
MshowPrice3.setText("");
numField.setText("");
showFile.setText("");
isstart=false;//判断开始按钮是否已经按下
isload=false;//判断是否已经载入文件
isfp=false;//判断一等奖是否已经抽出
issp=false;//判断二等奖是否已经抽出
istp=false;//判断三等奖是否已经抽出
luckycount=0;
fnum=1;
snum=2;
tnum=3;
Mawardlist=new StringBuffer("");
Aawardlist1=new StringBuffer("");
Aawardlist2=new StringBuffer("");
Aawardlist3=new StringBuffer("");
JOptionPane.showMessageDialog(bgp,"初始化完毕","提示",JOptionPane.INFORMATION_MESSAGE);
}
});
about.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(bgp,"\n1.txt文件内容要以空格隔开才能被正确读取"
+ "\n2.共有两个模式\n 手动模式:手动控制开始与结束\n 自动模式:可以选择一/二/三等奖(人数可以自行设置)"
+ "\n3.抽奖结果默认保存在本目录下的list/ManList.txt和list/AutoList.txt中 "
,"关于",JOptionPane.PLAIN_MESSAGE);
}
});
savefile.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(AUTO_OR_MAN) {//手动
if(Mawardlist.toString().equals(""))JOptionPane.showMessageDialog(bgp,"暂无抽奖结果!","提示",JOptionPane.WARNING_MESSAGE);
else {
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(Mfile)))) {
bw.write("获奖名单:");
bw.newLine();
String[] temp=Mawardlist.toString().split(" ");
for(int i=0;i<temp.length;++i) {
bw.write(temp[i]+" ");
if((i+1)%8==0)bw.newLine();
}
JOptionPane.showMessageDialog(bgp,"结果保存至list目录中","提示",JOptionPane.PLAIN_MESSAGE);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}else {//自动
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(Afile)))) {
if(!Aawardlist1.toString().equals("")||!Aawardlist2.toString().equals("")
||!Aawardlist3.toString().equals("")) {
bw.write("获奖名单:");
String[] temp;
if(!Aawardlist1.toString().equals("")) {
bw.newLine();
bw.write("一等奖:");
temp=Aawardlist1.toString().split(" ");
for(int i=0;i<temp.length;++i) bw.write(temp[i]+" ");
}
if(!Aawardlist2.toString().equals("")) {
bw.newLine();
bw.write("二等奖:");
temp=Aawardlist2.toString().split(" ");
for(int i=0;i<temp.length;++i) bw.write(temp[i]+" ");
}
if(!Aawardlist3.toString().equals("")) {
bw.newLine();
bw.write("三等奖:");
temp=Aawardlist3.toString().split(" ");
for(int i=0;i<temp.length;++i) bw.write(temp[i]+" ");
}
JOptionPane.showMessageDialog(bgp,"结果已保存至list目录中","提示",JOptionPane.PLAIN_MESSAGE);
}else JOptionPane.showMessageDialog(bgp,"暂无抽奖结果!","提示",JOptionPane.WARNING_MESSAGE);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
modeBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(modeBox.getSelectedItem().toString()=="自动抽奖") {
AUTO_OR_MAN=false;
showField.setText("");
if(bgp.isAncestorOf(endButton)) {
startButton.setBounds(333,530,100,50);
bgp.remove(endButton);
}
if(!bgp.isAncestorOf(firstPrice)) {
bgp.add(firstPrice);
bgp.add(secondPrice);
bgp.add(thirdPrice);
}
if(!bgp.isAncestorOf(setButton)) {
bgp.add(setButton);
}
if(!bgp.isAncestorOf(AshowPrice1)) {
bgp.add(AshowPrice1);
bgp.add(AshowPrice2);
bgp.add(AshowPrice3);
bgp.remove(MshowPrice1);
bgp.remove(MshowPrice2);
bgp.remove(MshowPrice3);
}
bgp.updateUI();
}else {//手动抽奖
AUTO_OR_MAN=true;
showField.setText("");
if(!bgp.isAncestorOf(endButton)) {
startButton.setBounds(230,508,100,50);
bgp.add(endButton);
}
if(bgp.isAncestorOf(firstPrice)) {
bgp.remove(firstPrice);
bgp.remove(secondPrice);
bgp.remove(thirdPrice);
}
if(bgp.isAncestorOf(setButton)) {
bgp.remove(setButton);
}
if(!bgp.isAncestorOf(MshowPrice1)) {
bgp.add(MshowPrice1);
bgp.add(MshowPrice2);
bgp.add(MshowPrice3);
bgp.remove(AshowPrice1);
bgp.remove(AshowPrice2);
bgp.remove(AshowPrice3);
}
bgp.updateUI();
}
}});
chooseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fileDialog=new JFileChooser();
fileDialog.setCurrentDirectory(new File("."));//设置默认打开目录为当前目录
fileDialog.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//只可以打开文件和文件夹
fileDialog.setMultiSelectionEnabled(false);//是否允许多选
int state=fileDialog.showOpenDialog(getContentPane());
if(state==JFileChooser.APPROVE_OPTION) {
File dir=fileDialog.getSelectedFile();
showFile.setText(dir.getName());//把文件名显示出来
try {//读文件
FileInputStream fr=new FileInputStream(dir.getPath());
InputStreamReader is = new InputStreamReader(fr, "UTF-8");
BufferedReader in=new BufferedReader(is);
nameData1=in.readLine().split(" ");
nameData2=nameData1.clone();
showField.setText("已准备就绪");
numField.setText(nameData1.length+"");
isload=true;
restlength=nameData1.length;
restlength2=restlength;
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
});
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(isload) {
if(!AUTO_OR_MAN) {//自动
new Thread(new Runnable() {
@Override
public void run() {
if(firstPrice.isSelected()&&!isfp) {
isfp=true;
for(int l=0;l<fnum;++l) {
int j = 0;
for(int i=0;i<20;++i) {
j=(int)(Math.random()*restlength2);
showField.setText(nameData2[j]);
try {
if(i<10)Thread.sleep(130);//快速轮换
else Thread.sleep(i*20);//逐渐减慢至停止
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(l==0)AshowPrice1.setText("一等奖:"+nameData2[j]);
else AshowPrice1.setText(AshowPrice1.getText()+" "+nameData2[j]);
Aawardlist1.append(nameData2[j]+" ");
nameData2[j]=nameData2[--restlength2];
try {
Thread.sleep(1600);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}else if(secondPrice.isSelected()&&!issp) {
issp=true;
for(int l=0;l<snum;++l) {
int j = 0;
for(int i=0;i<20;++i) {
j=(int)(Math.random()*restlength2);
showField.setText(nameData2[j]);
try {
if(i<10)Thread.sleep(130);//快速轮换
else Thread.sleep(i*20);//逐渐减慢至停止
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(l==0)AshowPrice2.setText("二等奖:"+nameData2[j]);
else AshowPrice2.setText(AshowPrice2.getText()+" "+nameData2[j]);
Aawardlist2.append(nameData2[j]+" ");
nameData2[j]=nameData2[--restlength2];
try {
Thread.sleep(1600);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}else if(thirdPrice.isSelected()&&!istp) {
istp=true;
for(int l=0;l<tnum;++l) {
int j = 0;
for(int i=0;i<20;++i) {
j=(int)(Math.random()*restlength2);
showField.setText(nameData2[j]);
try {
if(i<10)Thread.sleep(130);//快速轮换
else Thread.sleep(i*20);//逐渐减慢至停止
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(l==0)AshowPrice3.setText("三等奖:"+nameData2[j]);
else AshowPrice3.setText(AshowPrice3.getText()+" "+nameData2[j]);
Aawardlist3.append(nameData2[j]+" ");
nameData2[j]=nameData2[--restlength2];
try {
Thread.sleep(1600);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}else {
if(!firstPrice.isSelected()&&!secondPrice.isSelected()&&!thirdPrice.isSelected())JOptionPane.showMessageDialog(bgp,"请选择要抽取的奖项","提示",JOptionPane.WARNING_MESSAGE);
else JOptionPane.showMessageDialog(bgp,"该奖项已抽完","提示",JOptionPane.WARNING_MESSAGE);
}
}
}).start();
}else {//手动
if(!isstart&&restlength>0) {
isstart=true;
new Thread(new Runnable() {
@Override
public void run() {
int i=0;
while(isstart) {
if(i==restlength)i=0;
showField.setText(nameData1[i++]);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(luckycount/8==0)MshowPrice1.setText(MshowPrice1.getText()+" "+showField.getText());
else if(luckycount/8==1)MshowPrice2.setText(MshowPrice2.getText()+" "+showField.getText());
else MshowPrice3.setText(MshowPrice3.getText()+" "+showField.getText());
Mawardlist.append(nameData1[i-1]+" ");
nameData1[i-1]=nameData1[restlength-1];//把最后的那个提到已经被抽过的位置
restlength--;
luckycount++;
}
}).start();
}else {
if(restlength<=0)JOptionPane.showMessageDialog(bgp,"已抽完!","提示",JOptionPane.WARNING_MESSAGE);
else JOptionPane.showMessageDialog(bgp,"请先结束!","提示",JOptionPane.WARNING_MESSAGE);
}
}
}else JOptionPane.showMessageDialog(bgp,"请先载入文件","提示",JOptionPane.WARNING_MESSAGE);
}
});
endButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(isstart)isstart=false;
else JOptionPane.showMessageDialog(bgp,"请先开始","提示",JOptionPane.WARNING_MESSAGE);
}
});
setButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new AwardSetting(fnum,snum,tnum);
}
});
add(bgp);
setSize(700,856);
setLocation(600,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
}
public static void main(String[] args) {
new ExtractInterface();
}
}
//设置背景
class BackGroundPanel extends JPanel{
Image im;
BackGroundPanel(Image im){
setLayout(null);
this.im=im;
setOpaque(true);
}
public void paintComponent(Graphics g) {
super.paintComponents(g);
g.drawImage(im,0,0,this.getWidth(),this.getHeight(),this);
}
}
//AwardSetting.java 人数设置
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class AwardSetting extends JFrame{
JLabel flabel;
JLabel slabel;
JLabel tlabel;
JComboBox<Integer> fBox;//一等奖人数
JComboBox<Integer> sBox;//二等奖人数
JComboBox<Integer> tBox;//三等奖人数
JButton comButton;//确定按钮
AwardSetting(int fnum,int snum,int tnum){
setLayout(new FlowLayout(FlowLayout.CENTER,10,20));//布局
Font f=new Font("宋体",Font.BOLD,15);//字体
Color co=new Color(245,219,175);//颜色
flabel=new JLabel("一等奖:");
slabel=new JLabel("二等奖:");
tlabel=new JLabel("三等奖:");
fBox=new JComboBox<Integer>(new Integer[] {1,2,3,4,5,6,7,8});
sBox=new JComboBox<Integer>(new Integer[] {1,2,3,4,5,6,7,8});
tBox=new JComboBox<Integer>(new Integer[] {1,2,3,4,5,6,7,8});
comButton=new JButton("确定");
comButton.setPreferredSize(new Dimension(80,30));
comButton.setFont(f);
comButton.setBackground(co);
//设置默认值
fBox.setSelectedItem(fnum);
sBox.setSelectedItem(snum);
tBox.setSelectedItem(tnum);
//设置字体
flabel.setFont(f);
slabel.setFont(f);
tlabel.setFont(f);
//设置颜色
fBox.setBackground(co);
sBox.setBackground(co);
tBox.setBackground(co);
add(flabel);
add(fBox);
add(slabel);
add(sBox);
add(tlabel);
add(tBox);
add(comButton);
comButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int fnum=Integer.parseInt(fBox.getSelectedItem().toString());
int snum=Integer.parseInt(sBox.getSelectedItem().toString());
int tnum=Integer.parseInt(tBox.getSelectedItem().toString());
ExtractInterface.fnum=fnum;
ExtractInterface.snum=snum;
ExtractInterface.tnum=tnum;
dispose();
}
});
setSize(420,150);
setLocation(600,100);
setVisible(true);
setResizable(false);
}
}
整个程序的布局是空布局,全是通过坐标来设置各个组件的位置的,适合窗口大小固定的情况。
学Java没多久,代码质量还不是很高,有很多地方可以还是可以改进的。