[Java] 自己写了个随机抽签器
上网上找了半天, 没一个满意的, 干脆自己捣腾着做了一个.
界面比较简单,不过使用还是比较方便的.
说明:
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);}}
[Java] 自己写了个随机抽签器相关推荐
- java爬虫写一个百度图片下载器
文章目录 img_download 1.0 看看效果吧 2.0 了解一下 "图片下载器软件" 目录结构 3.0 如何使用? 4.0 源码剖析 5.0 项目地址 6.0 写在最后的话 ...
- 用Java swing写的一个音乐播放器的个性化界面实现作业(只有界面!)
这个是我们用户界面设计的作业. 要求是,可以换肤,个性化,皮肤半透明. 话不多说,先上图吧.第一个界面做得精致一些,第二个,没来得及好好改.但是原理就这样了. 接下来贴代码 这个是主界面 packag ...
- 用java制作一个简易抽签器
最近闲来无事,想用Java编写一个随机抽签器,最终编写后的运行结果是这样的 下面说一下怎么具体实现,非常简单. 首先,要想出现这个滚动标签,首先必须要有一个窗口来承载它,所以要在main()方法中创建 ...
- 用Java写一个电影自动下载器
你好! 下面是一些步骤来帮助你写一个电影自动下载器: 建立一个新的Java项目 选择一个电影下载网站作为数据源, 并使用网络爬虫或API来获取电影的信息(如标题, 时长, 格式, 大小等) 使用Jav ...
- java音乐播放器所需jar包,这个用Java写的开源音乐播放器,我粉了
原标题:这个用Java写的开源音乐播放器,我粉了 开源最前线(ID:OpenSourceTop) 随着版权意识的增强,如今想听几首歌,都得下载好几个音乐软件.这也就算了,大部分音乐还都是付费的,安装这 ...
- 作为一个才刚刚开始学习java的小白 居然显示码龄3年??每天吃饭点菜成为了一个难题 然后今天简单写了一个随机菜单
简单使用了一个ArrayList集合写了一个随机菜单 解决选择困难症 简单使用了一个ArrayList集合写了一个自定义菜单 直接上代码 代码 public static String getMeat ...
- 姓名抽签器c语言,在线抽签器
int main() { int a = 0; srand((unsigned)time(NULL)); a = rand()%40 + 1; /*1到40号中抽出一个*/ printf(" ...
- 《深入理解Java虚拟机》-----第3章 垃圾收集器与内存分配策略
Java与C++之间有一堵由内存动态分配和垃圾收集技术所围成的"高墙",墙外面的人想进去,墙里面的人却想出来. 3.1 概述 说起垃圾收集(Garbage Collection,G ...
- Java开发者写SQL时常犯的10个错误
首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 我要投稿 更多频道 » - 导航条 -首页所有文章资讯Web架构基础技术书籍教程我要投稿更多频道 »- iOS- Python- Androi ...
- Java如何解析markdown_使用Java实现的一款Markdown解析器md2x
使用Java实现的一款Markdown解析器md2x 前段时间在写自己的博客程序的时候,在前台使用了marked.js来解析自己的markdown文章,然后发现在进入文章页面的时候总会闪烁一下(前台解 ...
最新文章
- 无线通信频率分配表(详细)
- Kubernetes中部署Docker registry2.7.1并通过containerd实现拉取镜像到应用Pod的部署
- AI技术已达如此高度:去码、上色6到飞起
- android icon在线更新,Android在线更新下载方案
- 前端学习(2964):路由的实现
- 图管够!灌篮高手、女儿国…阿里日_这帮程序员太会玩了!
- 云+X案例展 | 金融类:七牛云Pandora 助阵某银行实现日志智能管理
- rocketmq学习杂记
- corspost请求失败_利用CORS实现POST方式跨域请求数据
- UML教程8:构件图 部署图 附录
- pop3 c语言,VisualC#编写实现POP3的程序
- 集成电路--封装种类
- 服务器IP被封的原因
- discuz论坛模板文件目录
- 安装win7纯净版系统时,提示缺少所需的CD/DVD驱动器设备驱动程序的解决方案,亲测有效
- VMware虚拟机ubuntu指定使用主机的wifi无线网卡
- 中国七夕情人节快到了2009
- Thingsboard 时序数据和属性数据
- (转)AndroidManifest 清单文件合并时出现 【quires a placeholder substitution but no value for is provided.】问题
- Spring Data JPA使用JPQL与原生SQL进行查询