如何用java编写一个花名随机抽取器

您所在的位置:网站首页 如何抽签名卡 如何用java编写一个花名随机抽取器

如何用java编写一个花名随机抽取器

2024-07-09 21:54| 来源: 网络整理| 查看: 265

2020博客之星年度总评选进行中:请为74号的狗子投上宝贵的一票! 我的投票地址:点击为我投票 在这里插入图片描述

文章目录 一.程序效果二.需要用到的包三.代码1.相关实例对象,所以对象均为全局对象2.建立窗体,并完成组件的初始化3.添加“打开文件”按钮监听事件:4.“关于”按钮监听事件5.下拉列表框选择监听事件6.“开始抽取”按钮监听事件7.如果成功打开文件并读取,文本框显示内容8.主方法9.资源下载

一.程序效果

在这里插入图片描述 还记得以前上课的时候,老师会用自己写的一个抽取器抽取同学回答问题,当时想着我也要做一个,假期没事干,自学了java,闲来无聊,我也写一个,但是写的没有老师好,哈哈,好了说一下思路,先把界面布置好,然后逐一实现每个按钮的功能,其实也没什么难的。

二.需要用到的包 import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.util.*; 三.代码 1.相关实例对象,所以对象均为全局对象 private static JLabel jl= new JLabel("文件:"); private static JTextField jt =new JTextField(); private static JButton OpenButton =new JButton("选择文件"); private static JTextField jt2 =new JTextField();//文本框 显示抽取名单 private static JButton StartButton =new JButton("开始抽取"); private static JLabel ClassjL= new JLabel("班级:"); private static JTextField ClassjT =new JTextField();//显示班级 private static JLabel NumjL= new JLabel("人数:"); private static JTextField NumjT =new JTextField();//显示人数 private static JLabel jl2= new JLabel("抽取模式:"); private static JComboBox jc= new JComboBox();//下拉列表框 private static JButton AboutButton =new JButton("关于"); private static JOptionPane jo =new JOptionPane();//弹出一个提示框 private static String[]s ;//用来存放人名 private static Font font = new Font("宋体",Font.BOLD,18); //设置字体对象 private static int Number=0;//用来存放抽取人数 2.建立窗体,并完成组件的初始化 private void windows() { JFrame jf =new JFrame ("花狗抽取器 本人博客:fdogcsdn.com"); jf.setIconImage(new ImageIcon("Icon.jpg").getImage()); Container c=jf.getContentPane(); c.setLayout(new GridLayout(4,2,10,10)); OpenButton.setFocusPainted(false); StartButton.setFocusPainted(false); AboutButton.setFocusPainted(false);//去掉按钮文字旁边的虚线框 JPanel jp1 =new JPanel(); JPanel jp2 =new JPanel(new BorderLayout()); JPanel jp3 =new JPanel(); JPanel jp4 =new JPanel();//添加面板 jt.setColumns(10); ClassjT.setColumns(6); NumjT.setColumns(4); jt2.setHorizontalAlignment(JTextField.CENTER); jc.addItem("--请选择--"); jc.addItem("抽取一人"); jc.addItem("抽取三人"); jc.addItem("抽取五人"); jp1.add(jl); jp1.add(jt); jp1.add(OpenButton); jp2.add(jt2,BorderLayout.CENTER); jp3.add(ClassjL); jp3.add(ClassjT); jp3.add(NumjL); jp3.add(NumjT); jp3.add(jl2); jp3.add(jc); jp4.add(StartButton); jp4.add(AboutButton); c.add(jp1); c.add(jp2); c.add(jp3); c.add(jp4); jf.setVisible(true); jf.setBounds(800, 200, 400, 500); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getOpenButton(); //下面三个方法是用来监听按钮事件方法 getAboutButton(); getSrartButton(); } 3.添加“打开文件”按钮监听事件: private void getOpenButton() { OpenButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser fc =new JFileChooser();//这个对象就是我们点击打开文件,出来的文件选择器 fc.setCurrentDirectory(new File("."));//指定当前默认目录 fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//可以选择只打开文件或者文件夹 fc.setMultiSelectionEnabled(false);//是否允许多选文件 int i =fc.showOpenDialog(getContentPane()); if(i==JFileChooser.APPROVE_OPTION) {//判断是否打开 File file =fc.getSelectedFile(); //显示选中内容 jt.setText(fc.getSelectedFile().getName()); try{ FileReader fr =new FileReader(file); BufferedReader in =new BufferedReader (fr); String line= in.readLine();//读取txt文件中的内容 s =line.split(" ");//以空格为分隔符,存储人名 NewMessage(); //监听事件 getjcomboBox();//监听事件 }catch(Exception e1) { e1.printStackTrace(); } } } }); } 4.“关于”按钮监听事件 private void getAboutButton() { AboutButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jo.showMessageDialog(null, "可建立txt文件:\n写入班级名字然后空格学生名字\n名字和名字之间必须要用空格隔开\n即可识别班级名称和人数以及名单"); } }); } 5.下拉列表框选择监听事件 private void getjcomboBox() { jc.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED) { String itemSize = (String) e.getItem(); if(itemSize=="抽取一人") { Number =1; } if(itemSize=="抽取三人") { Number =3; } if(itemSize=="抽取五人") { Number =5; } } } }); } 6.“开始抽取”按钮监听事件 private void getSrartButton() { StartButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { switch(Number) { case 1: int num1 =(int)1+(int)(Math.random()*(s.length-1-1)); jt2.setText(s[num1]); break; case 3: int []num3=new int[100]; for(int i=0;i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3