实验要求:可以设置种子选手,可以导入或粘贴候选列表,使用Consumer接口

一、界面

插入的名单的数据格式(name+score)

1.初始界面

2.文件导入或粘贴名单后

3.开始分组

二、代码

package week8;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.function.Consumer;import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.FileChooser;
import javafx.stage.Stage;public class RandomGroup extends Application {public static class Candidate {private final SimpleIntegerProperty number;private final SimpleStringProperty name;private final SimpleIntegerProperty score;private final SimpleIntegerProperty groupNo;private Candidate(int number, String name, int score, int groupNo) {this.number = new SimpleIntegerProperty(number);this.name = new SimpleStringProperty(name);this.score = new SimpleIntegerProperty(score);this.groupNo = new SimpleIntegerProperty(groupNo);}public int getNumber() {return this.number.get();}public void setNumber(int number) {this.number.set(number);;}public String getName() {return this.name.get();}public void setName(String name) {this.name.set(name);}public int getScore() {return this.score.get();}public void setScore(int score) {this.score.set(score);;}public int getGroupNo() {return this.groupNo.get();}public void setGroupNo(int groupNo) {this.groupNo.set(groupNo);}@Overridepublic String toString() {return "Candidate [number =" + getNumber() + ",name =" + getName() + ",score = " + getScore()+ ",groupNo = " + getGroupNo() + "]";}}private int canNum = 0;private final int canMax = 100;Candidate[] oldCans = new Candidate[canMax];Candidate[] newCans = new Candidate[canMax];ObservableList<Candidate> oldList = FXCollections.observableArrayList();ObservableList<Candidate> newList = FXCollections.observableArrayList();// 控件TableView<Candidate> table = new TableView<>();Button btLoad = new Button("导入名单");Button btPaste = new Button("粘贴名单");CheckBox cbHasSeed = new CheckBox("种子分组");Button btDeleteSeed = new Button("清除种子");Button btGroupAdd = new Button("+");Button btGroupMin = new Button("-");Label lbGroupNum = new Label("5");Button btClear = new Button("清除分组");Button btGroup = new Button("开始分组");Button btCopy = new Button("复制分组");// 表格栏TableColumn idCol = new TableColumn("number");TableColumn nameCol = new TableColumn("name");TableColumn scoreCol = new TableColumn("score");TableColumn groupCol = new TableColumn("groupNo");// 粘贴名单TextArea ta = new TextArea();Button btYes = new Button("确定");Button btNo = new Button("取消");Label lbWarn = new Label();public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage primaryStage) throws Exception {table.setEditable(true);idCol.setVisible(false);nameCol.setVisible(false);scoreCol.setVisible(false);groupCol.setVisible(false);idCol.setMinWidth(50);nameCol.setMinWidth(140);scoreCol.setMinWidth(140);groupCol.setMinWidth(140);idCol.setCellValueFactory(new PropertyValueFactory<>("number"));nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));scoreCol.setCellValueFactory(new PropertyValueFactory<>("score"));groupCol.setCellValueFactory(new PropertyValueFactory<>("groupNo"));table.getColumns().addAll(idCol, nameCol, scoreCol, groupCol);Font font1 = new Font("Arial", 12);btLoad.setFont(font1);btPaste.setFont(font1);cbHasSeed.setFont(font1);btDeleteSeed.setFont(font1);btGroupAdd.setFont(font1);btGroupMin.setFont(font1);lbGroupNum.setFont(font1);btClear.setFont(font1);btGroup.setFont(font1);btCopy.setFont(font1);Color color1 = new Color(0.8, 0.2, 0.6, 1);btLoad.setTextFill(color1);btPaste.setTextFill(color1);btDeleteSeed.setTextFill(color1);btGroupAdd.setTextFill(color1);btGroupMin.setTextFill(color1);btClear.setTextFill(color1);btGroup.setTextFill(color1);btCopy.setTextFill(color1);cbHasSeed.setTextFill(color1);lbGroupNum.setTextFill(color1);btLoad.setStyle("-fx-border-color:#abbfff");btPaste.setStyle("-fx-border-color:#abbfff");btDeleteSeed.setStyle("-fx-border-color:#abbfff");btGroupAdd.setStyle("-fx-border-color:#abbfff");btGroupMin.setStyle("-fx-border-color:#abbfff");btClear.setStyle("-fx-border-color:#abbfff");btGroup.setStyle("-fx-border-color:#abbfff");btCopy.setStyle("-fx-border-color:#abbfff");table.setStyle("-fx-border-color:#abbfff");btLoad.setBackground(new Background(new BackgroundFill(Color.GHOSTWHITE, null, null)));btPaste.setBackground(new Background(new BackgroundFill(Color.GHOSTWHITE, null, null)));btDeleteSeed.setBackground(new Background(new BackgroundFill(Color.GHOSTWHITE, null, null)));btGroupAdd.setBackground(new Background(new BackgroundFill(Color.GHOSTWHITE, null, null)));btGroupMin.setBackground(new Background(new BackgroundFill(Color.GHOSTWHITE, null, null)));btClear.setBackground(new Background(new BackgroundFill(Color.GHOSTWHITE, null, null)));btGroup.setBackground(new Background(new BackgroundFill(Color.GHOSTWHITE, null, null)));btCopy.setBackground(new Background(new BackgroundFill(Color.GHOSTWHITE, null, null)));table.setBackground(new Background(new BackgroundFill(Color.HONEYDEW, null, null)));HBox hb = new HBox(5, btGroupMin, lbGroupNum, btGroupAdd);hb.setAlignment(Pos.CENTER);VBox vb = new VBox(10, new Label("名单设置"), btLoad, btPaste, new Label("种子设置"), cbHasSeed, btDeleteSeed,new Label("分组数量"), hb, new Label(" "), btClear, btGroup, btCopy);vb.setPrefWidth(80);vb.setAlignment(Pos.TOP_CENTER);BorderPane pane = new BorderPane();pane.setCenter(table);pane.setRight(vb);pane.setPadding(new Insets(10));pane.setBackground(new Background(new BackgroundFill(Color.AZURE, null, null)));primaryStage.setTitle("随机分组");primaryStage.setScene(new Scene(pane, 600, 400));primaryStage.show();// 粘贴名单ta.setPromptText("请将名单粘贴在文本区内!");ta.setPrefColumnCount(10);ta.setPrefRowCount(8);ta.setWrapText(true);btYes.setPrefSize(50, 30);btNo.setPrefSize(50, 30);HBox choose = new HBox(20, btYes, btNo);VBox vbox1 = new VBox(10, ta, choose);choose.setAlignment(Pos.CENTER);vbox1.setAlignment(Pos.CENTER);vbox1.setPadding(new Insets(10));vbox1.setBackground(new Background(new BackgroundFill(Color.ALICEBLUE, null, null)));Stage stage1 = new Stage();stage1.setScene(new Scene(vbox1, 300, 200));stage1.setTitle("粘贴名单");// 未粘贴提示HBox hbox1 = new HBox(lbWarn);hbox1.setPadding(new Insets(10));hbox1.setAlignment(Pos.CENTER_LEFT);hbox1.setBackground(new Background(new BackgroundFill(Color.ALICEBLUE, null, null)));Font font2 = new Font("", 14);lbWarn.setFont(font2);Stage stage2 = new Stage();stage2.setScene(new Scene(hbox1, 400, 50));// 导入名单btLoad.setOnAction(e -> {table.getItems().remove(0, canNum);FileChooser fc = new FileChooser();File f = fc.showOpenDialog(primaryStage);fileDeal(f);});// 粘贴名单btPaste.setOnAction(e -> {ta.setText(null);stage1.show();});btYes.setOnAction(e -> {if (ta.getText() != null) {table.getItems().remove(0, canNum);File a = new File("临时文件.txt");try {FileWriter fw = new FileWriter(a);fw.write(ta.getText());fw.flush();} catch (IOException e1) {e1.printStackTrace();}fileDeal(a);a.delete();stage1.close();} else {lbWarn.setText("文本区错误,请重新粘贴名单!");stage2.setTitle("错误提示");stage2.show();}});btNo.setOnAction(e -> {stage1.close();});// 清除种子btDeleteSeed.setOnAction(e -> {int groupNum = Integer.valueOf(lbGroupNum.getText());if (canNum / groupNum >= 2) {for (int i = 0; i < canNum - 1; i++) {for (int j = 0; j < canNum - i - 1; j++) {if (oldCans[j].getScore() < oldCans[j + 1].getScore()) {Candidate temp = oldCans[j];oldCans[j] = oldCans[j + 1];oldCans[j + 1] = temp;}}}for (int i = 0; i < canNum; i++)oldList.add(oldCans[i]);table.getItems().remove(0, canNum);table.setItems(oldList);table.getItems().remove(0, groupNum);canNum = table.getItems().size();} else {lbWarn.setText("单组人数存在仅有一人的情况,不可清除种子!");stage2.setTitle("错误提示");stage2.show();}});// 分组数量+btGroupAdd.setOnAction(e -> {int i = Integer.valueOf(lbGroupNum.getText());if (i < canNum) {lbGroupNum.setText(String.valueOf(i + 1));}});// 分组数量-btGroupMin.setOnAction(e -> {int i = Integer.valueOf(lbGroupNum.getText());if (i > 2) {lbGroupNum.setText(String.valueOf(i - 1));}});// 清除分组btClear.setOnAction(e -> {table.getItems().remove(0, canNum);table.setItems(null);for (int i = 0; i < canNum; i++) {oldCans[i].setGroupNo(0);oldList.add(oldCans[i]);newCans[i] = oldCans[i];}table.setItems(oldList);});// 开始分组btGroup.setOnAction(e -> {table.getItems().remove(0, canNum);int groupNum = Integer.valueOf(lbGroupNum.getText());ArrayList<Integer> list = new ArrayList<>();if (cbHasSeed.isSelected()) {for (int i = 0; i < groupNum; i++) {list.add(i);}for (int i = 0; i < canNum - 1; i++) {for (int j = 0; j < canNum - i - 1; j++) {if (newCans[j].getScore() < newCans[j + 1].getScore()) {Candidate temp = newCans[j];newCans[j] = newCans[j + 1];newCans[j + 1] = temp;}}}int index = 0;while (index < canNum) {Collections.shuffle(list);for (int i = 0; i < groupNum && index < canNum; i++) {newCans[index].setGroupNo(list.get(i) % groupNum + 1);index++;}}} else {for (int i = 0; i < canNum; i++) {list.add(i);}Collections.shuffle(list);for (int j = 0; j < canNum; j++) {newCans[j].setGroupNo(list.get(j) % groupNum + 1);}}for (int i = 0; i < canNum - 1; i++) {for (int j = 0; j < canNum - i - 1; j++) {if (newCans[j].getGroupNo() > newCans[j + 1].getGroupNo()) {Candidate temp = newCans[j];newCans[j] = newCans[j + 1];newCans[j + 1] = temp;}}}for (int i = 0; i < canNum; i++)newList.add(newCans[i]);table.setItems(newList);});// 复制分组btCopy.setOnAction(e -> {int groupNum = Integer.valueOf(lbGroupNum.getText());if (newCans[0].getGroupNo() != 0) {String s = "";String a = "";for (int i = 0; i < groupNum; i++) {a = "第" + (i + 1) + "组: ";for (Candidate m : newList) {if (m != null) {if (m.getGroupNo() == (i + 1))a = a + m.getName() + " ";}}a = a + "\n";s += a;}Clipboard clipboard = Clipboard.getSystemClipboard();ClipboardContent clipboardContent = new ClipboardContent();clipboardContent.putString(s);clipboard.setContent(clipboardContent);lbWarn.setText("已将分组结果复制到剪切板!");stage2.setTitle("成功提示");} else {lbWarn.setText("您还未分组!");stage2.setTitle("错误提示");}stage2.show();});}private void fileDeal(File x) {try (Scanner b = new Scanner(x)) {String text = "";while (b.hasNextLine())text += b.nextLine() + "\n";textAnalyze(text);} catch (FileNotFoundException e1) {e1.printStackTrace();}table.setItems(oldList);}private void textAnalyze(String text) {canNum = 0;Consumer con = s -> {String list[] = ((String) s).split(",");oldCans[canNum] = new Candidate(canNum + 1, list[0], Integer.parseInt(list[1]), 0);newCans[canNum] = new Candidate(canNum + 1, list[0], Integer.parseInt(list[1]), 0);oldList.add(oldCans[oldList.size()]);idCol.setVisible(true);nameCol.setVisible(true);groupCol.setVisible(true);scoreCol.setVisible(true);canNum++;};for (String s : text.split("\n"))con.accept(s);oldList.forEach(System.out::println);}
}

【java】随机分组:设计一个GUI程序,可以用来随机分组功能,如小组作业、球赛赛程相关推荐

  1. 1.设计一个抽奖程序,程序通过随机方式产生中奖序号。程序可以设置序号包含数字位数 和最大序号,一次抽取几个中奖序号(最少 1 个序号,最多不超过 2 个序号)和总共中奖 人数(最少 5 人)。抽奖过程

    实验目的   掌握随机函数的使用 主要仪器设备及耗材    安装了 JDK1.8 的 PC 一台 实验内容 1.设计一个抽奖程序,程序通过随机方式产生中奖序号.程序可以设置序号包含数字位数 和最大序号 ...

  2. pythongui程序,python第一个GUI程序

    第一个GUI程序 截止目前,我们的python基本语法就已经讲完了,但是python的应用确实无比之广,不同的应用领域需要学习不同的Python库,比如爬虫的urllib模块,科学计算numpy模块, ...

  3. PHP 程序员如何设计一个爬虫程序

     A8U几年前接过一个项目,类似一个 PHP 爬虫程序,做一个微博舆情分析系统:要爬取新浪微博,用特定关键词搜索中的页面的微博内容. 那是我第一次接触网络爬虫,根本没有思路,也不了解什么Scrap ...

  4. 设计一个shell程序,在/userdata目录下建立50个目录,并对每个目录给754权限!

    设计一个Shell程序,在/userdata目录下建立50个目录,即user1-user50,并设置每个目录的权限,其中其他用户的权限为:读:文件所有者的权限为:读.写.执行:文件所有者所在组的权限为 ...

  5. python的GUI编程和tkinter学习笔记——第一个GUI程序

    一.第一个GUI程序 from tkinter import * from tkinter import messagebox# 创建窗口 root = Tk()btn01 = Button(root ...

  6. python怎么开发gui程序_第一个GUI程序

    Python GUI 开发有好几个第三方的库,我选择的是tkinter 最简单的一个GUI程序 import tkinter as tk//给库来个简写,用的时候简洁一点 root = tk.Tk() ...

  7. 日常生活中,人们需要对某些常见图形和几何体做计算面积,体积,周长等相关计算。设计一个形状计算器,完成此功能。通过对菜单的选择,可以对不同的形状进行计算。

    日常生活中,人们需要对某些常见图形和几何体做计算面积,体积,周长等相关计算.设计一个形状计算器,完成此功能.通过对菜单的选择,可以对不同的形状进行计算. 1 作业中的接口与类 2 抽象类用于所有空间实 ...

  8. 编写一个C程序,实现以下功能:定义一个学生结构体Student(含学号、姓名、年龄、身高)和一个函数sort(struct Student *p),该函数使用选择排序法按年龄由小到大排序。在主函数中

    编写一个C程序,实现以下功能: 定义一个学生结构体Student(含学号.姓名.年龄.身高)和一个函数sort(struct Student *p),该函数使用选择排序法按年龄由小到大排序.在主函数中 ...

  9. 编写一个C程序,实现以下功能:编写一个函数decTobin(int n),该函数能将一个十进制数n转换成二进制数,输入13 输出 1101。在main函数中输入整数n,调用函数,输出它的二进制

    题目要求: 编写一个C程序,实现以下功能: //编写一个函数decTobin(int n),该函数能将一个十进制数n转换成二进制数,输入13 输出 1101. //在main函数中输入整数n,调用函数 ...

  10. 编写一个C程序,实现以下功能:编写一个常规的函数和一个递归函数,两个函数均能将输入的一个字符串以按反序形式的字符串作为返回值。在main函数中输入一行字符串,分别调用两个函数,输出反序后的字符串。

    题目要求: 编写一个C程序,实现以下功能:编写一个常规的函数和一个递归函数,两个函数均能将输入的一个字符串以按反序形式的字符串作为返回值.在main函数中输入一行字符串,分别调用两个函数,输出反序后的 ...

最新文章

  1. vfp全国计算机二级,全国计算机二级VFP试题
  2. Linux环境下安装Mysql+SphinxSE
  3. ug后处理如何加密_什么叫UG编程?UG编程是干嘛的?不得不看哦!的UG
  4. C语言数组学习 - 使用窗口版程序演示
  5. 阿拉伯数字转为罗马数字
  6. android studio 获取SHA1值 MD5值
  7. 支持商用吗_可商用的插画素材 | 美翻了
  8. Java高级语法笔记-抽象类
  9. Promise源码学习(2)
  10. vs2008 调试时不会命中断点,源代码与原始版本不同,解决办法
  11. VB6.0中,DTPicker日期、时间控件不允许为空时,采用文本框与日期、时间控件相互替换赋值(解决方案)
  12. Struts2之ajax初析
  13. kindeditor 文件上传在 spring mvc下的使用
  14. 程序员常用的计算机编程语言介绍
  15. 在linux系统上搭建测试环境
  16. word分栏对齐方法
  17. zotero与Obsidian联动笔记(一):ob中直接调用zotero的文献,并生成笔记
  18. HPUX 无法启动 卡在EVN_MC_INITIATED
  19. 人工智能大战苹果缺席 保护用户隐私拖慢其步伐
  20. super在python中是什么意思_python中super()的作用是什么

热门文章

  1. Java 简单爬虫 代码
  2. Python爬虫利器 ——代码转换
  3. 亿乐社区一比一高仿源码全开源
  4. R语言安装及包的使用
  5. 帕萨特加载模式启用怎么解除_大众帕萨特VCDS刷隐藏功能方法
  6. 蓝桥杯历年真题分类汇总(史上最全版本,一定不要错过)
  7. 程序员双十一都该买点啥?
  8. python反编译_反编译 python
  9. java海康摄像头添加人脸_java及opencv实现调用本地摄像头、网络摄像头完成人脸检测、人脸收集、人脸识别、性别识别...
  10. 暴风影音CTO:暴风门事件给研发团队带来的启示