java實(shí)現(xiàn)隨機(jī)數(shù)生成器
本文實(shí)例為大家分享了java實(shí)現(xiàn)隨機(jī)數(shù)生成器的具體代碼,供大家參考,具體內(nèi)容如下
自己編的隨機(jī)數(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("隨機(jī)數(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("請輸入隨機(jī)數(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包,實(shí)際上它是Jdk源碼自帶的集合類,需要的朋友可以參考下2023-11-11Spring Boot中Redis數(shù)據(jù)庫的使用實(shí)例
Spring Boot中除了對常用的關(guān)系型數(shù)據(jù)庫提供了優(yōu)秀的自動(dòng)化支持之外,對于很多NoSQL數(shù)據(jù)庫一樣提供了自動(dòng)化配置的支持。本篇文章主要介紹了Spring Boot中Redis的使用實(shí)例代碼,有興趣的開業(yè)了解一下。2017-04-04Java中的Set、List、Map的用法與區(qū)別介紹
這篇文章主要介紹了Java中的Set、List、Map的用法與區(qū)別,需要的朋友可以參考下2016-06-06Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(34)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07Idea配置Maven阿里云鏡像加速的實(shí)現(xiàn)
這篇文章主要介紹了Idea配置Maven阿里云鏡像加速的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Spring中@Validated和@Valid區(qū)別淺析
@Valid是javax.validation里的,?@Validated是@Valid?的一次封裝,是Spring提供的校驗(yàn)機(jī)制使用,下面這篇文章主要給大家介紹了關(guān)于Spring中@Validated和@Valid區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-04-04Java Stopwatch類,性能與時(shí)間計(jì)時(shí)器案例詳解
這篇文章主要介紹了Java Stopwatch類,性能與時(shí)間計(jì)時(shí)器案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09