模拟学员自动分组系统界面设计
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Random;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
//import javax.swing.RowFilter.Entry;
import javax.swing.Timer;
public class MainFrame extends javax.swing.JFrame {
static String stuFile = “/students.properties”;
boolean isRead = false; //是否已经读取过学生信息
Properties stus = new Properties();
DefaultListModel dlm = new DefaultListModel();
Random random = new Random();
int stuNumber = 0; //总学生数
int stuInGroup = 5; //每组学生数
int groupNumber = 1; //分组数
private Timer timer; //创建定时器对象
int selectedNumber = 0; //已经分配了多少学生
public MainFrame() {
initComponents();
this.setLocationRelativeTo(null);
jList1.setModel(dlm);
loading.setVisible(false);
//创建定时器,并注册监听器
timer = new Timer(1000,new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
stuNumber = dlm.size();
if(stuNumber == 0){
timer.stop();
groupButton.setEnabled(false);
loading.setVisible(false);
exportButton.setEnabled(true);
JOptionPane.showMessageDialog(MainFrame.this, “分组结束”);
return;
}
/*
//Swing中支持HTML格式文本
String timeText = “<html><font size=\”6\” color=\”blue\”>” + df.format(now.getTime()) + “</font></html>”;
*/
int index = random.nextInt(stuNumber); //生成随机数
Student selectedStu = (Student)dlm.remove(index);
if(selectedNumber++ % stuInGroup == 0){
if(selectedNumber != 1){
jTextArea1.append(“\n”);
}
jTextArea1.append(“第” + (selectedNumber/stuInGroup + 1) + “开发小组:”);
}
jTextArea1.append(selectedStu.getName() + ” “);
}
});
}
@SuppressWarnings(“unchecked”)
// <editor-fold defaultstate=”collapsed” desc=”Generated Code”>
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel3 = new javax.swing.JLabel();
readStuInfoButton = new javax.swing.JButton();
groupButton = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
numberPerGroup = new javax.swing.JComboBox();
loading = new javax.swing.JLabel();
exportButton = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle(“北京德润启航科技发展有限公司”);
jLabel1.setFont(new java.awt.Font(“楷体”, 1, 24)); // NOI18N
jLabel1.setText(” 北京德润科技Java极致班”);
jLabel2.setFont(new java.awt.Font(“楷体”, 1, 36)); // NOI18N
jLabel2.setForeground(new java.awt.Color(0, 51, 102));
jLabel2.setText(“自动分组系统”);
jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
jList1.setCellRenderer(new MyCellRender());
jScrollPane1.setViewportView(jList1);
jTextArea1.setColumns(20);
jTextArea1.setFont(new java.awt.Font(“楷体”, 0, 18)); // NOI18N
jTextArea1.setRows(5);
jScrollPane2.setViewportView(jTextArea1);
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource(“/images/derun.png”))); // NOI18N
readStuInfoButton.setText(“读取学生信息”);
readStuInfoButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
readStuInfoButtonActionPerformed(evt);
}
});
groupButton.setText(“开始分组”);
groupButton.setEnabled(false);
groupButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
groupButtonActionPerformed(evt);
}
});
jLabel4.setText(“人数限制(人/组):”);
numberPerGroup.setModel(new javax.swing.DefaultComboBoxModel(new String[] { “3”, “4”, “5”, “6”, “7”, “8” }));
numberPerGroup.setSelectedIndex(1);
numberPerGroup.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
numberPerGroupActionPerformed(evt);
}
});
loading.setIcon(new javax.swing.ImageIcon(getClass().getResource(“/images/23.gif”))); // NOI18N
exportButton.setText(“导出到文本文件到桌面”);
exportButton.setEnabled(false);
exportButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exportButtonActionPerformed(evt);
}
});
jTextField1.setBackground(new java.awt.Color(240, 240, 240));
jTextField1.setText(“学员总数:0人”);
jTextField1.setEnabled(false);
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(90, 90, 90))
.addGroup(layout.createSequentialGroup()
.addGap(8, 8, 8)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 555, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(readStuInfoButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(53, 53, 53)
.addComponent(groupButton)
.addGap(18, 18, 18)
.addComponent(loading)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(exportButton)
.addGap(41, 41, 41)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(numberPerGroup, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26))
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(132, 132, 132))))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(loading, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(numberPerGroup, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(exportButton))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(readStuInfoButton)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(groupButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)))
);
pack();
}// </editor-fold>
//读取学生信息
private void readStuInfoButtonActionPerformed(java.awt.event.ActionEvent evt) {
if(isRead){
dlm.clear();
selectedNumber = 0; //已分配的学生数归零
groupButton.setEnabled(true);
exportButton.setEnabled(false);
}
//开始读取学生信息
try {
stus.load(MainFrame.class.getResourceAsStream(stuFile));
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, “没有找到学员信息”,”警告”,JOptionPane.WARNING_MESSAGE);
return;
}
// Set<Entry<Object, Object>> stusinfo = stus.entrySet();
Set<Entry<Object, Object>> stusinfo = stus.entrySet();
this.showStuInfo(stusinfo);
}
public void showStuInfo(Set<Entry<Object, Object>> stusinfo){
for (Entry<Object, Object> stu : stusinfo){
String name = (String) stu.getKey();
String gender = (String) stu.getValue();
Icon icon;
if(“男”.equals(gender)){
// icon = new ImageIcon(getClass().getResource(“/images/m.png”));
icon = new ImageIcon(getClass().getResource(“/images/m.png”));
}else{
icon = new ImageIcon(getClass().getResource(“/images/f.png”));
}
Student student = new Student(icon,name,gender);
dlm.addElement(student);
}
jTextField1.setText(“学员总数:” + stusinfo.size() + “人”);
groupButton.setEnabled(true);
readStuInfoButton.setText(“重新读取信息”);
isRead = true;
//获得分组
stuNumber = dlm.size(); //总学生数
}
private void numberPerGroupActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
//开始分组
private void groupButtonActionPerformed(java.awt.event.ActionEvent evt) {
if(stuNumber == 0){
JOptionPane.showMessageDialog(this, “目前还没有学生可供分组”);
return;
}
stuInGroup = numberPerGroup.getSelectedIndex() + 3; //每组学生数
groupNumber = stuNumber /stuInGroup + 1;
//开始分组
jTextArea1.setText(“”); //先清空
timer.start(); //启动定时器
loading.setVisible(true);
}
private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {
//导出文本到桌面
String filePath = “C:\\Users\\dell\\Desktop\\文件接收柜\\group.txt”;
String content = jTextArea1.getText();
exportContent(content,filePath);
}
private void exportContent(String content,String filePath){
try {
FileWriter fw = new FileWriter(filePath);
fw.write(content);
fw.flush();
JOptionPane.showMessageDialog(this, “分组文件已导出!”);
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, “分组文件导出时出现错误,请稍后重试!”);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate=”collapsed” desc=” Look and feel setting code (optional) “>
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if (“Nimbus”.equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainFrame().setVisible(true);
}
});
}
// Variables declaration – do not modify
private javax.swing.JButton exportButton;
private javax.swing.JButton groupButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JList jList1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JLabel loading;
private javax.swing.JComboBox numberPerGroup;
private javax.swing.JButton readStuInfoButton;
// End of variables declaration
}
自动分组界面: