随机点名小程序--- -JAVA版本
Posted Randlly
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了随机点名小程序--- -JAVA版本相关的知识,希望对你有一定的参考价值。
话不多少,直接上代码
一个能够直接运行的随机点名的小程序,一个界面化的小程序。望广大网友多多支持!
1.创建一个随机点名的类
1 public class ProcessRandomName { 2 3 JFrame rFrame; 4 JLabel name; 5 JButton btn; 6 Random rd; 7 8 ArrayList<String> nameList = new ArrayList<>(); 9 10 public ProcessRandomName() { 11 rFrame = new JFrame("随机点名"); 12 name = new JLabel(); 13 btn = new JButton("开始点名"); 14 rd = new Random(); 15 } 16 17 public void init() { 18 SetName(nameList); 19 JLabel jLabel = new JLabel("随机点名器"); 20 jLabel.setHorizontalAlignment(0); 21 jLabel.setFont(new Font("开始点名了", 0, 35)); 22 23 name.setHorizontalAlignment(0); 24 btn.addActionListener(new ActionListener() { 25 26 final ProcessRandomName this$0; 27 28 public void actionPerformed(ActionEvent paramActionEvent) { 29 String curindex = getRandomName(); 30 name.setText(curindex); 31 name.setFont(new Font(curindex, 0, 35)); 32 name.setForeground(Color.red); 33 } 34 35 { 36 this$0 = ProcessRandomName.this; 37 // super(); 38 } 39 }); 40 41 Container p = rFrame.getContentPane(); 42 p.setLayout(new BorderLayout(3, 1)); 43 p.add(jLabel, "North"); 44 p.add(name, "Center"); 45 p.add(btn, "South"); 46 // p.setBackground(Color.black); 47 48 rFrame.pack(); 49 rFrame.setVisible(true); 50 rFrame.setSize(300,300); 51 rFrame.setDefaultCloseOperation(0); 52 rFrame.setResizable(true); 53 rFrame.setLocationRelativeTo(null); 54 rFrame.addWindowListener(new WindowListener() { 55 56 @Override 57 public void windowOpened(WindowEvent paramWindowEvent) { 58 paramWindowEvent.getComponent(); 59 rFrame.isShowing(); 60 } 61 62 @Override 63 public void windowIconified(WindowEvent paramWindowEvent) { 64 // TODO Auto-generated method stub 65 66 } 67 68 @Override 69 public void windowDeiconified(WindowEvent paramWindowEvent) { 70 // TODO Auto-generated method stub 71 72 } 73 74 @Override 75 public void windowDeactivated(WindowEvent paramWindowEvent) { 76 // TODO Auto-generated method stub 77 78 } 79 80 @Override 81 public void windowClosing(WindowEvent paramWindowEvent) { 82 System.exit(0); 83 } 84 85 @Override 86 public void windowClosed(WindowEvent paramWindowEvent) { 87 // TODO Auto-generated method stub 88 89 } 90 91 @Override 92 public void windowActivated(WindowEvent paramWindowEvent) { 93 // TODO Auto-generated method stub 94 95 } 96 }); 97 } 98 99 public void SetName(ArrayList<String> nameList) { 100 nameList.add("张三"); 101 nameList.add("李四"); 102 nameList.add("王五"); 103 nameList.add("代雪"); 104 105 } 106 107 public String getRandomName() { 108 int index = 1; 109 index = rd.nextInt(nameList.size()); 110 String nameIndex= nameList.get(index); 111 return nameIndex; 112 } 113 }
2.创建一个运行的类
1 public static void main(String[] args) { 2 ProcessRandomName test = new ProcessRandomName(); 3 test.init(); 4 5 }
简单的做了以上两个步骤Ctrl+C,Ctrl+V就能创建一个随机点名的小程序,如果想要生成一个可运行的exe文件,请点击我!
以上是关于随机点名小程序--- -JAVA版本的主要内容,如果未能解决你的问题,请参考以下文章