Java implements a simple random vertex name Generator

Source: Internet
Author: User

Java implements a simple random vertex name Generator
Let's 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 ("random vertex name"); // name String [] stuName = {"Yang Jun ", "Lu Hui", "rong peng", "Liu Jian peng", "Zhao Wei", "Lu Shi Rong", "Xu Qian", "Li pan", "Liao ruohui ", "Ma peng", "Cao Tianyu", "Li Yongcheng", "tong Xuming", "Guo Zhengyu", "Feng xiaole", "Wang Xinlu", "Wang Dan ", "Liu huilan", "He Fa Hai", "Liu Bai Yu"}; // The JLabel name used to store the name = new JLabel (); // button JButton btn = new JButton ("start to name"); // use a pseudo-random number. You can skip this, on the Internet, you can find the java Random number setting Random rd = new Random (); public void init () {// prompt label page JLabel jt = new JLabel ("random vertex"); // set the label center jt. setHorizontalAlignment (SwingConstants. CENTER); // set the font size. setFont (new java. awt. font ("random vertex name",); // you can specify the center name of the label displayed by name. setHorizontalAlignment (SwingConstants. CENTER); // listener for the Action button through the Anonymous class btn. addActionListener (new ActionListener () {@ Overridepublic void actionreceivmed (ActionEvent e) {// TODO Auto-generated method stub // obtain random name String n = getRandomName (); // set the name label text name. setText (n); // set the font name. setFont (new java. awt. font (n, 1, 35); // set the Font color name. setForeground (Color. red) ;}}); // obtain the JFrame panel Container p = this. rFrame. getContentPane (); // sets the layout mode. I use the BorderLayout layout p. setLayout (new BorderLayout (3, 1); // Add the prompt label in the north p. add (jt, BorderLayout. NORTH); // Add the name label in the center p. add (name, BorderLayout. CENTER); // Add the button control in south p. add (btn, BorderLayout. SOUTH); // adjust the size. This is the rFrame where labels cannot be set in java. pack (); // set the form size rFrame. setSize (300,300); // set to display rFrame. setVisible (true) ;}// obtain the random name public String getRandomName () {int a = 0; // when the random class implements a random number, only the upper limit can be set, that is to say, random numbers are all numbers a = rd between 0 and stuname.length. nextInt (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 ();}}

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.