java實現(xiàn)隨機數(shù)生成器
更新時間:2018年12月20日 17:10:05 作者:Elon''''
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)隨機數(shù)生成器,隨機數(shù)生成小程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了java實現(xiàn)隨機數(shù)生成器的具體代碼,供大家參考,具體內(nèi)容如下
自己編的隨機數(shù)生成器,比較簡陋,功能也單一,當(dāng)作練手。
Application.java
package src; public class Application { public static void main(String[] args) { WindowBoxLayout win=new WindowBoxLayout(); win.setBounds(100,200,300,350); win.setTitle("隨機數(shù)生成器"); win.setVisible(true); } }
RandomListener.java
package src; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.*; public class RandomListener implements ActionListener{ JTextField text1; JTextArea text2; Random random=new Random(); int result=0; public void setJTextField(JTextField t){ text1=t; } public void setJTextArea(JTextArea t){ text2=t; } @Override public void actionPerformed(ActionEvent e) { try{ text2.setText(""); Integer number=Integer.parseInt(text1.getText()); result=random.nextInt(number)+1; text2.append(result+""); } catch(Exception exp){ text2.append("請輸入數(shù)字字符"); } } }
Windowboxlayout.java
package src; import javax.swing.*; public class WindowBoxLayout extends JFrame{ /** * */ private static final long serialVersionUID = 1L; Box baseBox; JTextField text1; JTextArea text2; JButton button; RandomListener random; public WindowBoxLayout(){ setLayout(new java.awt.FlowLayout()); init(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void init(){ baseBox=Box.createVerticalBox(); baseBox.add(new JLabel("請輸入隨機數(shù)的最大范圍值:")); baseBox.add(Box.createVerticalStrut(8)); text1=new JTextField(5); baseBox.add(text1); baseBox.add(Box.createVerticalStrut(8)); button=new JButton("確定"); baseBox.add(Box.createVerticalStrut(8)); baseBox.add(button); text2=new JTextArea(6,12); baseBox.add(Box.createVerticalStrut(18)); baseBox.add(text2); add(baseBox); random=new RandomListener(); random.setJTextField(text1); random.setJTextArea(text2); button.addActionListener(random); } }
類結(jié)構(gòu)圖:
效果圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中IdentityHashMap與HashMap區(qū)別詳解
這篇文章主要介紹了Java中IdentityHashMap與HashMap區(qū)別詳解,很多人不曉得IdentityHashMap的存在,其中不乏工作很多年的Java開發(fā)者,他們看到就說是第三方j(luò)ar包,實際上它是Jdk源碼自帶的集合類,需要的朋友可以參考下2023-11-11Spring Boot中Redis數(shù)據(jù)庫的使用實例
Spring Boot中除了對常用的關(guān)系型數(shù)據(jù)庫提供了優(yōu)秀的自動化支持之外,對于很多NoSQL數(shù)據(jù)庫一樣提供了自動化配置的支持。本篇文章主要介紹了Spring Boot中Redis的使用實例代碼,有興趣的開業(yè)了解一下。2017-04-04Java中的Set、List、Map的用法與區(qū)別介紹
這篇文章主要介紹了Java中的Set、List、Map的用法與區(qū)別,需要的朋友可以參考下2016-06-06Spring中@Validated和@Valid區(qū)別淺析
@Valid是javax.validation里的,?@Validated是@Valid?的一次封裝,是Spring提供的校驗機制使用,下面這篇文章主要給大家介紹了關(guān)于Spring中@Validated和@Valid區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-04-04