jpanel是什么_不知道下一顿吃什么?让java抽签器来帮你搞定

一个比较简单的java随机抽签器

自己每天快到饭点的时候都在不知道吃什么不如来一个随机抽签器来帮自己做决定。

方便简单不用思考。

cad171e9a62000b820a590edbeb93c90.png

源码

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;

}

}

b13aeed273fc15bdb7b8b5262685cb20.png

主窗口

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 nameList =new ArrayList();

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());

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" + "");

} 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());

String[] num = {"盖浇饭","汤面", "寿司","炸鸡"};

If (nameList.size()> num.length()) {

return num[r.nextInt(nameList.size())];

} else {

return "";

}

}

}

26c2402ac699f9333c4bc4def086a5c3.png

欢迎大家评论区留言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值