随便写的随机数生成器,目前并没有实现设置精度和去重功能

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javatest;

import java.util.Arrays;
import java.util.Random;

/**
 *
 * @author 17520
 */
public class GameFrame extends javax.swing.JFrame
{

    /**
     * Creates new form GameFrame
     */
    public GameFrame()
    {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents()
    {

        GamePanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        ResultPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ResultArea = new javax.swing.JTextArea();
        ResultArea.setLineWrap(true);
        ResultArea.setWrapStyleWord(true);
        RepeatCheckBox = new javax.swing.JCheckBox();
        AmountSpinner = new javax.swing.JSpinner();
        AmountSpinner.setModel(new javax.swing.SpinnerNumberModel(2, 2, 100, 1));
        MinSpinner = new javax.swing.JSpinner();
        MinSpinner.setModel(new javax.swing.SpinnerNumberModel(1, -1024, 1024,1));
        ExcuteSpinner = new javax.swing.JSpinner();
        ExcuteSpinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 5, 1));
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        MaxSpinner = new javax.swing.JSpinner();
        MaxSpinner.setModel(new javax.swing.SpinnerNumberModel(10, -1024, 1024,1));
        jLabel5 = new javax.swing.JLabel();
        GenButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Yosha's random generator");
        setBackground(new java.awt.Color(28, 27, 32));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        GamePanel.setBackground(new java.awt.Color(28, 27, 32));
        GamePanel.setForeground(new java.awt.Color(255, 255, 255));
        GamePanel.setFont(new java.awt.Font("雅黑宋体", 0, 14)); // NOI18N
        GamePanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("雅黑宋体", 1, 36)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(240, 240, 240));
        jLabel1.setText("随机数生成器");
        GamePanel.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 10, 240, 60));

        ResultPanel.setBackground(new java.awt.Color(41, 40, 45));
        ResultPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "结果序列", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("雅黑宋体", 0, 18), new java.awt.Color(240, 240, 240))); // NOI18N
        ResultPanel.setForeground(new java.awt.Color(255, 255, 255));
        ResultPanel.setAutoscrolls(true);
        ResultPanel.setFont(new java.awt.Font("微软雅黑", 0, 12)); // NOI18N
        ResultPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        ResultArea.setBackground(new java.awt.Color(41, 40, 45));
        ResultArea.setColumns(20);
        ResultArea.setFont(new java.awt.Font("雅黑宋体", 0, 18)); // NOI18N
        ResultArea.setForeground(new java.awt.Color(255, 255, 255));
        ResultArea.setRows(5);
        ResultArea.setCaretColor(new java.awt.Color(240, 240, 240));
        ResultArea.setDisabledTextColor(new java.awt.Color(240, 240, 240));
        jScrollPane1.setViewportView(ResultArea);

        ResultPanel.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 380, 220));

        GamePanel.add(ResultPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 200, 410, 270));

        RepeatCheckBox.setFont(new java.awt.Font("雅黑宋体", 0, 14)); // NOI18N
        RepeatCheckBox.setForeground(new java.awt.Color(255, 255, 255));
        RepeatCheckBox.setSelected(true);
        RepeatCheckBox.setText("可重复");
        GamePanel.add(RepeatCheckBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 320, -1, -1));

        AmountSpinner.setFont(new java.awt.Font("雅黑宋体", 0, 14)); // NOI18N
        AmountSpinner.setToolTipText("产生的个数");
        GamePanel.add(AmountSpinner, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 150, 50, -1));

        MinSpinner.setFont(new java.awt.Font("雅黑宋体", 0, 14)); // NOI18N
        MinSpinner.setToolTipText("最小值");
        GamePanel.add(MinSpinner, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 90, 50, -1));

        ExcuteSpinner.setFont(new java.awt.Font("雅黑宋体", 0, 14)); // NOI18N
        ExcuteSpinner.setToolTipText("精确到");
        GamePanel.add(ExcuteSpinner, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 150, 50, -1));

        jLabel2.setFont(new java.awt.Font("雅黑宋体", 0, 14)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(240, 240, 240));
        jLabel2.setText("产生个数");
        GamePanel.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 150, 60, 20));

        jLabel3.setFont(new java.awt.Font("雅黑宋体", 0, 14)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(240, 240, 240));
        jLabel3.setText("最小值");
        GamePanel.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 90, 50, 20));

        jLabel4.setFont(new java.awt.Font("雅黑宋体", 0, 14)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(240, 240, 240));
        jLabel4.setText("小数点后几位");
        GamePanel.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 150, 100, 20));

        MaxSpinner.setFont(new java.awt.Font("雅黑宋体", 0, 14)); // NOI18N
        MaxSpinner.setToolTipText("最大值");
        GamePanel.add(MaxSpinner, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 90, 50, -1));

        jLabel5.setFont(new java.awt.Font("雅黑宋体", 0, 14)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(240, 240, 240));
        jLabel5.setText("最大值");
        GamePanel.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 90, 50, 20));

        GenButton.setBackground(new java.awt.Color(71, 70, 76));
        GenButton.setFont(new java.awt.Font("雅黑宋体", 1, 18)); // NOI18N
        GenButton.setForeground(new java.awt.Color(255, 255, 255));
        GenButton.setText("生成");
        GenButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        GenButton.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                GenButtonMouseClicked(evt);
            }
        });
        GenButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                GenButtonActionPerformed(evt);
            }
        });
        GamePanel.add(GenButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 360, -1, 70));

        getContentPane().add(GamePanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 610, 480));

        pack();
    }// </editor-fold>                        

    private void GenButtonActionPerformed(java.awt.event.ActionEvent evt)                                          
    {                                              
        // TODO add your handling code here:
    }                                         

    private void GenButtonMouseClicked(java.awt.event.MouseEvent evt)                                       
    {                                           
        // TODO add your handling code here:
        int max=Integer.valueOf(String.valueOf(MaxSpinner.getValue()));
        int min=Integer.valueOf(String.valueOf(MinSpinner.getValue()));
        int amount=Integer.valueOf(String.valueOf(AmountSpinner.getValue()));
        
        int []result=new int[amount];
        for(int i=0;i<amount;i++)
        {
            Random random=new Random();
            result[i]=random.nextInt(max)%(max-min+1)+min;
        }
        
        ResultArea.setText(Arrays.toString(result));
    }                                      

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
            {
                if ("Nimbus".equals(info.getName()))
                {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex)
        {
            java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex)
        {
            java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex)
        {
            java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex)
        {
            java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new GameFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JSpinner AmountSpinner;
    private javax.swing.JSpinner ExcuteSpinner;
    private javax.swing.JPanel GamePanel;
    private javax.swing.JButton GenButton;
    private javax.swing.JSpinner MaxSpinner;
    private javax.swing.JSpinner MinSpinner;
    private javax.swing.JCheckBox RepeatCheckBox;
    private javax.swing.JTextArea ResultArea;
    private javax.swing.JPanel ResultPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration                   
}

  

 

posted @ 2016-08-16 12:57  Yosha  阅读(229)  评论(0编辑  收藏  举报