基于Javafx制作的随机抽签软件

基于Javafx制作的随机抽签软件

最近刚学习javafx,上英语课的时候刚看见英语老师用复古界面的抽签软件(用C语言写的)点名让同学回答问题,UI界面美观值真的是让人汗颜- -,在网上好像也搜不到用javafx做的抽签软件,swing写的倒是挺多的,觉得好玩自己便做了一个类似的抽签软件,希望对javafx感兴趣的新手有点帮助。

软件下载地址

网盘https://pan.baidu.com/s/1W0_KgdJXId2__gth16UzEQ [V1.0]
网盘: https://pan.baidu.com/s/16WFzyl0O_vUrdqorVKYb4A   [V1.2]
jar包(源码):链接:https://pan.baidu.com/s/1tvscoLZkO4X7VQu4G5QD1g 提取码:99s5

使用的知识

软件所使用的知识点其实是大多都是UI组件和属性绑定 ,时间相应,监听 ,ArrayList 容器,Iterator迭代器等都是基础的东西,不涉及ODBC数据库和服务器,代码量在1300行左右。

  • Timeline 用来实现名字随机动画的设置 播放

动画的定义和绑定

      //定义
     `EventHandler<ActionEvent>colors = e4->{
	      tf.setFill(Color.color((int)Math.random()*255,(int)Math.random()*255,(int)Math.random()*255));
	  };
		//绑定
		Timeline colorPlay = new Timeline(new KeyFrame(Duration.millis(300),colors));
				colorPlay.setCycleCount(Timeline.INDEFINITE);`.
  • 配置文件 (如果没有,则自己创建 ,Eclipse项目中 "save_Data.properties"表示文件在project工程目录下,"src\priv\Xiaoc\class\save_Data.properties"则在.class目录下)
 //保存用户配置
public static String save(String URL,ArrayList<String> waitInsertData,ArrayList<String> waitInsertDataValue) {
		String error_details ="保存成功!!";
		URL  = "save.properties";
		Properties saveFile = new Properties();
		try {
			FileOutputStream cin = new FileOutputStream(URL);
			for(int i = 0;i<waitInsertData.size();i++) {
				saveFile.setProperty(waitInsertData.get(i).toString(),waitInsertDataValue.get(i).toString()); //save relative Value
			}
			try {
				saveFile.store(cin, null);
				} catch (IOException e) {
					error_details = "读写成功,保存失败";
					}
			try {
				cin.close();
				} catch (IOException e) {
					error_details = "读写成功,保存成功 但关闭失败";
				}
			} catch (FileNotFoundException e) {
				error_details  = "文件读写失败";
				}
		return error_details;
	}
	
}
//加载用户配置 返回的是名字 和值(键值)的动态字符串数组
public static ArrayList<String>[] load(String URL){
		Properties loadFile = new Properties();
		Iterator<String> it,value0;
		ArrayList<String> names[] =  new ArrayList[2];
		for(int y=0;y<2;y++) {
			names[y] = new ArrayList<String>();
		}
		try {
			FileInputStream fis = new FileInputStream(URL);
			BufferedInputStream bfis = new BufferedInputStream(fis);
			InputStream is = bfis;
			try {
				
				loadFile.load(is);
				it = loadFile.stringPropertyNames().iterator();
				value0= loadFile.stringPropertyNames().iterator();
				while(it.hasNext()) {  //Get name
					names[0].add(it.next());
				}
				while(value0.hasNext()) { // Get Key Value
					names[1].add(loadFile.getProperty(value0.next()));
				}
				is.close();
				bfis.close();
				fis.close();
				return names;
			} catch (IOException e) {
				//System.out.println("加载失败");
				return null;
			}
		} catch (FileNotFoundException e) {
			//System.out.println("打开文件失败");
			
		}	
		return null;
	}
  • 读取文件所用到的输入流 输出流 I/O流
//URLFile 是ChooseFile文件选择框选择的文件 ,code是读取的时候的编码 ,默认是UTF-8,我的设置成GBK才不会乱码。。
public ArrayList getFileDetails(File URLFile) { // to get student name;
		ArrayList<String> temp = new ArrayList();
		FileInputStream cin;	
		try {
			InputStreamReader fr = new InputStreamReader(new FileInputStream(URLFile),code);
			BufferedReader bf = new BufferedReader(fr);
			String s= null;
			while((s = bf.readLine())!=null) {
				temp.add(s);   //读取每一行学生的名字 
			}
			bf.close();
			fr.close();
		}catch(Exception e) {};
		return temp;   //返回所有学生的名字
	}
  • 颜色选择器中的颜色十六进制值转RBG
//colorSelect 是颜色选择器实体化的对象
color_name_r = colorselect.getValue().getRed()*255;				//R
			color_name_g = colorselect.getValue().getGreen()*255;  //G
			color_name_b = colorselect.getValue().getBlue()*255; 	//B
			color_name = "rgb("+color_name_r+","+color_name_g+","+color_name_b+");";
			mainPane.setStyle("-fx-background-color:"+color_name);//css设置面板背景颜色
  • 多线程(默认是Javafx-Application Thread,如果这时我们要去干其他事情,那么原来的UI界面就会停止响应,界面就会奔溃,直到新任务Task完成才会回到原来的界面,这时我们就应该新建线程,这样就不会阻塞,并发性)
	// we start new Thread
		Thread ui = new Thread() {
				public void run() {
					ani2.play();
					try {
						for(int i = 0;i<stu.size();i++) {
							this.currentThread().sleep(30);
							show.appendText(stu.get(i).toString()+"\n");//一行一行的在文本域中添加学生名字,这样就看起来比较舒畅,而不是一打开文件就直接给你加载完毕,感觉有点快
						}
					}catch(Exception e) {
						tip_m2.settext(e.getMessage());}	
				} //run
			};
			ui.start();//新开的线程运行
  • 事件响应,比如Button,Check 等组件的响应
        clear.setOnAction(e->{             //清空文本按钮 点击时的处理事件
			if(end.getText().length() == 0) {
				dosclick.play();
				tip_m.setText("已经空啦");
			}
			else
				end.setText("");
		});
  • 事件的监听,当要求输入值得时候,我们往往会给TextFiled文本输入框添加监听事件,例如我们让用户在输入框只能输入数字,数字以外的符号包括字母不准输入,否则一旦输入则立即出现Alert之类的错误提示。
//cinExtract 是TextFiled的具体化的实例化对象
cinExtract.textProperty().addListener((observation,oldValueLast,newValueNow)->{
			try {
			if(cinExtract.getText().length()>0)
				for(int i = 0;i<cinExtract.getText().length();i++) {
					if(cinExtract.getText().toString().charAt(i)<'0'||
							cinExtract.getText().toString().charAt(i)>'9') {
						tip_m.setText("只能输入数字哦");
						cinExtract.setText("");
					}
				}
			else {
				cinExtract.setText("1");
			}
			if(cinExtract.getText().length()>0) //当学生人数为30人 ,但我们输入了35人,这时cinExtract就会取最大值30,自动改变
				if(Integer.parseInt(cinExtract.getText())>stu.size()) {
					tip_m.setText("错误 输入数据大于所具有的人数");
					cinExtract.setText(String.format("%d",stu.size()));//排版 SDF
					}
			
				}catch(Exception error) {
					tip.setContentText("输入有错误啦 请重新输入吧#_#");
					tip.showAndWait();
					};
			});
  • 关于Button数组问题
    如果一个软件需要用到很多很多按钮,那么设置个Button数组统一对他们操作会比较好点。比如
//一个个
	Button select = new Button("select");
	Button reselect = new Button("extract");
	Button clear = new Button("清空");
	Button recover = new Button("recover");
	Button color = new Button("color");
	Button cancel =new Button("pause");
	Button modify_code = new Button("code");
	Button colorPicker_b = new Button("colorPickerSelect");
//数组
	Button mainPane_button[] = new Button[8];
	for(int i = 0;i<8;i++){
		if(i == 0)
			mainPane_button[i] = new Button(name);
		else
			mainPane_button[i] = new Button(otherName);
}

在这里插入图片描述

在V1.2中,新增简单抽功能模块
在这里插入图片描述

可以在输入的学号范围内抽取,同时右边可以设置抽取速度。
在这里插入图片描述
具体效果:

在这里插入图片描述
:gif帧数低,所以号数闪现速度会慢很多

打包的软件需要依赖jre环境,因为用的是开源免费软件,所以打包好的软件在电脑上运行需要有JRE环境,不然将会打不开软件。

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值