[Java] 自己寫了個隨機抽籤器
阿新 • • 發佈:2018-12-03
上網上找了半天, 沒一個滿意的, 乾脆自己搗騰著做了一個.
介面比較簡單,不過使用還是比較方便的.
說明:
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
貼上原始碼, 求大神指點
這是讀取配置的
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); } }