[Java][Swing实现随机数生成器]

举报
John2021 发表于 2022/02/24 21:45:55 2022/02/24
【摘要】 前言以一个用Swing作为界面实现的随机数生成器作为案例,来熟悉Java的Swing相关知识点及使用方法。随机数采用变异系数公式生成。 需求分析 界面在界面的左边从上到下分别是显示10个随机数的文本框,平均值X,标准差SD,变异系数CV值界面背景色为默认10个随机数的文本框的背景色为默认平均值X的背景色为#FF8000标准差SD的背景色为#74DF00变异系数CV值的背景色为#00FF00...

前言

以一个用Swing作为界面实现的随机数生成器作为案例,来熟悉Java的Swing相关知识点及使用方法。随机数采用变异系数公式生成。

需求分析

界面

  • 在界面的左边从上到下分别是显示10个随机数的文本框,平均值X,标准差SD,变异系数CV值
  • 界面背景色为默认
  • 10个随机数的文本框的背景色为默认
  • 平均值X的背景色为#FF8000
  • 标准差SD的背景色为#74DF00
  • 变异系数CV值的背景色为#00FF00
  • 界面字体使用宋体加粗,大小19

数据

  • 10个随机数取值范围为:0.36000.3699
  • 变异系数取值范围为:0.71.5
  • 平均值X取值范围为小数点后5位
  • 标准差SD取值范围为小数点后9位
  • 变异系数CV值取值范围为小数点后2位,要加上百分号%
  • 平均值X公式为: X = i 1 n X i n \overline{X}=\frac{\sum\limits_{i-1}^nX_i}{n}
  • 标准差SD公式为: S D = i 1 n ( X i X ) 2 / ( n 1 ) SD=\sqrt{\sum\limits_{i-1}^n(X_i-{\overline{X}})^2/(n-1)}
  • 变异系数CV公式为: C V ( % ) = S D X × 100 % CV({\%})={\frac{SD}{\overline{X}}}\times100\%

工程环境

  • JDK1.8
  • IDEA

代码实现

  1. 新建一个Java工程,由于只用到本地包,不需要新建Maven工程

  1. 完成代码骨架并导包
    • 类App是启动类
    • 类Ran继承JFrame,实现界面显示及随机数生成,里面包含两个函数体,分别是:public void initUI()public boolean check()
    • public void initUI()用于设置显示在JFrame上的元素
    • public boolean check()用于进行数据计算并进行数据格式化
    • java.awt.Color;用于设置颜色
    • java.awt.Font;用于设置字体
    • java.text.NumberFormat;用于格式化数字
    • javax.swing.JFrame;用于设置JFrame窗体
    • javax.swing.JLabel;用于设置JLabel标签
    • javax.swing.JTextField;用于设置JTextField文本框
package com.huawei;

import java.awt.Color;
import java.awt.Font;
import java.text.NumberFormat;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class App
{
    public static void main(String[] args)
    {
        //todo
    }
}

class Ran extends JFrame
{
    //todo
    public void initUI()
    {
        //todo
    }
    
    public boolean check()
    {
        //todo
    }
}
  1. 在Ran类中定义变量
    • num1num10代表1-10个随机数
    • avg代表平均值
    • sqrt代表标准差
    • cv代表变异系数
double num1,num2,num3,num4,num5,num6,num7,num8,num9,num10;
        double avg,sqrt,cv;
  1. 建立13个JLabel标签和13个JTextField文本框用于显示数据
JLabel label1=new JLabel();
        JLabel label2=new JLabel();
        JLabel label3=new JLabel();
        JLabel label4=new JLabel();
        JLabel label5=new JLabel();
        JLabel label6=new JLabel();
        JLabel label7=new JLabel();
        JLabel label8=new JLabel();
        JLabel label9=new JLabel();
        JLabel label10=new JLabel();
        JLabel label11=new JLabel();
        JLabel label12=new JLabel();
        JLabel label13=new JLabel();
        JTextField textField1=new JTextField();
        JTextField textField2=new JTextField();
        JTextField textField3=new JTextField();
        JTextField textField4=new JTextField();
        JTextField textField5=new JTextField();
        JTextField textField6=new JTextField();
        JTextField textField7=new JTextField();
        JTextField textField8=new JTextField();
        JTextField textField9=new JTextField();
        JTextField textField10=new JTextField();
        JTextField textField11=new JTextField();
        JTextField textField12=new JTextField();
        JTextField textField13=new JTextField();
  1. 设置窗口以及标签文本框显示位置
    • 设置窗体大小为宽330,高480
    • 设置窗体标题为"Swing实现随机数生成器"
    • 设置窗体不可改变大小
    • 设置字体为宋体,加粗,大小为19
public void initUI()
        {
        this.setSize(330,480);
        this.setTitle("Swing实现随机数生成器");
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        /**
         * 未设置Layout时,Java默认为flowLayout布局的,设置为null即为清空布局管理器,
         * 之后添加组件,常常是设置组件左上角坐标相对于容器左上角(0,0)的x,y值来确定组件的位置,
         * 即使更改容器大小也不会改变位置。这种方式常常用于窗口大小固定的容器里。
         */
        this.setLayout(null);
        this.setResizable(false);
        Font font=new Font("宋体",Font.BOLD,19);
        
        label1.setBounds(15,10,40,40);
        label1.setFont(font);
        label1.setText("1:");
        this.add(label1);
        textField1.setBounds(60,15,90,27);
        textField1.setFont(font);
        textField1.setEditable(false);
        this.add(textField1);
        }
  1. 实现随机数生成及格式化
    • 如果check()返回true不再执行check()
    • 如果check()返回false则再次执行check(),知道true为止
    • check()为true后才将界面显示
double max=0.3699,min=0.3600;
        num1=(double)(Math.random()*(max-min)+min);
        num2=(double)(Math.random()*(max-min)+min);
        num3=(double)(Math.random()*(max-min)+min);
        num4=(double)(Math.random()*(max-min)+min);
        num5=(double)(Math.random()*(max-min)+min);
        num6=(double)(Math.random()*(max-min)+min);
        num7=(double)(Math.random()*(max-min)+min);
        num8=(double)(Math.random()*(max-min)+min);
        num9=(double)(Math.random()*(max-min)+min);
        num10=(double)(Math.random()*(max-min)+min);
        
        NumberFormat numberFormat=NumberFormat.getNumberInstance();
        NumberFormat numberFormat1=NumberFormat.getNumberInstance();
        NumberFormat numberFormat2=NumberFormat.getNumberInstance();
        NumberFormat numberFormat5=NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(4);
        numberFormat1.setMaximumFractionDigits(9);
        numberFormat2.setMaximumFractionDigits(2);
        numberFormat5.setMaximumFractionDigits(5);
        num1=Double.parseDouble(numberFormat.format(num1));
        num2=Double.parseDouble(numberFormat.format(num2));
        num3=Double.parseDouble(numberFormat.format(num3));
        num4=Double.parseDouble(numberFormat.format(num4));
        num5=Double.parseDouble(numberFormat.format(num5));
        num6=Double.parseDouble(numberFormat.format(num6));
        num7=Double.parseDouble(numberFormat.format(num7));
        num8=Double.parseDouble(numberFormat.format(num8));
        num9=Double.parseDouble(numberFormat.format(num9));
        num10=Double.parseDouble(numberFormat.format(num10));

完整代码

package com.huawei;

import java.awt.Color;
import java.awt.Font;
import java.text.NumberFormat;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class App
{
    public static void main(String[] args)
    {
        Ran ran = new Ran();
        ran.initUI();
    }
}

class Ran extends JFrame
{
    double num1, num2, num3, num4, num5, num6, num7, num8, num9, num10;
    double avg, sqrt, cv;
    JLabel label1 = new JLabel();
    JLabel label2 = new JLabel();
    JLabel label3 = new JLabel();
    JLabel label4 = new JLabel();
    JLabel label5 = new JLabel();
    JLabel label6 = new JLabel();
    JLabel label7 = new JLabel();
    JLabel label8 = new JLabel();
    JLabel label9 = new JLabel();
    JLabel label10 = new JLabel();
    JLabel label11 = new JLabel();
    JLabel label12 = new JLabel();
    JLabel label13 = new JLabel();
    JTextField textField1 = new JTextField();
    JTextField textField2 = new JTextField();
    JTextField textField3 = new JTextField();
    JTextField textField4 = new JTextField();
    JTextField textField5 = new JTextField();
    JTextField textField6 = new JTextField();
    JTextField textField7 = new JTextField();
    JTextField textField8 = new JTextField();
    JTextField textField9 = new JTextField();
    JTextField textField10 = new JTextField();
    JTextField textField11 = new JTextField();
    JTextField textField12 = new JTextField();
    JTextField textField13 = new JTextField();
    
    public void initUI()
    {
        this.setSize(330, 480);
        this.setTitle("Swing实现随机数生成器");
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        /**
         * 未设置Layout时,Java默认为flowLayout布局的,设置为null即为清空布局管理器,
         * 之后添加组件,常常是设置组件左上角坐标相对于容器左上角(0,0)的x,y值来确定组件的位置,
         * 即使更改容器大小也不会改变位置。这种方式常常用于窗口大小固定的容器里。
         */
        this.setLayout(null);
        this.setResizable(false);
        Font font = new Font("宋体", Font.BOLD, 19);
        
        label1.setBounds(15, 10, 40, 40);
        label1.setFont(font);
        label1.setText("1:");
        this.add(label1);
        textField1.setBounds(60, 15, 90, 27);
        textField1.setFont(font);
        textField1.setEditable(false);
        this.add(textField1);
        
        label2.setBounds(15, 40, 40, 40);
        label2.setFont(font);
        label2.setText("2:");
        this.add(label2);
        textField2.setBounds(60, 45, 90, 27);
        textField2.setFont(font);
        textField2.setEditable(false);
        this.add(textField2);
        
        label3.setBounds(15, 70, 40, 40);
        label3.setFont(font);
        label3.setText("3:");
        this.add(label3);
        textField3.setBounds(60, 75, 90, 27);
        textField3.setFont(font);
        textField3.setEditable(false);
        this.add(textField3);
        
        label4.setBounds(15, 100, 40, 40);
        label4.setFont(font);
        label4.setText("4:");
        this.add(label4);
        textField4.setBounds(60, 105, 90, 27);
        textField4.setFont(font);
        textField4.setEditable(false);
        this.add(textField4);
        
        label5.setBounds(15, 130, 40, 40);
        label5.setFont(font);
        label5.setText("5:");
        this.add(label5);
        textField5.setBounds(60, 135, 90, 27);
        textField5.setFont(font);
        textField5.setEditable(false);
        this.add(textField5);
        
        label6.setBounds(15, 160, 40, 40);
        label6.setFont(font);
        label6.setText("6:");
        this.add(label6);
        textField6.setBounds(60, 165, 90, 27);
        textField6.setFont(font);
        textField6.setEditable(false);
        this.add(textField6);
        
        label7.setBounds(15, 190, 40, 40);
        label7.setFont(font);
        label7.setText("7:");
        this.add(label7);
        textField7.setBounds(60, 195, 90, 27);
        textField7.setFont(font);
        textField7.setEditable(false);
        this.add(textField7);
        
        label8.setBounds(15, 220, 40, 40);
        label8.setFont(font);
        label8.setText("8:");
        this.add(label8);
        textField8.setBounds(60, 225, 90, 27);
        textField8.setFont(font);
        textField8.setEditable(false);
        this.add(textField8);
        
        label9.setBounds(15, 250, 40, 40);
        label9.setFont(font);
        label9.setText("9:");
        this.add(label9);
        textField9.setBounds(60, 255, 90, 27);
        textField9.setFont(font);
        textField9.setEditable(false);
        this.add(textField9);
        
        label10.setBounds(15, 280, 45, 45);
        label10.setFont(font);
        label10.setText("10:");
        this.add(label10);
        textField10.setBounds(60, 285, 90, 27);
        textField10.setFont(font);
        textField10.setEditable(false);
        this.add(textField10);
        
        label11.setBounds(15, 330, 40, 40);
        label11.setFont(font);
        label11.setText("X:");
        this.add(label11);
        textField11.setBounds(60, 335, 90, 27);
        textField11.setFont(font);
        textField11.setBackground(Color.decode("#FF8000"));
        textField11.setEditable(false);
        this.add(textField11);
        
        label12.setBounds(15, 360, 45, 45);
        label12.setFont(font);
        label12.setText("SD:");
        this.add(label12);
        textField12.setBounds(60, 365, 140, 27);
        textField12.setFont(font);
        textField12.setBackground(Color.decode("#74DF00"));
        textField12.setEditable(false);
        this.add(textField12);
        
        label13.setBounds(15, 390, 45, 45);
        label13.setFont(font);
        label13.setText("CV:");
        this.add(label13);
        textField13.setBounds(60, 395, 90, 27);
        textField13.setFont(font);
        textField13.setBackground(Color.decode("#00FF00"));
        textField13.setEditable(false);
        this.add(textField13);
        
        if (check() == true)
        {
        } else
        {
            while (check() == false)
            {
                check();
            }
        }
        this.setVisible(true);
    }
    
    public boolean check()
    {
        double max = 0.3699, min = 0.3600;
        num1 = (double) (Math.random() * (max - min) + min);
        num2 = (double) (Math.random() * (max - min) + min);
        num3 = (double) (Math.random() * (max - min) + min);
        num4 = (double) (Math.random() * (max - min) + min);
        num5 = (double) (Math.random() * (max - min) + min);
        num6 = (double) (Math.random() * (max - min) + min);
        num7 = (double) (Math.random() * (max - min) + min);
        num8 = (double) (Math.random() * (max - min) + min);
        num9 = (double) (Math.random() * (max - min) + min);
        num10 = (double) (Math.random() * (max - min) + min);
        
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        NumberFormat numberFormat1 = NumberFormat.getNumberInstance();
        NumberFormat numberFormat2 = NumberFormat.getNumberInstance();
        NumberFormat numberFormat5 = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(4);
        numberFormat1.setMaximumFractionDigits(9);
        numberFormat2.setMaximumFractionDigits(2);
        numberFormat5.setMaximumFractionDigits(5);
        num1 = Double.parseDouble(numberFormat.format(num1));
        num2 = Double.parseDouble(numberFormat.format(num2));
        num3 = Double.parseDouble(numberFormat.format(num3));
        num4 = Double.parseDouble(numberFormat.format(num4));
        num5 = Double.parseDouble(numberFormat.format(num5));
        num6 = Double.parseDouble(numberFormat.format(num6));
        num7 = Double.parseDouble(numberFormat.format(num7));
        num8 = Double.parseDouble(numberFormat.format(num8));
        num9 = Double.parseDouble(numberFormat.format(num9));
        num10 = Double.parseDouble(numberFormat.format(num10));
        
        double[] totalNum = {num1, num2, num3, num4, num5, num6, num7, num8, num9, num10};
        double resultNum = 0;
        for (int i = 0; i < 10; i++)
        {
            resultNum += totalNum[i];
        }
        avg = resultNum / 10;
        avg = Double.parseDouble(numberFormat5.format(avg));
        double stdeva = 0;
        for (int i = 0; i < 10; i++)
        {
            stdeva += Math.pow(totalNum[i] - avg, 2);
        }
        sqrt = Math.sqrt(stdeva / (10 - 1));
        sqrt = Double.parseDouble(numberFormat1.format(sqrt));
        cv = (sqrt / avg) * 100;
        cv = Double.parseDouble(numberFormat2.format(cv));
        
        if (cv < 0.7 || cv > 1.5)
        {
            //System.out.println("CV= " + cv + " ***重新计算***");
            return false;
        } else
        {
           /* for (int i = 0; i < 10; i++)
            {
                System.out.println(totalNum[i] + "=====");
            }
            System.out.println("avg is : " + avg);
            System.out.println("sqrt is : " + sqrt);
            System.out.println("cv is :" + cv + "%");*/
            textField1.setText(String.valueOf(num1));
            textField2.setText(String.valueOf(num2));
            textField3.setText(String.valueOf(num3));
            textField4.setText(String.valueOf(num4));
            textField5.setText(String.valueOf(num5));
            textField6.setText(String.valueOf(num6));
            textField7.setText(String.valueOf(num7));
            textField8.setText(String.valueOf(num8));
            textField9.setText(String.valueOf(num9));
            textField10.setText(String.valueOf(num10));
            textField11.setText(String.valueOf(avg));
            textField12.setText(String.valueOf(sqrt));
            textField13.setText(String.valueOf(cv) + "%");
            return true;
        }
    }
}

打包生成jar文件

  1. 最终效果

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。