Java implements a simple random name register

Source: Internet
Author: User

Nonsense not much to say that everyone can look at the code
Import Java.awt.borderlayout;import java.awt.color;import java.awt.container;import java.awt.event.ActionEvent; Import Java.awt.event.actionlistener;import java.util.random;import javax.swing.jbutton;import javax.swing.JFrame; Import Javax.swing.jlabel;import Javax.swing.swingconstants;public class Randomname {//main panel JFrame rframe=new JFrame ("with  Machine name device "); Name string[] stuname={"Yang Yi", "Lu Hui", "Rongpeng", "Liu Jianpeng", "Zhao Kun", "Ruthron", "Xu Yan", "GM", "Lio Jo", "Ma Peng", "Cao Tianyu", "Li Yongcheng", "Deng Xuming", "Guo reputation", "Feng Xiaolo", "Wang Lulu"  , "Wang Dan", "Liu Huilan", "He Fahai", "Liu Baiyu"};  The label used to store the name JLabel name = new JLabel ();  Button JButton btn = new JButton ("Start name");  The use of pseudo-random numbers, we can not use this, this can be found on the Web Java random number settings Random rd = new random ();  public void init () {//Hint label page JLabel jt= new JLabel ("Random name Register");  Set Label Center jt.sethorizontalalignment (Swingconstants.center);  Set the font size Jt.setfont (new Java.awt.Font ("Random name Register", 1,35));      Set the label of the name display centered name.sethorizontalalignment (swingconstants.center); The Listener Event Btn.addactionlistener (new ActionListener () {@Overridepublic of the action button is implemented through an anonymous classvoid actionperformed (ActionEvent e) {//TODO auto-generated method stub//Gets the random name String n=getrandomname ();    Sets the name label's text Name.settext (n);    Set Font Name.setfont (new Java.awt.Font (n,1,35));            Set the font color Name.setforeground (color.red);}      });  Gets the jframe panel Container p = This.rFrame.getContentPane ();  To set the layout way, I used the BorderLayout layout p.setlayout (new BorderLayout (3,1));  Add hint label in North P.add (Jt,borderlayout.north);  Add name tag in central p.add (Name,borderlayout.center);  Add a button control in the South P.add (Btn,borderlayout.south);  Resizing, this is the size of rframe.pack () that cannot be set in Java;  Set the form size rframe.setsize (300, 300);      Settings can display rframe.setvisible (true);  }//Get the random name Public String getrandomname () {int a = 0;  When the random class is to implement the stochastic number, it can only set the upper limit, that is, the random number is generated by the number A = Rd.nextint (stuname.length) between 0-stuname.length;  Rd.setseed ();    A = (int) math.random () *stuname.length;  return Stuname[a];  } public static void Main (string[] args) {randomname rn=new randomname ();  Rn.init (); }}

Java implements a simple random name register

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.