产生任意区间的随机数 java
使用java产生任意区间的随机数,效果图如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
代码如下: package Random; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.WindowConstants; /** * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class NewJFrame extends javax.swing.JFrame { private JTextField jTextField1; private JButton jButton1; private JLabel jLabel3; private JLabel jLabel2; private JLabel jLabel1; private JTextField jTextField2; /** * Auto-generated main method to display this JFrame */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { NewJFrame inst = new NewJFrame(); inst.setLocationRelativeTo(null); inst.setVisible(true); } }); } public NewJFrame() { super(); initGUI(); } private void initGUI() { try { setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); getContentPane().setLayout(null); { jTextField1 = new JTextField(); getContentPane().add(jTextField1); jTextField1.setText("1"); jTextField1.setBounds(0, 38, 67, 24); } { jTextField2 = new JTextField(); getContentPane().add(jTextField2); jTextField2.setText("10"); jTextField2.setBounds(104, 38, 67, 24); } { jButton1 = new JButton(); getContentPane().add(jButton1); jButton1.setText("\u786e\u5b9a"); jButton1.setBounds(109, 71, 62, 24); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { //主要代码区 String lowNumber=jTextField1.getText(); String upNumber=jTextField2.getText(); int lownumber=Integer.parseInt(lowNumber); int upnumber=Integer.parseInt(upNumber); int ans=(int)((upnumber-lownumber)*Math.random()); //System.out.print(ans); ans=ans+lownumber; String Ans=""+ans; //NewJFrame ss=new NewJFrame(); jLabel1.setText(Ans); } }); jButton1.addComponentListener(new ComponentAdapter() { }); } { jLabel1 = new JLabel(); getContentPane().add(jLabel1); jLabel1.setText("0"); jLabel1.setBounds(186, 139, 120, 33); } { jLabel2 = new JLabel(); getContentPane().add(jLabel2); jLabel2.setText("\u8f93\u5165\u968f\u673a\u6570\u7684\u8303\u56f4"); jLabel2.setBounds(23, 15, 96, 17); } { jLabel3 = new JLabel(); getContentPane().add(jLabel3); jLabel3.setText("\u968f\u673a\u6570"); jLabel3.setBounds(162, 120, 61, 27); } pack(); this.setSize(349, 222); } catch (Exception e) { //add your error handling code here e.printStackTrace(); } } } |
Rememberautumn
2014年10月8日 21:48
好简单的程序,我都没脸见人了。。。
独行
2014年10月8日 22:38
博主加油!