如何用java编写一个花名随机抽取器
【摘要】 2020博客之星年度总评选进行中:请为74号的狗子投上宝贵的一票! 我的投票地址:点击为我投票
文章目录
一.程序效果二.需要用到的包三.代码1.相关实例对象,所以对象均为全局对象2.建立窗体,并完成组件的初始化3.添加“打开文件”按钮监听事件:4.“关于”按钮监听事件5.下拉列表框选择监听事件6.“开始抽取”按钮监听事件7.如果成功打开文件并读取,文本框显...
2020博客之星年度总评选进行中:请为74号的狗子投上宝贵的一票!
我的投票地址:点击为我投票
一.程序效果
还记得以前上课的时候,老师会用自己写的一个抽取器抽取同学回答问题,当时想着我也要做一个,假期没事干,自学了java,闲来无聊,我也写一个,但是写的没有老师好,哈哈,好了说一下思路,先把界面布置好,然后逐一实现每个按钮的功能,其实也没什么难的。
二.需要用到的包
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
- 1
- 2
- 3
- 4
- 5
三.代码
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;//用来存放抽取人数
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
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(); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
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(); } } } }); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
4.“关于”按钮监听事件
private void getAboutButton() { AboutButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jo.showMessageDialog(null, "可建立txt文件:\n写入班级名字然后空格学生名字\n名字和名字之间必须要用空格隔开\n即可识别班级名称和人数以及名单"); } }); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
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; } } } }); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
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<100;i++) { num3[i]=(int)1+(int)(Math.random()*(s.length-1-1)); } for(int i=0;i<98;i++) { if(num3[i]!=num3[i+1] && num3[i]!=num3[i+2] && num3[i+1]!=num3[i+2]) { String strtext1= s[num3[i]]+" "+s[num3[i+1]]+" "+s[num3[i+2]]; jt2.setText(strtext1); break; } } break; case 5: int []num5=new int[100]; for(int i=0;i<100;i++) { num5[i]=(int)1+(int)(Math.random()*(s.length-1-1)); } for(int i=0;i<95;i++) { if(num5[i]!=num5[i+1] && num5[i]!=num5[i+2] && num5[i]!=num5[i+3] && num5[i]!=num5[i+4] && num5[i+1]!=num5[i+2] && num5[i+1]!=num5[i+3] && num5[i+1]!=num5[i+4] &&num5[i+2]!=num5[i+3] && num5[i+2]!=num5[i+4] &&num5[i+3]!=num5[i+4]) { String strtext1= s[num5[i]]+" "+s[num5[i+1]]+" "+s[num5[i+2]]+" "+s[num5[i+3]]+" "+s[num5[i+4]]; jt2.setText(strtext1); break; } } break; } } }); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
7.如果成功打开文件并读取,文本框显示内容
private void NewMessage() { ClassjT.setText(s[0]); String s1=""; s1=""+(s.length-1); NumjT.setText(s1); jt2.setFont(font); jt2.setForeground(Color.blue); jt2.setText("已就绪,请开始抽取。"); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
8.主方法
public static void main(String[] args) {
callmy call = new callmy();
call.windows();
}
- 1
- 2
- 3
- 4
9.资源下载
可以去我的资源下载相关源代码
若有错误,欢迎指正批评,欢迎评论;
每文一句:世间之事,有难有意,有成功也会有失败。事情能否成功,不在于事情的难易,而在于你是否付出努力。古往今来,但凡做事懒惰,不思进取之人,想要取得成功,根本就是天方夜谭而勤劳的人,付出艰辛努力,克服重重困难,往往能够取得成功。
文章来源: blog.csdn.net,作者:花狗Fdog,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/Fdog_/article/details/104651681
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)