JAVA做的随机点名小程序

2019-11-16 来源: Randlly 发布在  https://www.cnblogs.com/Randlly/p/11871794.html

话不多少,直接上代码

  一个能够直接运行的随机点名的小程序,一个界面化的小程序。望广大网友多多支持!

1.创建一个随机点名的类

  public class ProcessRandomName {

          JFrame rFrame;
          JLabel name;
          JButton btn;
          Random rd;

          ArrayList<String> nameList = new ArrayList<>();

          public ProcessRandomName() {
              rFrame = new JFrame("随机点名");
              name = new JLabel();
              btn = new JButton("开始点名");
              rd = new Random();
          }

          public void init() {
              SetName(nameList);
              JLabel jLabel = new JLabel("随机点名器");
              jLabel.setHorizontalAlignment(0);
              jLabel.setFont(new Font("开始点名了", 0, 35));

              name.setHorizontalAlignment(0);
              btn.addActionListener(new ActionListener() {

                  final ProcessRandomName this$0;

                  public void actionPerformed(ActionEvent paramActionEvent) {
                      String curindex = getRandomName();
                      name.setText(curindex);
                      name.setFont(new Font(curindex, 0, 35));
                      name.setForeground(Color.red);
                  }

                  {
                      this$0 = ProcessRandomName.this;
      //                super();
                  }
              });

              Container p = rFrame.getContentPane();
              p.setLayout(new BorderLayout(3, 1));
              p.add(jLabel, "North");
              p.add(name, "Center");
              p.add(btn, "South");
      //        p.setBackground(Color.black);

              rFrame.pack();
              rFrame.setVisible(true);
              rFrame.setSize(300,300);
              rFrame.setDefaultCloseOperation(0);
              rFrame.setResizable(true);
              rFrame.setLocationRelativeTo(null);
              rFrame.addWindowListener(new WindowListener() {

                  @Override
                  public void windowOpened(WindowEvent paramWindowEvent) {
                      paramWindowEvent.getComponent();
                      rFrame.isShowing();
                  }

                  @Override
                  public void windowIconified(WindowEvent paramWindowEvent) {
                      // TODO Auto-generated method stub

                  }

                  @Override
                  public void windowDeiconified(WindowEvent paramWindowEvent) {
                      // TODO Auto-generated method stub

                  }

                  @Override
                  public void windowDeactivated(WindowEvent paramWindowEvent) {
                      // TODO Auto-generated method stub

                  }

                  @Override
                  public void windowClosing(WindowEvent paramWindowEvent) {
                      System.exit(0);
                  }

                  @Override
                  public void windowClosed(WindowEvent paramWindowEvent) {
                      // TODO Auto-generated method stub

                  }

                  @Override
                  public void windowActivated(WindowEvent paramWindowEvent) {
                      // TODO Auto-generated method stub

                  }
              });
          }

          public void SetName(ArrayList<String> nameList) {
              nameList.add("张三");
              nameList.add("李四");
              nameList.add("王五");
              nameList.add("代雪");

          }

          public String getRandomName() {
              int index = 1;
              index = rd.nextInt(nameList.size());
              String nameIndex= nameList.get(index);
              return nameIndex;
          }
      }

2.创建一个运行的类

 public static void main(String[] args) {
         ProcessRandomName test =  new ProcessRandomName();
         test.init();

  }

简单的做了以上两个步骤Ctrl+C,Ctrl+V就能创建一个随机点名的小程序,如果想要生成一个可运行的exe文件,请点击我!

相关文章