吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 22196|回复: 34
收起左侧

[Java 转载] 自己写了个随机抽签器

  [复制链接]
meilon 发表于 2017-6-18 21:07
本帖最后由 meilon 于 2017-6-18 21:24 编辑

本来是老师想要个随机点名器, 上网上找了半天, 没一个满意的, 干脆自己捣腾着做了一个.
001.jpg

界面比较简单,不过使用还是比较方便的.

说明:
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, 下载次数: 1430)

贴上源码, 求大神指点
这是读取配置的
[Java] 纯文本查看 复制代码
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;
    }
}


这是主窗口
[Java] 纯文本查看 复制代码
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);
        }
        
}

点评

核心 Random()  发表于 2017-6-19 11:55

免费评分

参与人数 5吾爱币 +4 热心值 +5 收起 理由
万物皆空 + 1 + 1 用心讨论,共获提升!
雫Hao洋洋 + 1 热心回复!
womeiyoufeng + 1 + 1 谢谢@Thanks!
紫月幽冥灵 + 1 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
1997 + 1 + 1 时间真多

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

chinkout 发表于 2017-6-19 00:04
请问一下楼主如何使用?没有exe啊。。。
peterq521 发表于 2017-6-18 21:44
萌萌哒、merry 发表于 2017-6-18 21:41
大兄弟,作弊不是好同学啊

哈哈 没办法啊 企业抽奖一般都是暗箱操作啊 没办法 楼主有可以操作的吗
peterq521 发表于 2017-6-18 21:35
收到 感谢楼主 请问可以设置预先操作吗 就是设定一个人 抽奖时一定能抽中的
萌萌哒、merry 发表于 2017-6-18 21:41
peterq521 发表于 2017-6-18 21:35
收到 感谢楼主 请问可以设置预先操作吗 就是设定一个人 抽奖时一定能抽中的

大兄弟,作弊不是好同学啊
ttttyyyy 发表于 2017-6-18 21:42
谢谢楼主分享!下来学习!
头像被屏蔽
大象无形 发表于 2017-6-18 21:44
提示: 作者被禁止或删除 内容自动屏蔽
 楼主| meilon 发表于 2017-6-18 21:47
peterq521 发表于 2017-6-18 21:35
收到 感谢楼主 请问可以设置预先操作吗 就是设定一个人 抽奖时一定能抽中的

没设置这功能, 得改源码...
gao792636281 发表于 2017-6-18 22:11
谢谢楼主分享!下来学习!
womeiyoufeng 发表于 2017-6-18 22:27

谢谢楼主分享!
snowbird 发表于 2017-6-18 22:51
谢谢楼主提供,看一下。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-3-29 07:14

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表