代码实现界面如下
首先创建一个Group类,实现学生的添加选择,分组以及选出主讲人的功能
代码如下
package fz;
import java.util.ArrayList;
import java.util.Random;
public class Group {
// 记录学生
private ArrayList<Student> student = new ArrayList<>();
// 记录小组
private ArrayList<ArrayList<Student>> group = new ArrayList<>();
// 记录演讲人
private ArrayList<Student> speaker = new ArrayList<>();
// 分组开关
private boolean fenGroup = false;
// 获取分组开关
public boolean getFenGroup() {
return fenGroup;
}
// 设置分组开关
public void setFenGroup(boolean b) {
this.fenGroup = b;
}
// 获取学生人数
public int getStudentNum() {
if (student.isEmpty()) {
return 0;
}
return student.size();
}
// 获取小组人数
public int getGroupNum() {
if (group.isEmpty()) {
return 0;
}
return group.size();
}
// 获取i组j位的学生名字
public String getGroupStudentName(int i, int j) {
if (group.isEmpty()) {
return "";
}
return group.get(i).get(j).getName();
}
// 获取i组j位的学生id
public int getGroupStudentId(int i, int j) {
if (group.isEmpty()) {
return 0;
}
return group.get(i).get(j).getId();
}
// 获取每组成员人数
public int getGroupStudentNum(int num) {
if (group.isEmpty()) {
return 0;
}
return group.get(num).size();
}
// 随机添加学生
public void addStudent(int num) {
int count = 0;// 计算学生id
int addStudent = 0;// 计算添加的学生
// 从1001开始计算
if (student != null) {
count = 1001 + student.size();
addStudent = student.size();
} else {
count = 1001;
}
String x = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String name = "张三";
for (int i = addStudent; i < (addStudent + num); i++) {
Student st = new Student(name + x.charAt(i), count);
student.add(st);
count++;
}
}
// 分组,num个为一组
public void selectGroup(int num) {
if (student.isEmpty() || num < 1) {
System.out.println("学生或小组数量为0,无法分组");
return;
}
ArrayList<Student> stu = new ArrayList<>();
Random r = new Random();
int addStudent = 0;// 需要存放的学生
int t = 0;// 随机数
if (!group.isEmpty()) {
int temp = (group.size() - 1) * num + group.get(group.size() - 1).size();
addStudent = student.size() - temp;
this.copyFenGroupStudent(stu, temp, student.size());// 深拷贝存放的学生
int addStu = num;
if (stu.size() > num - group.get(group.size() - 1).size()) {
addStu = num - group.get(group.size() - 1).size();
}
ArrayList<Student> st = new ArrayList<Student>(group.get(group.size() - 1));
group.remove(group.get(group.size() - 1));
for (int i = 0; i < addStu; i++) {
t = r.nextInt(addStudent);
Student s = new Student(stu.get(t).getName(), stu.get(t).getId());
st.add(s);
addStudent--;
this.exchangeStudent(stu, t, addStudent);
}
group.add(st);
} else {
addStudent = student.size();
this.copyFenGroupStudent(stu, 0, addStudent);// 深拷贝存放的学生
}
int Group1 = addStudent / num;// 满成员小组个数
int Group2 = addStudent % num;// 小于num个数的学生为新的一组
for (int i = 0; i < Group1; i++) {
ArrayList<Student> st = new ArrayList<>();
for (int j = 0; j < num; j++) {
t = r.nextInt(addStudent);
Student s = new Student(stu.get(t).getName(), stu.get(t).getId());
st.add(s);
addStudent--;
this.exchangeStudent(stu, t, addStudent);
}
group.add(st);
}
if (Group2 > 0) {
ArrayList<Student> st = new ArrayList<>();
for (int i = 0; i < Group2; i++) {
Student s = new Student(stu.get(i).getName(), stu.get(i).getId());
st.add(s);
}
group.add(st);
}
}
// 小组成员交换
private void exchangeStudent(ArrayList<Student> stu, int qian, int hou) {
Student temp = new Student(stu.get(hou).getName(), stu.get(hou).getId());
stu.get(hou).addStudentDate(stu.get(qian).getName(), stu.get(qian).getId());
stu.get(qian).addStudentDate(temp.getName(), temp.getId());
}
// 深拷贝学生数据
private void copyFenGroupStudent(ArrayList<Student> stu, int existStudent, int allStudent) {
for (int i = 0; i < allStudent - existStudent; i++) {
Student s = new Student(student.get(existStudent + i).getName(), student.get(existStudent + i).getId());
stu.add(s);
}
}
// 从小组中抽取演讲人
public void selectStudent() {
if (group.isEmpty()) {
System.out.println("小组为空,不能抽取演讲人");
return;
}
int t = 0;// 记录抽取到的随机数
Random r = new Random();// 随机数函数
int speakerNum = 0;// 演讲人数
if (!speaker.isEmpty()) {
speakerNum = speaker.size();
// 如果最后一组没满员重新抽取
if (group.get(0).size() != group.get(speakerNum - 1).size()) {
speakerNum--;
speaker.remove(speakerNum - 1);
}
}
for (int i = speakerNum; i < this.group.size(); i++) {
int size = this.group.get(i).size();
t = r.nextInt(size);
Student stu = new Student(group.get(i).get(t).getName(), group.get(i).get(t).getId());
speaker.add(stu);
}
}
// 显示演讲人
public String showGroupSpeaker(int num) {
if (speaker.isEmpty()) {
return "演讲人为空\n";
}
if (num > -1 && num < speaker.size()) {
return ("第" + (num + 1) + "组" + "演讲人为:" + speaker.get(num).getName() + " id:" + speaker.get(num).getId()
+ "\n");
} else if (num >= speaker.size()) {
return ("第" + (num + 2) + "组未抽取演讲人\n");
} else {
return ("查看演讲人失败,输入数据错误!\n");
}
}
// 查看小组成员名字
public String lookGroupName(int num) {
if (group.isEmpty()) {
return ("小组为空,不能查看");
}
String getName = ("第" + (num + 1) + "组\n" + "小组成员名字为:");
for (int i = 0; i < this.group.get(num).size(); i++) {
getName += ("\t" + this.group.get(num).get(i).getName());
}
getName += "\n";
return getName;
}
// 查看小组成员id
public String lookGroupId(int num) {
if (group.isEmpty()) {
return ("小组为空,不能查看");
}
String getId = "小组成员id为:";
for (int i = 0; i < this.group.get(num).size(); i++) {
getId += ("\t" + this.group.get(num).get(i).getId());
}
getId += "\n";
return getId;
}
// 全部清空
public void clearAll(int num) {
if (num == 0) {
student.clear();
group.clear();
speaker.clear();
System.out.println("清空成功");
}
}
// 结束类
}
class Student {
private String studentName;// 学生名字
private int studentId;// 学生id
public Student() {
}// 无参构造函数
// 有参构造函数
public Student(String name, int id) {
this.studentName = name;
this.studentId = id;
}
// 添加学生信息
public void addStudentDate(String name, int id) {
this.studentName = name;
this.studentId = id;
}
public String getName() {
return this.studentName;
}
public int getId() {
return this.studentId;
}
}
接着创建一个选择学生,选择主讲人以及实现显示分组,清空分组功能的一个面板
也就是创建一个GroupJFrame类
代码如下
package fz;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
public class GroupJFrame extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = -3394774975694421546L;
Group g = new Group();
// 创建按钮
JButton jbt1 = new JButton("添加学生");
JButton jbt2 = new JButton("分组");
JButton jbt3 = new JButton("清空");
JButton jbt4 = new JButton("抽取演讲人");
JButton jbt5 = new JButton("显示分组情况");
// //创建文本
JTextField jf1 = null;
// JTextField jf2 = null;
// 下拉菜单
JComboBox<String> co1 = new JComboBox<>();
JComboBox<String> co2 = new JComboBox<>();
// 消息对话框
JDialog jd1 = new JDialog();
JDialog jd2 = new JDialog();
JDialog jd3 = new JDialog();
// 标签
Font font = new Font("宋体", Font.PLAIN, 20);
JLabel jl1 = new JLabel("添加学生的人数:");
JLabel jl2 = new JLabel("每组人数:");
JLabel jl3 = new JLabel("学生总人数:");
JLabel jl4 = new JLabel("0");
JLabel jl5 = new JLabel();
// 文本区
JTextArea jt1 = new JTextArea();
JTextArea jt2 = new JTextArea();
// 菜单
JMenu menu1 = new JMenu("选项");
JMenuItem mi1 = new JMenuItem("清空");
JMenuItem mi2 = new JMenuItem("关闭");
JMenu menu2 = new JMenu("关于我们");
JMenuItem mi10 = new JMenuItem("QQ");
JMenuItem mi11 = new JMenuItem("微信");
public GroupJFrame() {
// 标题
super("分组");
// 界面
this.setSize(850, 600);
this.setResizable(false);
// 置顶
this.setAlwaysOnTop(true);
// 居中
this.setLocationRelativeTo(null);
// 取消居中放置
this.setLayout(null);
// 添加文本域
JPanel jp = new JPanel();
this.add(jp);
jl1.setBounds(20, 20, 150, 32);
jl1.setFont(font);
this.add(jl1);
jl2.setBounds(320, 20, 100, 32);
jl2.setFont(font);
this.add(jl2);
jl3.setBounds(550, 20, 120, 32);
jl3.setFont(font);
this.add(jl3);
jl4.setBounds(670, 20, 40, 32);
jl4.setFont(font);
this.add(jl4);
// 图片
JLabel jl10 = new JLabel(new ImageIcon("image\\qq.jpg"));
JLabel jl11 = new JLabel(new ImageIcon("image\\weixin.jpg"));
jl10.setBounds(0, 0, 400, 600);
// 将图片添加到对话框
jd1.getContentPane().add(jl10);
// 设置弹框大小
jd1.setSize(600, 600);
jd1.setAlwaysOnTop(true);
jd1.setLocationRelativeTo(null);
// 弹框不关闭则无法关闭
jd1.setModal(true);
jl11.setBounds(0, 0, 400, 600);
// 将图片添加到对话框
jd2.getContentPane().add(jl11);
// 设置弹框大小
jd2.setSize(600, 600);
jd2.setAlwaysOnTop(true);
jd2.setLocationRelativeTo(null);
// 弹框不关闭则无法关闭
jd2.setModal(true);
jf1 = new JTextField();
jf1.setBounds(420, 20, 80, 32);
// jf2 = new JTextField();
// jf2.setFont(font);
// jf2.setBounds(20, 150, 500, 300);
// jf2.setEditable(false);
// this.add(jf2);
// jl5.setBounds(20,150,500,400);
// jl5.setFont(font);
// this.add(jl5);
// 文本区
// jt1.setBounds(20,150,530,360);
jt1.setFont(font);
// this.add(jt1);
// 滚动条
JScrollPane scrollPane = new JScrollPane(jt1);
// 总是显示滚动条
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(20, 150, 530, 360);
this.add(scrollPane);
// 下拉菜单设置
co1.addItem("1");
co1.addItem("2");
co1.addItem("3");
co1.addItem("4");
co1.addItem("5");
co1.addItem("6");
co1.addItem("7");
co1.addItem("8");
co1.addItem("9");
co1.addItem("10");
co1.addItem("11");
co1.addItem("12");
co1.setBounds(170, 20, 80, 32);
co1.setFont(font);
this.add(co1);
co2.addItem("1");
co2.addItem("2");
co2.addItem("3");
co2.addItem("4");
co2.addItem("5");
co2.setEditable(false);
co2.setBounds(420, 20, 80, 32);
co2.setFont(font);
this.add(co2);
// 设置按钮坐标
jbt1.setBounds(50, 100, 120, 40);
jbt2.setBounds(230, 100, 120, 40);
jbt3.setBounds(570, 100, 120, 40);
jbt4.setBounds(400, 100, 120, 40);
jbt5.setBounds(570, 180, 120, 40);
jbt1.addActionListener(this);
jbt2.addActionListener(this);
jbt3.addActionListener(this);
jbt4.addActionListener(this);
jbt5.addActionListener(this);
this.getContentPane().add(jbt1);
this.getContentPane().add(jbt2);
this.getContentPane().add(jbt3);
this.getContentPane().add(jbt4);
this.getContentPane().add(jbt5);
// 初始化菜单
JMenuBar menubar = new JMenuBar();
menubar.setFont(font);
this.setJMenuBar(menubar);
menubar.add(menu1);
menubar.add(menu2);
ButtonGroup bg1 = new ButtonGroup();
bg1.add(mi1);
bg1.add(mi2);
menu1.add(mi1);
menu1.add(mi2);
bg1.add(mi10);
bg1.add(mi11);
menu2.add(mi10);
menu2.add(mi11);
mi1.addActionListener(this);
mi2.addActionListener(this);
mi10.addActionListener(this);
mi11.addActionListener(this);
// 显示
this.setVisible(true);
// 设置关闭模式
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
Object source = e.getSource();
if (source == jbt1) {
String s = (String) co1.getSelectedItem();
int num = Integer.parseInt(s);
g.addStudent(num);
g.setFenGroup(true);
jl4.setText("" + g.getStudentNum());
JOptionPane.showMessageDialog(this, "成功添加" + s + "名学生");
} else if (source == jbt2) {
if (g.getFenGroup()) {
String s = (String) co2.getSelectedItem();
int num = Integer.parseInt(s);
g.selectGroup(num);
g.setFenGroup(false);
this.add(jf1);
co2.removeAll();
co2.addItem(s);
co2.setEditable(false);
JOptionPane.showMessageDialog(this, "成功分组");
}
} else if (source == jbt3) {
g.clearAll(0);
g.setFenGroup(false);
jl4.setText("" + g.getStudentNum());
jt1.setText("");
} else if (source == jbt4) {
g.selectStudent();
JOptionPane.showMessageDialog(this, "成功抽取" + g.getGroupNum() + "名演讲人");
} else if (source == jbt5) {
String text = "";
jt1.setText(text);
for (int i = 0; i < g.getGroupNum(); i++) {
text = g.lookGroupName(i);// 名字
jt1.append(text);
text = g.lookGroupId(i);// id号
jt1.append(text);
// 获取演讲人
text = g.showGroupSpeaker(i);
jt1.append(text);
}
} else if (source == mi1) {
g.clearAll(0);
g.setFenGroup(false);
jl4.setText("" + g.getStudentNum());
jt1.setText("");
} else if (source == mi2) {
System.exit(0);
} else if (source == mi10) {
// 显示弹框
jd1.setVisible(true);
} else if (source == mi11) {
// 显示弹框
jd2.setVisible(true);
}
}
}
最后调用main函数实现以上方法
package fz;
import java.util.Random;
public class GroupMain {
static void test()
{
for(int i=0;i<100;i++)
{
Random r=new Random();
int t = r.nextInt(1);
System.out.println(t);
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
GroupJFrame j = new GroupJFrame();
//test();
}
}
以上实现了一个简单的选人,使用了面板操作,也可以用于接入数据库,加以添加更多的数据可以实现更全面的功能。
如有错误,欢迎指正!