上网上找了半天, 没一个满意的, 干脆自己捣腾着做了一个.
界面比较简单,不过使用还是比较方便的.
说明:
document.txt 中添加/删除 抽签的条目, 每行一个条目,可输入数字作为随机抽奖器用, 也可输入人名当随机点名器用.
propert.ini 可修改配置
fontFamily : 设置字体
fontSize : 设置字体大小(20~100)
fontWeight : 设置字体粗细(0~1), 默认0
width : 设置窗口宽度(600~MAX), 默认600, MAX屏幕大小
height : 设置窗口高度(400~MAX), 默认400
amortize : 设置缓冲次数(1~30), 默认25
抽签器.zip (8.03 KB, 下载次数: 297)
贴上源码, 求大神指点
这是读取配置的
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
public class Setting {
//缓冲
private int amortize = 25;
//窗体宽度
private int width = 600;
//窗体高度
private int height = 400;
//字体大小
private int fontSize = 100;
//字体粗细
private int fontWeight = 0;
//字体
private String fontFamily = "黑体";
//屏幕宽
private int screenWidth;
//屏幕高
private int screenHeight;
Setting(){
File file = new File("propert.ini");
Properties props = new Properties();
//储存
if (!file.exists()) {
try {
props.put("width", "600");
props.put("height", "400");
props.put("fontSize", "100");
props.put("fontWeight", "0");
props.put("fontFamily", "黑体");
props.put("amortize", "25");
props.store(new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(file), "gbk")), null);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else {
//读取
try (BufferedReader br = new BufferedReader(new FileReader(file))){
props.load(br);
setFontFamily(props.getProperty("fontFamily").trim());
setFontSize(props.getProperty("fontSize").trim());
setFontWeight(props.getProperty("fontWeight").trim());
setWidth(props.getProperty("width").trim());
setHeight(props.getProperty("height").trim());
setAmortize(props.getProperty("amortize").trim());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public int getAmortize() {
return amortize;
}
public void setAmortize(String amortize) {
Integer am;
if (!amortize.matches("[0-9]*")) {
this.amortize = 1;
return;
}else{
am = new Integer(amortize);
if (am > 30 || am < 1) {
am = 30;
}
}
this.amortize = am;
}
public int getWidth() {
return width;
}
public void setWidth(String width) {
Integer wi;
if (width.equalsIgnoreCase("MAX")) {
this.width = getScreenWidth();
return;
}
if (!width.matches("[0-9]*")) {
this.width = 600;
return;
}else{
wi = new Integer(width);
if (wi > getScreenWidth()) {
wi = getScreenWidth();
}
if (wi < 600) {
wi = 600;
}
}
this.width = wi;
}
public int getHeight() {
return height;
}
public void setHeight(String height) {
Integer hei;
if (height.equalsIgnoreCase("MAX")) {
this.height = getScreenHeight();
return;
}
if (!height.matches("[0-9]*")) {
this.height = 400;
return;
}else{
hei = new Integer(height);
if (hei > getScreenHeight()) {
hei = getScreenHeight();
}
if (hei < 400) {
hei = 400;
}
}
this.height = hei;
}
public int getFontSize() {
return fontSize;
}
public void setFontSize(String fontSize) {
Integer fon;
if (!fontSize.matches("[0-9]*")) {
this.height = 20;
return;
}else{
fon = new Integer(fontSize);
if (fon < 20) {
fon = 20;
}
if (fon > 100) {
fon = 100;
}
}
this.fontSize = fon;
}
public int getFontWeight() {
return fontWeight;
}
public void setFontWeight(String fontWeight) {
int fw;
switch (fontWeight) {
case "0":
fw = 0;
break;
case "1":
fw = 1;
break;
default:
fw = 0;
break;
}
this.fontWeight = fw;
}
public String getFontFamily() {
return fontFamily;
}
public void setFontFamily(String font) {
this.fontFamily = font;
}
public void setScreenWidth(int screenWidth){
this.screenWidth=screenWidth;
}
public void setScreenHeight(int screenHeight){
this.screenHeight=screenHeight;
}
public int getScreenWidth(){
setScreenWidth((int)Toolkit.getDefaultToolkit().getScreenSize().width);
return screenWidth;
}
public int getScreenHeight(){
setScreenHeight((int)Toolkit.getDefaultToolkit().getScreenSize().height);
return screenHeight - 40;
}
}
这是主窗口
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class Frame_name extends JFrame{
Setting set = new Setting();
//缓冲
private int amortize = set.getAmortize();
//窗体宽度
private int width = set.getWidth();
//窗体高度
private int height = set.getHeight();
//字体大小
private int fontSize = set.getFontSize();
//字体粗细
private int fontWeight = set.getFontWeight();
//字体
private String fontFamily = set.getFontFamily();
JTextPane text = new JTextPane();
JButton but = new JButton("开始抽签");
JPanel jpUp = new JPanel();
JPanel jpCenter = new JPanel();
JPanel jpDown = new JPanel();
List<String> nameList =new ArrayList<String>();
//创建窗口
Frame_name(){
setTitle("随机抽签器");
//设置窗口位置
setLocation((set.getScreenWidth() - width) / 2,(set.getScreenHeight() - height) / 2);
//设置窗口大小
setSize(width,height);
//禁止调整窗口大小
setResizable(false);
//设置主窗口为绝对布局
setLayout(null);
add(jpUp);
add(jpCenter);
add(jpDown);
jpUp.setBounds(0,0,width,80);
jpUp.setLayout(new FlowLayout());
jpCenter.setBounds(0,80,width,height-200);
jpCenter.setLayout(new BorderLayout());
jpCenter.add(text,BorderLayout.CENTER);
//字体
text.setFont(new Font(fontFamily,fontWeight,fontSize));
//颜色
text.setForeground(Color.RED);
//文本居中
StyledDocument doc = text.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
//禁止操作文本域
text.setEditable(false);
//背景透明
text.setOpaque(false);
jpDown.setBounds(0,height-120,width,120);
jpDown.setLayout(new FlowLayout());
jpDown.add(but);
//隐藏按钮焦点框
but.setFocusPainted(false);
//按钮字体及大小
but.setFont(new java.awt.Font("微软雅黑",1,35));
//监听按钮事件
but.addActionListener((ActionEvent e) -> runTest());
//默认按X键关闭程序
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//激活窗口
setVisible(true);
}
//获取条目集合
public void name(){
File file = new File("document.txt");
if (!file.exists()) {
//如果文件不存在则自行创建
try (BufferedWriter fw = new BufferedWriter(new FileWriter(file))){
fw.write("001" + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}
//如果存在则读取
try (BufferedReader bufw = new BufferedReader(new FileReader(file))){
String name;
while ((name = (bufw.readLine()).trim()) != null ) {
nameList.add(name);
}
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
//显示条目
private void runTest(){
new Thread(() -> {
for (int i = 0; i < amortize; i++) {
try {
//设置条目标题文字
text.setText(randomName());
Thread.sleep(50 + i * (i/4));
} catch (InterruptedException ee) {
ee.printStackTrace();
}
}
}).start();
}
//随机抽取一个条目
private String randomName(){
Random r = new Random(System.currentTimeMillis());
int s = r.nextInt(nameList.size());
return nameList.get(s);
}
}