首页 / JAVA / java 简单 学生管理系统
java 简单 学生管理系统
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java 简单 学生管理系统,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含73678字,纯文字阅读大概需要106分钟。
内容图文
![java 简单 学生管理系统](/upload/InfoBanner/zyjiaocheng/630/335e51f7dbcd49ce8c8b6537dd06963b.jpg)
这应该算是一个简单学生管理系统吧。由于没有听课,刚开始凭群文件作业要求写了一个,后来发现有点过于简单,于是小小的扩展了一下。先展示一下程序界面。
第一次手生,代码不太好看,但是有点累,也不想再改了。
可以从文件导入或导出 。文件格式是XML,如图
支持筛选操作,但是不支持模糊搜索,模糊搜索部分不想再做了,感觉很累,就先这么着吧,
选中列表某一行会及那个信息自动填充到上面的框里面,但是目前我还没有支持直接在单元格内编辑保存,不过,监听什么的我全部写好了,只要稍加十几行代码就可以实现,同样也不想再写了,虽然区区十几行代码,还是不想动手。以后随缘再说。
筛选条件选成性别女
加上姓名筛选
只有id为主键,id是唯一的,其他字段都允许重复
单元格最后一行,点击会提示是否删除改行。
这一部分代码我写的累赘了,但是,为了学有所用,用另一种方法实现,而不是在原有方法里面加条件来实现是个很好的事情,所以我没有精简代码
其他筛选条件类似
下面是代码
主类,本来是写成三个类的,student类,start类,data类,后来我觉得太长,就把他们都分开了,花了些时间,走了些弯路,现在的代码也是有点累赘,同样的 不想改了,。
写成好几个类的一大坏处就是粘贴代码都很累。
![java 简单 学生管理系统 - 文章图片](/upload/getfiles/0001/2021/5/1/20210501114124442.jpg)
![java 简单 学生管理系统 - 文章图片](/upload/getfiles/0001/2021/5/1/20210501114124479.jpg)
1 package studentGUI; 2 3 4 import javax.swing.*; 5 import javax.swing.filechooser.FileNameExtensionFilter; 6 import java.awt.*; 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener; 9 import java.awt.event.KeyEvent; 10 import java.io.*; 11 import java.util.Scanner; 12 13 import static java.lang.System.exit; 14 import static studentGUI.XMLUtils.XML2list; 15 import static studentGUI.XMLUtils.list2XML; 16 17 public class Start { 18 private JFrame frame; 19 private Student student; 20 //private static List<Student> STUDENTS = new ArrayList<Student>(); 21 private static String FILEPATH = ""; 22 23 // 全局对象 交换信息用 24 private StudentList stList; 25 private Screen screen; 26 private MyTopPanel topPanel; 27 private MyTable myTable; 28 private MyScreenPanel screenPanel; 29 30 public Start() { 31 student = new Student(); 32 frame = new JFrame("STUDENT"); 33 stList = new StudentList(); 34 35 36 // 37 stList = new StudentList(); 38 topPanel = new MyTopPanel(); 39 myTable = new MyTable(); 40 screenPanel = new MyScreenPanel(); 41 screen = screenPanel.getScreen(); 42 43 topPanel.set(screenPanel,myTable,topPanel,stList); 44 myTable.set(screenPanel,myTable,topPanel,stList); 45 screenPanel.set(screenPanel,myTable,topPanel,stList); 46 47 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 48 frame.setMinimumSize(new Dimension(900, 600)); 49 frame.setResizable(false); 50 frame.setLayout(new GridLayout(2, 1, 1, 1)); //为Frame窗口设置布局为BorderLayout 51 frame.setJMenuBar(menuBar()); 52 frame.add(topPanel.getTopPanel()); 53 frame.add(myTable.getPanel(), BorderLayout.CENTER); 54 frame.setBounds(300, 200, 600, 300); 55 frame.setVisible(true); 56 } 57 58 public JMenuBar menuBar() { 59 JMenuBar menuBar = new JMenuBar(); 60 menuBar.add(createFileMenu()); 61 menuBar.add(createEditMenu()); 62 return menuBar; 63 } 64 65 66 private void refreshFrame() { 67 System.out.println("fresh screen!"); 68 frame.getContentPane().removeAll(); 69 frame.setMinimumSize(new Dimension(600, 500)); 70 frame.setResizable(false); 71 frame.setLayout(new GridLayout(2, 1, 1, 1)); //为Frame窗口设置布局为BorderLayout 72 // frame.setJMenuBar(menuBar()); 73 frame.add(topPanel.getTopPanel()); 74 frame.add(myTable.getPanel()); 75 frame.setVisible(true); 76 } 77 78 private JMenu createFileMenu() { 79 JMenu menu = new JMenu("File"); 80 menu.setMnemonic(KeyEvent.VK_F); 81 JMenuItem item = new JMenuItem("New(N)", KeyEvent.VK_N);//使用指定的文本和键盘助记符创建 JMenuItem 。 82 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK)); 83 item.setName("createNew"); 84 item.addActionListener(new fileMenuListener()); 85 menu.add(item); 86 item = new JMenuItem("Open(O)", KeyEvent.VK_O); 87 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK)); 88 item.setName("openFile"); 89 item.addActionListener(new fileMenuListener()); 90 menu.add(item); 91 item = new JMenuItem("Save(S)", KeyEvent.VK_S); 92 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK)); 93 item.setName("saveFile"); 94 item.addActionListener(new fileMenuListener()); 95 menu.add(item); 96 menu.addSeparator(); 97 item = new JMenuItem("Exit(E)", KeyEvent.VK_E); 98 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.CTRL_MASK)); 99 item.setName("exit"); 100 item.addActionListener(new fileMenuListener()); 101 menu.add(item); 102 return menu; 103 } 104 105 class fileMenuListener implements ActionListener { 106 107 @Override 108 public void actionPerformed(ActionEvent e) { 109 String cmd = e.getActionCommand(); 110 switch (cmd) { 111 case "New(N)": 112 funcNew(); 113 break; 114 case "Open(O)": 115 funcOpen(); 116 break; 117 case "Save(S)": 118 funcSave(); 119 break; 120 case "Exit(E)": 121 funcExit(); 122 } 123 } 124 125 private void funcExit() { 126 int n = JOptionPane.showConfirmDialog(null,"确认退出?","确认", 127 JOptionPane.YES_NO_OPTION ); 128 if (n == JOptionPane.YES_OPTION){ 129 exit(0); 130 } 131 } 132 133 private void funcSave() { 134 if (FILEPATH.length() == 0){ 135 FILEPATH = chooseFile("保存"); 136 } 137 if (FILEPATH.length() == 0){ 138 return; 139 } 140 //list2XML(FILEPATH); 141 File file = new File(FILEPATH); 142 if (!file.exists()) { 143 try { 144 file.createNewFile(); 145 } catch (IOException e) { 146 e.printStackTrace(); 147 JOptionPane.showMessageDialog(null, "文件创建失败!", "ERROR", JOptionPane.ERROR_MESSAGE); 148 return; 149 } 150 } 151 String writeString = list2XML(stList.getSTUDENTS()); 152 if ( file.canWrite()){ 153 OutputStream stream = null; 154 try { 155 stream = new FileOutputStream(file); 156 stream.write(writeString.getBytes("UTF8")); 157 JOptionPane.showMessageDialog(null,"写入成功!","SUCCESS",JOptionPane.ERROR_MESSAGE); 158 } catch (IOException e) { 159 e.printStackTrace(); 160 JOptionPane.showMessageDialog(null,"写入失败!","ERROR",JOptionPane.ERROR_MESSAGE); 161 }finally { 162 try { 163 stream.close(); 164 } catch (IOException e) { 165 e.printStackTrace(); 166 } 167 } 168 } 169 170 FILEPATH = ""; 171 } 172 173 174 175 176 private String chooseFile(String tip) { 177 FileNameExtensionFilter filter = new FileNameExtensionFilter("XML文件", "xml", "XML", "txt"); 178 // filter.a 179 JFileChooser fc = new JFileChooser(); 180 fc.setToolTipText(tip); 181 fc.setApproveButtonText(tip); 182 fc.setDialogTitle(tip); 183 fc.setApproveButtonToolTipText(tip); 184 fc.setFileFilter(filter); //设置一个文件筛选器 185 fc.setMultiSelectionEnabled(false); //设置不允许多选 186 /*使用showOpenDialog()方法,显示出打开选择文件的窗口,当选择了某个文件后,或者关闭此窗口那么都会返回一个 187 整型数值,如果返回的是0,代表已经选择了某个文件。如果返回1代表选择了取消按钮或者直接关闭了窗口*/ 188 int result = fc.showSaveDialog(null); 189 if (result == JFileChooser.APPROVE_OPTION) { 190 //获取当前选择的文件路径 191 File file = fc.getSelectedFile(); 192 //返回文件路径 193 return file.getPath(); 194 } 195 JOptionPane.showMessageDialog(null,"取消!","失败",JOptionPane.ERROR_MESSAGE); 196 return ""; 197 } 198 199 private void funcOpen() { 200 FILEPATH = chooseFile("打开"); 201 if (FILEPATH.length() == 0){ 202 return; 203 } 204 File file = new File(FILEPATH); 205 if (!file.exists()) { 206 try { 207 file.createNewFile(); 208 } catch (IOException e) { 209 e.printStackTrace(); 210 JOptionPane.showMessageDialog(null, "文件不存在!", "ERROR", JOptionPane.ERROR_MESSAGE); 211 return; 212 } 213 } 214 try { 215 //stList.setSTUDENTS(new ArrayList<Student>()); 216 StringBuffer XMLBuffer = new StringBuffer(); 217 Scanner input = new Scanner(file); 218 while (input.hasNextLine()){ 219 XMLBuffer.append(input.nextLine()); 220 } 221 System.out.println("***********************************\n" 222 +"FILE VALUE:\n" 223 +XMLBuffer.toString() + 224 "************************"); 225 input.close(); 226 stList.setSTUDENTS(XML2list(XMLBuffer.toString())); 227 } catch (FileNotFoundException e) { 228 e.printStackTrace(); 229 } 230 231 refreshFrame(); 232 } 233 234 private void funcNew() { 235 } 236 } 237 238 private JMenu createEditMenu() { 239 JMenu menu = new JMenu("Edit"); 240 menu.setMnemonic(KeyEvent.VK_E); 241 JMenuItem item = new JMenuItem("暂无功能,就是想加上", KeyEvent.VK_N);//使用指定的文本和键盘助记符创建 JMenuItem 。 242 item.addActionListener(new fileMenuListener()); 243 menu.add(item); 244 return menu; 245 } 246 247 248 public JFrame getFrame() { 249 return frame; 250 } 251 252 public static void main(String[] args) { 253 Start s = new Start(); 254 } 255 }主类 Start类
![java 简单 学生管理系统 - 文章图片](/upload/getfiles/0001/2021/5/1/20210501114124442.jpg)
![java 简单 学生管理系统 - 文章图片](/upload/getfiles/0001/2021/5/1/20210501114124479.jpg)
1 package studentGUI; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 import java.util.Calendar; 8 9 public class MyScreenPanel { 10 JPanel MAIN_PANEL; 11 // 全局对象 交换信息用 12 private StudentList STUDENTLIST; 13 private Screen screen; //筛选器 14 private MyTopPanel topPanel; 15 private MyTable TABLE; 16 private MyScreenPanel screenPanel; 17 18 JLabel nameLabel; 19 JLabel genderLabel ; 20 JLabel birthLabel ; 21 JLabel yearLabel; 22 JLabel monthLabel; 23 JLabel dayLabel; 24 JLabel idLabel; 25 JLabel classLabel; 26 JLabel schoolLabel; 27 JLabel otherLabel; 28 JComboBox genderBox; 29 JComboBox ageYearBox; 30 JComboBox monthBox; 31 JComboBox dayBox; 32 JTextField nameTextField; 33 JTextField idTextField ; 34 JTextField classTextField ; 35 JTextField schoolTextField ; 36 JTextArea otherTextField ; 37 JPanel leftDatePanel; 38 JLabel startDateLabel; 39 JLabel leftYearLabel; 40 JLabel leftMonthLabel; 41 JLabel leftDayLabel; 42 JComboBox leftYearBox; //创建JComboBox 43 JComboBox leftMnthBox; //创建JComboBox 44 JComboBox leftDayBox; //创建JComboBox 45 JPanel rightDatePanel; 46 JLabel endDateLabel; 47 JLabel rightYearLabel; 48 JLabel rightMonthLabel; 49 JLabel rightDayLabel; 50 JComboBox rightYearBox; //创建JComboBox 51 JComboBox rightMnthBox; //创建JComboBox 52 JComboBox rightDayBox; //创建JComboBox 53 54 public MyScreenPanel() { 55 MAIN_PANEL = new JPanel(); 56 screen = new Screen(); //新建筛选器 57 58 nameLabel = new JLabel("姓名:"); 59 genderLabel = new JLabel("性别:"); 60 idLabel = new JLabel("学号:"); 61 classLabel = new JLabel("班级:"); 62 schoolLabel = new JLabel("学校:"); 63 nameTextField = new JTextField(8); 64 idTextField = new JTextField(10); 65 classTextField = new JTextField(10); 66 schoolTextField = new JTextField(15); 67 genderBox = new JComboBox(); //创建JComboBox 68 69 nameTextField.setText(screen.getName()); 70 idTextField.setText(screen.getStudentID()); 71 classTextField.setText(screen.getStudentClass()); 72 schoolTextField.setText(screen.getSchool()); 73 74 leftDatePanel = new JPanel(); 75 // leftDatePanel.setLayout(bagLayout); 76 startDateLabel = new JLabel("生日从:"); 77 leftYearLabel = new JLabel("年"); 78 leftMonthLabel = new JLabel("月"); 79 leftDayLabel = new JLabel(""); 80 leftYearBox = new JComboBox(); //创建JComboBox 81 leftMnthBox = new JComboBox(); //创建JComboBox 82 leftDayBox = new JComboBox(); //创建JComboBox 83 84 rightDatePanel = new JPanel(); 85 //rightDatePanel.setLayout(bagLayout); 86 endDateLabel = new JLabel(" 到:"); 87 rightYearLabel = new JLabel("年"); 88 rightMonthLabel = new JLabel("月"); 89 rightDayLabel = new JLabel(""); 90 rightYearBox = new JComboBox(); //创建JComboBox 91 rightMnthBox = new JComboBox(); //创建JComboBox 92 rightDayBox = new JComboBox(); //创建JComboBox 93 } 94 95 public void set(MyScreenPanel screenPanel, MyTable myTable, MyTopPanel myTopPanel, StudentList list){ 96 this.screenPanel = screenPanel; 97 this.topPanel = myTopPanel; 98 this.STUDENTLIST = list; 99 this.screenPanel = screenPanel; 100 this.TABLE = myTable; 101 } 102 103 public void refresh(){ 104 topPanel.refresh(); 105 /** 106 * TODO LiSt 添加 bottomPAnel refresh 107 * 因为过滤器刷新,总伴随表格刷新 108 * 109 */ 110 } 111 112 public JPanel getScreenPanel(){ 113 MAIN_PANEL = new JPanel(); 114 setPanel(MAIN_PANEL); 115 return MAIN_PANEL; 116 } 117 118 public JPanel setPanel(JPanel p) { 119 GridBagLayout bagLayout = new GridBagLayout(); //创建GridBagLayout布局管理器 120 GridBagConstraints constraints = new GridBagConstraints(); 121 p.setLayout(bagLayout); //使用GridBagLayout布局管理器 122 constraints.fill = GridBagConstraints.BOTH; //组件填充显示区域 123 constraints.weightx = 0.0; // 124 125 //姓名 126 constraints.gridy = 0; 127 constraints.gridx = 0; 128 constraints.gridwidth = 0; 129 JPanel tempPanel = new JPanel(); 130 tempPanel.add(nameLabel); 131 tempPanel.add(nameTextField); 132 p.add(tempPanel, constraints); 133 134 //性别 135 constraints.gridy = 1; 136 genderBox.removeAllItems(); 137 genderBox.addItem(Gender.MAN); 138 genderBox.addItem(Gender.FEMALE); 139 genderBox.addItem(Gender.OTHER); 140 genderBox.addItem(""); 141 System.out.println("筛选器性别:" + screen.getGender()); 142 constraints.gridx = 0; 143 constraints.gridwidth = 0; 144 tempPanel = new JPanel(); 145 tempPanel.add(genderLabel); 146 tempPanel.add(genderBox); 147 p.add(tempPanel, constraints); 148 149 //id 150 constraints.gridy = 2; 151 constraints.gridx = 0; 152 constraints.gridwidth = 0; 153 tempPanel = new JPanel(); 154 tempPanel.add(idLabel); 155 tempPanel.add(idTextField); 156 p.add(tempPanel, constraints); 157 158 //学校 159 constraints.gridy = 3; 160 constraints.gridx = 0; 161 constraints.gridwidth = 0; 162 tempPanel = new JPanel(); 163 tempPanel.add(schoolLabel); 164 tempPanel.add(schoolTextField); 165 p.add(tempPanel, constraints); 166 167 // 班级 168 169 constraints.gridx = 0; 170 constraints.gridy = 4; 171 constraints.gridwidth = 0; 172 tempPanel = new JPanel(); 173 tempPanel.add(classLabel); 174 tempPanel.add(classTextField); 175 p.add(tempPanel, constraints); 176 177 178 //leftDate 179 constraints.gridx = 0; 180 constraints.gridy = 1; 181 constraints.gridwidth = 4; 182 leftDatePanel.add(startDateLabel, constraints); 183 leftYearBox.removeAllItems(); 184 for (int i = Calendar.getInstance().get(Calendar.YEAR); i >= MyDate.MIN_YEAR; i--) { 185 leftYearBox.addItem(i); 186 } 187 leftYearBox.setSelectedItem(screen.getLeftDate().getY()); 188 constraints.gridx = 5; 189 constraints.gridwidth = 4; 190 leftDatePanel.add(leftYearBox, constraints); 191 constraints.gridx = 10; 192 constraints.gridwidth = 4; 193 leftDatePanel.add(leftYearLabel, constraints); 194 195 leftMnthBox.removeAllItems(); 196 for (int i = 1; i < 13; i++) { 197 leftMnthBox.addItem(i); 198 } 199 leftMnthBox.setSelectedItem(screen.getLeftDate().getM()); 200 constraints.gridx = 15; 201 constraints.gridwidth = 2; 202 leftDatePanel.add(leftMnthBox, constraints); 203 constraints.gridx = 18; 204 constraints.gridwidth = 4; 205 leftDatePanel.add(leftMonthLabel, constraints); 206 207 for (int i = 1; i < 32; i++) { 208 leftDayBox.addItem(i); 209 } 210 leftDayBox.setSelectedItem(screen.getLeftDate().getD()); 211 constraints.gridx = 23; 212 constraints.gridwidth = 2; 213 leftDatePanel.add(leftDayBox, constraints); 214 constraints.gridx = 26; 215 constraints.gridy = 0; 216 constraints.gridwidth = 0; 217 leftDatePanel.add(leftDayLabel, constraints); 218 219 //rightDate 220 rightYearBox.removeAllItems(); 221 constraints.gridx = 0; 222 constraints.gridy = 1; 223 constraints.gridwidth = 4; 224 rightDatePanel.add(endDateLabel, constraints); 225 for (int i = Calendar.getInstance().get(Calendar.YEAR); i >= MyDate.MIN_YEAR; i--) { 226 rightYearBox.addItem(i); 227 } 228 rightYearBox.setSelectedItem(screen.getRightDate().getY()); 229 constraints.gridx = 5; 230 constraints.gridwidth = 4; 231 rightDatePanel.add(rightYearBox, constraints); 232 constraints.gridx = 10; 233 constraints.gridwidth = 4; 234 rightDatePanel.add(rightYearLabel, constraints); 235 236 rightMnthBox.removeAllItems(); 237 for (int i = 1; i < 13; i++) { 238 rightMnthBox.addItem(i); 239 } 240 rightMnthBox.setSelectedItem(screen.getRightDate().getM()); 241 constraints.gridx = 15; 242 constraints.gridwidth = 2; 243 rightDatePanel.add(rightMnthBox, constraints); 244 constraints.gridx = 18; 245 constraints.gridwidth = 4; 246 rightDatePanel.add(rightMonthLabel, constraints); 247 248 for (int i = 1; i < 32; i++) { 249 rightDayBox.addItem(i); 250 } 251 rightDayBox.setSelectedItem(screen.getRightDate().getD()); 252 constraints.gridx = 23; 253 constraints.gridwidth = 2; 254 rightDatePanel.add(rightDayBox, constraints); 255 constraints.gridx = 26; 256 constraints.gridy = 0; 257 constraints.gridwidth = 0; 258 rightDatePanel.add(rightDayLabel, constraints); 259 260 //end 261 262 constraints.gridx = 0; 263 constraints.gridy = 5; 264 constraints.gridwidth = 29; 265 p.add(leftDatePanel, constraints); 266 267 constraints.gridy = 7; 268 constraints.gridwidth = 29; 269 p.add(rightDatePanel, constraints); 270 271 constraints.gridwidth = 1; 272 constraints.gridy = 9; 273 p.add(new JPanel(), constraints); 274 275 JButton buttonS = new JButton("筛选"); 276 buttonS.setBackground(Color.white); 277 278 JButton buttonB = new JButton("清空筛选条件"); 279 buttonB.setBackground(Color.white); 280 281 282 JPanel buPanel = new JPanel(); 283 buPanel.add(buttonS); 284 buPanel.add(buttonB); 285 286 constraints.gridy = 12; 287 p.add(buPanel, constraints); 288 289 //帅选七初始化 290 nameTextField.setText(screen.getName()); 291 genderBox.setSelectedItem(screen.getGender()); 292 idTextField.setText(screen.getStudentID()); 293 classTextField.setText(screen.getStudentClass()); 294 schoolTextField.setText(screen.getSchool()); 295 leftYearBox.setSelectedItem(screen.getLeftDate().getY()); 296 leftMnthBox.setSelectedItem(screen.getLeftDate().getM()); 297 leftDayBox.setSelectedItem(screen.getLeftDate().getD()); 298 299 rightYearBox.setSelectedItem(screen.getRightDate().getY()); 300 rightMnthBox.setSelectedItem(screen.getRightDate().getM()); 301 rightDayBox.setSelectedItem(screen.getRightDate().getD()); 302 System.out.println("筛选刷新成功!"); 303 304 305 leftYearBox.addActionListener(new ActionListener() { 306 @Override 307 public void actionPerformed(ActionEvent e) { 308 if ( leftYearBox.getSelectedItem() == null || 309 leftMnthBox.getSelectedItem() == null){ 310 return; 311 } 312 int y = (int) leftYearBox.getSelectedItem(); 313 int m = (int) leftMnthBox.getSelectedItem(); 314 Calendar calendar = Calendar.getInstance(); 315 calendar.set(y - 1900, m - 1, 1); 316 calendar.roll(Calendar.DATE, -1); 317 int max = calendar.get(Calendar.DATE); 318 leftDayBox.removeAllItems(); 319 for (int i = 1; i <= max; i++) { 320 leftDayBox.addItem(i); 321 } 322 } 323 }); 324 325 leftMnthBox.addActionListener(new ActionListener() { 326 @Override 327 public void actionPerformed(ActionEvent e) { 328 if ( leftYearBox.getSelectedItem() == null || 329 leftMnthBox.getSelectedItem() == null){ 330 return; 331 } 332 int m = (int) leftMnthBox.getSelectedItem(); 333 int y = (int) leftYearBox.getSelectedItem(); 334 Calendar calendar = Calendar.getInstance(); 335 calendar.set(y - 1900, m - 1, 1); 336 calendar.roll(Calendar.DATE, -1); 337 int max = calendar.get(Calendar.DATE); 338 leftDayBox.removeAllItems(); 339 for (int i = 1; i <= max; i++) { 340 leftDayBox.addItem(i); 341 } 342 } 343 }); 344 345 rightYearBox.addActionListener(new ActionListener() { 346 @Override 347 public void actionPerformed(ActionEvent e) { 348 if ( rightYearBox.getSelectedItem() == null || 349 rightMnthBox.getSelectedItem() == null){ 350 return; 351 } 352 int y = (int) rightYearBox.getSelectedItem(); 353 int m = (int) rightMnthBox.getSelectedItem(); 354 Calendar calendar = Calendar.getInstance(); 355 calendar.set(y - 1900, m - 1, 1); 356 calendar.roll(Calendar.DATE, -1); 357 int max = calendar.get(Calendar.DATE); 358 rightDayBox.removeAllItems(); 359 for (int i = 1; i <= max; i++) { 360 rightDayBox.addItem(i); 361 } 362 } 363 }); 364 365 rightMnthBox.addActionListener(new ActionListener() { 366 @Override 367 public void actionPerformed(ActionEvent e) { 368 if ( rightYearBox.getSelectedItem() == null || 369 rightMnthBox.getSelectedItem() == null){ 370 return; 371 } 372 int m = (int) rightMnthBox.getSelectedItem(); 373 int y = (int) rightYearBox.getSelectedItem(); 374 Calendar calendar = Calendar.getInstance(); 375 calendar.set(y - 1900, m - 1, 1); 376 calendar.roll(Calendar.DATE, -1); 377 int max = calendar.get(Calendar.DATE); 378 rightDayBox.removeAllItems(); 379 for (int i = 1; i <= max; i++) { 380 rightDayBox.addItem(i); 381 } 382 } 383 }); 384 385 386 387 /** 388 * 这是 过滤按钮 389 */ 390 buttonS.addActionListener(new ActionListener() { 391 @Override 392 public void actionPerformed(ActionEvent e) { 393 System.out.print("SCREEN :date start:" + screen.getLeftDate().toString()); 394 System.out.print("SCREEN :date end:" + screen.getRightDate().toString()); 395 screen.setName(nameTextField.getText()); 396 screen.setLeftDate(new MyDate((int) leftYearBox.getSelectedItem(), (int) leftMnthBox.getSelectedItem(), (int) leftDayBox.getSelectedItem())); 397 screen.setRightDate(new MyDate((int) rightYearBox.getSelectedItem(), (int) rightMnthBox.getSelectedItem(), (int) rightDayBox.getSelectedItem())); 398 screen.setStudentID(idTextField.getText()); 399 screen.setGender((String) genderBox.getSelectedItem()); 400 screen.setSchool(schoolTextField.getText()); 401 screen.setStudentClass(classTextField.getText()); 402 if (!dateIsRight()) { 403 JOptionPane.showMessageDialog(null, "开始日期不能大于结束日期!", "ERROR", JOptionPane.ERROR_MESSAGE); 404 return; 405 } 406 System.out.println("\n筛选器设置为:" + idTextField.getText()+ screen.toString()); 407 TABLE.refresh(); 408 } 409 410 private Boolean dateIsRight() { 411 int flag = screen.getLeftDate().myEquals(screen.getRightDate()); 412 return flag > 0 ? false : true; 413 } 414 415 }); 416 //清空过滤 417 buttonB.addActionListener(new ActionListener() { 418 @Override 419 public void actionPerformed(ActionEvent e) { 420 screen = new Screen(); 421 System.out.println("\n新建筛选器:" + screen.toString()); 422 refresh(); 423 TABLE.refresh(); 424 } 425 }); 426 return p; 427 } 428 429 public JLabel getNameLabel() { 430 return nameLabel; 431 } 432 433 public void setNameLabel(JLabel nameLabel) { 434 this.nameLabel = nameLabel; 435 } 436 437 public JLabel getGenderLabel() { 438 return genderLabel; 439 } 440 441 public void setGenderLabel(JLabel genderLabel) { 442 this.genderLabel = genderLabel; 443 } 444 445 public JLabel getBirthLabel() { 446 return birthLabel; 447 } 448 449 public void setBirthLabel(JLabel birthLabel) { 450 this.birthLabel = birthLabel; 451 } 452 453 public JLabel getYearLabel() { 454 return yearLabel; 455 } 456 457 public void setYearLabel(JLabel yearLabel) { 458 this.yearLabel = yearLabel; 459 } 460 461 public JLabel getMonthLabel() { 462 return monthLabel; 463 } 464 465 public void setMonthLabel(JLabel monthLabel) { 466 this.monthLabel = monthLabel; 467 } 468 469 public JLabel getDayLabel() { 470 return dayLabel; 471 } 472 473 public void setDayLabel(JLabel dayLabel) { 474 this.dayLabel = dayLabel; 475 } 476 477 public JLabel getIdLabel() { 478 return idLabel; 479 } 480 481 public void setIdLabel(JLabel idLabel) { 482 this.idLabel = idLabel; 483 } 484 485 public JLabel getClassLabel() { 486 return classLabel; 487 } 488 489 public void setClassLabel(JLabel classLabel) { 490 this.classLabel = classLabel; 491 } 492 493 public JLabel getSchoolLabel() { 494 return schoolLabel; 495 } 496 497 public void setSchoolLabel(JLabel schoolLabel) { 498 this.schoolLabel = schoolLabel; 499 } 500 501 public JLabel getOtherLabel() { 502 return otherLabel; 503 } 504 505 public void setOtherLabel(JLabel otherLabel) { 506 this.otherLabel = otherLabel; 507 } 508 509 public JComboBox getGenderBox() { 510 return genderBox; 511 } 512 513 public void setGenderBox(JComboBox genderBox) { 514 this.genderBox = genderBox; 515 } 516 517 public JComboBox getAgeYearBox() { 518 return ageYearBox; 519 } 520 521 public void setAgeYearBox(JComboBox ageYearBox) { 522 this.ageYearBox = ageYearBox; 523 } 524 525 public JComboBox getMonthBox() { 526 return monthBox; 527 } 528 529 public void setMonthBox(JComboBox monthBox) { 530 this.monthBox = monthBox; 531 } 532 533 public JComboBox getDayBox() { 534 return dayBox; 535 } 536 537 public void setDayBox(JComboBox dayBox) { 538 this.dayBox = dayBox; 539 } 540 541 public JTextField getNameTextField() { 542 return nameTextField; 543 } 544 545 public void setNameTextField(JTextField nameTextField) { 546 this.nameTextField = nameTextField; 547 } 548 549 public JTextField getIdTextField() { 550 return idTextField; 551 } 552 553 public void setIdTextField(JTextField idTextField) { 554 this.idTextField = idTextField; 555 } 556 557 public JTextField getClassTextField() { 558 return classTextField; 559 } 560 561 public void setClassTextField(JTextField classTextField) { 562 this.classTextField = classTextField; 563 } 564 565 public JTextField getSchoolTextField() { 566 return schoolTextField; 567 } 568 569 public void setSchoolTextField(JTextField schoolTextField) { 570 this.schoolTextField = schoolTextField; 571 } 572 573 public JTextArea getOtherTextField() { 574 return otherTextField; 575 } 576 577 public void setOtherTextField(JTextArea otherTextField) { 578 this.otherTextField = otherTextField; 579 } 580 581 public JLabel getStartDateLabel() { 582 return startDateLabel; 583 } 584 585 public void setStartDateLabel(JLabel startDateLabel) { 586 this.startDateLabel = startDateLabel; 587 } 588 589 public JLabel getLeftYearLabel() { 590 return leftYearLabel; 591 } 592 593 public void setLeftYearLabel(JLabel leftYearLabel) { 594 this.leftYearLabel = leftYearLabel; 595 } 596 597 public JLabel getLeftMonthLabel() { 598 return leftMonthLabel; 599 } 600 601 public void setLeftMonthLabel(JLabel leftMonthLabel) { 602 this.leftMonthLabel = leftMonthLabel; 603 } 604 605 public JLabel getLeftDayLabel() { 606 return leftDayLabel; 607 } 608 609 public void setLeftDayLabel(JLabel leftDayLabel) { 610 this.leftDayLabel = leftDayLabel; 611 } 612 613 public JComboBox getLeftYearBox() { 614 return leftYearBox; 615 } 616 617 public void setLeftYearBox(JComboBox leftYearBox) { 618 this.leftYearBox = leftYearBox; 619 } 620 621 public JComboBox getLeftMnthBox() { 622 return leftMnthBox; 623 } 624 625 public void setLeftMnthBox(JComboBox leftMnthBox) { 626 this.leftMnthBox = leftMnthBox; 627 } 628 629 public JComboBox getLeftDayBox() { 630 return leftDayBox; 631 } 632 633 public void setLeftDayBox(JComboBox leftDayBox) { 634 this.leftDayBox = leftDayBox; 635 } 636 637 public JPanel getRightDatePanel() { 638 return rightDatePanel; 639 } 640 641 public void setRightDatePanel(JPanel rightDatePanel) { 642 this.rightDatePanel = rightDatePanel; 643 } 644 645 public JLabel getEndDateLabel() { 646 return endDateLabel; 647 } 648 649 public void setEndDateLabel(JLabel endDateLabel) { 650 this.endDateLabel = endDateLabel; 651 } 652 653 public JLabel getRightYearLabel() { 654 return rightYearLabel; 655 } 656 657 public void setRightYearLabel(JLabel rightYearLabel) { 658 this.rightYearLabel = rightYearLabel; 659 } 660 661 public JLabel getRightMonthLabel() { 662 return rightMonthLabel; 663 } 664 665 public void setRightMonthLabel(JLabel rightMonthLabel) { 666 this.rightMonthLabel = rightMonthLabel; 667 } 668 669 public JLabel getRightDayLabel() { 670 return rightDayLabel; 671 } 672 673 public void setRightDayLabel(JLabel rightDayLabel) { 674 this.rightDayLabel = rightDayLabel; 675 } 676 677 public JComboBox getRightYearBox() { 678 return rightYearBox; 679 } 680 681 public void setRightYearBox(JComboBox rightYearBox) { 682 this.rightYearBox = rightYearBox; 683 } 684 685 public JComboBox getRightMnthBox() { 686 return rightMnthBox; 687 } 688 689 public void setRightMnthBox(JComboBox rightMnthBox) { 690 this.rightMnthBox = rightMnthBox; 691 } 692 693 public JComboBox getRightDayBox() { 694 return rightDayBox; 695 } 696 697 public void setRightDayBox(JComboBox rightDayBox) { 698 this.rightDayBox = rightDayBox; 699 } 700 701 public JPanel getMAIN_PANEL() { 702 return MAIN_PANEL; 703 } 704 705 public void setMAIN_PANEL(JPanel MAIN_PANEL) { 706 this.MAIN_PANEL = MAIN_PANEL; 707 } 708 709 public Screen getScreen() { 710 return screen; 711 } 712 713 public void setScreen(Screen screen) { 714 this.screen = screen; 715 } 716 717 public JPanel getLeftDatePanel() { 718 return leftDatePanel; 719 } 720 721 public void setLeftDatePanel(JPanel leftDatePanel) { 722 this.leftDatePanel = leftDatePanel; 723 } 724 725 }public class MyScreenPanel
![java 简单 学生管理系统 - 文章图片](/upload/getfiles/0001/2021/5/1/20210501114124442.jpg)
![java 简单 学生管理系统 - 文章图片](/upload/getfiles/0001/2021/5/1/20210501114124479.jpg)
1 package studentGUI; 2 3 import javax.swing.*; 4 import javax.swing.table.DefaultTableModel; 5 import javax.swing.table.TableCellEditor; 6 import javax.swing.table.TableCellRenderer; 7 import java.awt.*; 8 import java.awt.event.ActionEvent; 9 import java.awt.event.ActionListener; 10 11 public class ButtonTable extends JFrame{ 12 private static final long serialVersionUID = 1L; 13 private JTable table = null; 14 private DefaultTableModel model=null; 15 private JScrollPane js=null ; 16 public ButtonTable(){ 17 model = new DefaultTableModel(3,3); 18 table = new JTable(model); 19 table.getColumnModel().getColumn(1).setCellEditor(new MyRender());//设置编辑器 20 table.getColumnModel().getColumn(1).setCellRenderer(new MyRender() ); 21 js = new JScrollPane(table); 22 this.add(js); 23 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 24 this.setVisible(true); 25 this.setSize(399, 300); 26 this.setLocationRelativeTo(null); 27 } 28 public static void main(String[] args) { 29 // TODO Auto-generated method stub 30 new ButtonTable(); 31 32 DefaultTableModel model = new DefaultTableModel(); 33 JTable table = new JTable(model); 34 35 model.addColumn("Col1"); 36 model.addColumn("Col2"); 37 38 model.addRow(new Object[] { "v1", "v2" }); 39 40 model.addRow(new Object[] { "v1" }); 41 42 model.addRow(new Object[] { "v1", "v2", "v3" }); 43 44 JFrame f = new JFrame(); 45 f.setSize(300, 300); 46 f.add(new JScrollPane(table)); 47 f.setVisible(true); 48 49 } 50 } 51 //渲染 器 编辑器 52 53 class MyRender extends AbstractCellEditor implements TableCellRenderer,ActionListener, TableCellEditor{ 54 private static final long serialVersionUID = 1L; 55 private JButton button =null; 56 public MyRender(){ 57 button = new JButton("确定不4102?"); 58 button.addActionListener(this); 59 } 60 @Override 61 public Object getCellEditorValue() { 62 // TODO Auto-generated method stub 63 return null; 64 } 65 @Override 66 public Component getTableCellRendererComponent(JTable table, Object value, 67 boolean isSelected, boolean hasFocus, int row, int column) { 68 // TODO Auto-generated method stub 69 return button; 70 } 71 // @Override 72 // public void actionPerformed(ActionEvent e) { 73 //// TODO Auto-generated method stub 74 // JOptionPane.showMessageDialog(null, "渲染器学期1653", "消息", JOptionPane.OK_OPTION); 75 // } 76 77 @Override 78 public void actionPerformed(ActionEvent e) { 79 // TODO Auto-generated method stub 80 int flag = JOptionPane.showConfirmDialog(null, "确定删除该条记录?", "警告", JOptionPane.YES_NO_OPTION); 81 if (flag == JOptionPane.YES_OPTION){ 82 83 84 } 85 } 86 87 @Override 88 public Component getTableCellEditorComponent(JTable table, Object value, 89 boolean isSelected, int row, int column) { 90 // TODO Auto-generated method stub 91 return button; 92 } 93 }public class ButtonTable extends JFrame
![java 简单 学生管理系统 - 文章图片](/upload/getfiles/0001/2021/5/1/20210501114124442.jpg)
![java 简单 学生管理系统 - 文章图片](/upload/getfiles/0001/2021/5/1/20210501114124479.jpg)
1 package studentGUI; 2 3 import javax.swing.*; 4 import javax.swing.event.CaretEvent; 5 import javax.swing.event.CaretListener; 6 import java.awt.*; 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener; 9 import java.util.Calendar; 10 11 public class MyTopPanel { 12 JSplitPane MAIN_PANEL; 13 String INSERT_VALUE; 14 String CLEAN_VALUE; 15 16 17 // 全局对象 交换信息用 18 private StudentList STUDENTLIST; 19 private Screen screen; 20 private MyTopPanel topPanel; 21 private MyTable TABLE; 22 private MyScreenPanel screenPanel; 23 24 25 Student student; 26 JButton button; 27 JButton buttonC; 28 JSplitPane jSplitPane; //fresh 对象 29 JLabel nameLabel; 30 JLabel genderLabel ; 31 JLabel birthLabel ; 32 JLabel yearLabel; 33 JLabel monthLabel; 34 JLabel dayLabel; 35 JLabel idLabel; 36 JLabel classLabel; 37 JLabel schoolLabel; 38 JLabel otherLabel; 39 JComboBox genderBox; 40 JComboBox ageYearBox; 41 JComboBox monthBox; 42 JComboBox dayBox; 43 JTextField nameTextField; 44 JTextField idTextField ; 45 JTextField classTextField ; 46 JTextField schoolTextField ; 47 JTextArea otherTextField ; 48 JPanel birthPanel; 49 50 51 MyTopPanel(){ 52 MAIN_PANEL = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); 53 student = new Student(); 54 jSplitPane = new JSplitPane(); 55 nameLabel = new JLabel("姓名:"); 56 nameTextField = new JTextField(8); 57 genderLabel = new JLabel("性别:"); 58 genderBox = new JComboBox(); //创建JComboBox 59 birthLabel = new JLabel("年龄:"); 60 ageYearBox = new JComboBox(); //创建JComboBox 61 yearLabel = new JLabel("年"); 62 monthBox = new JComboBox(); //创建JComboBox 63 monthLabel = new JLabel("月"); 64 dayBox = new JComboBox(); //创建JComboBox 65 dayLabel = new JLabel(""); 66 idTextField = new JTextField(10); 67 idLabel = new JLabel("学号:"); 68 classTextField = new JTextField(10); 69 classLabel = new JLabel("班级:"); 70 schoolTextField = new JTextField(15); 71 schoolLabel = new JLabel("学校:"); 72 otherTextField = new JTextArea(); 73 otherLabel = new JLabel("备注:"); 74 birthPanel = new JPanel(); 75 INSERT_VALUE = "插入"; 76 CLEAN_VALUE = "清除"; 77 78 } 79 80 public void set(MyScreenPanel screenPanel, MyTable myTable, MyTopPanel myTopPanel, StudentList list){ 81 this.screenPanel = screenPanel; 82 this.topPanel = myTopPanel; 83 this.STUDENTLIST = list; 84 this.screenPanel = screenPanel; 85 this.TABLE = myTable; 86 } 87 public Student getStudent() { 88 return student; 89 } 90 91 public void setStudent(Student student) { 92 this.student = student; 93 } 94 95 public void refresh(){ 96 setPanel(); 97 MAIN_PANEL.setRightComponent(screenPanel.getScreenPanel()); 98 } 99 100 public JSplitPane getTopPanel() { 101 MAIN_PANEL = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); 102 setPanel(); 103 MAIN_PANEL.setRightComponent(screenPanel.getScreenPanel()); 104 return MAIN_PANEL; 105 } 106 public JSplitPane setPanel() { 107 JPanel p = new JPanel(); 108 GridBagLayout bagLayout = new GridBagLayout(); //创建GridBagLayout布局管理器 109 GridBagConstraints constraints = new GridBagConstraints(); 110 p.setLayout(bagLayout); //使用GridBagLayout布局管理器 111 constraints.fill = GridBagConstraints.BOTH; //组件填充显示区域 112 constraints.weightx = 0.0; // 113 114 birthPanel.setLayout(bagLayout); 115 116 constraints.gridx = 2; 117 constraints.gridy = 0; 118 constraints.gridwidth = 3; 119 p.add(nameLabel, constraints); 120 121 constraints.gridx = 6; 122 constraints.gridwidth = 9; 123 p.add(nameTextField, constraints); 124 125 constraints.gridx = 0; 126 constraints.gridwidth = 4; 127 birthPanel.add(birthLabel, constraints); 128 129 ageYearBox.removeAllItems(); 130 for (int i = Calendar.getInstance().get(Calendar.YEAR); i > 1900; i--) { 131 ageYearBox.addItem(i); 132 } 133 ageYearBox.setSelectedItem(2000); 134 constraints.gridx = 5; 135 constraints.gridwidth = 4; 136 birthPanel.add(ageYearBox, constraints); 137 138 constraints.gridx = 10; 139 constraints.gridwidth = 4; 140 birthPanel.add(yearLabel, constraints); 141 142 monthBox.removeAllItems(); 143 for (int i = 1; i < 13; i++) { 144 monthBox.addItem(i); 145 } 146 monthBox.setSelectedItem(1); 147 constraints.gridx = 15; 148 constraints.gridwidth = 2; 149 birthPanel.add(monthBox, constraints); 150 151 152 constraints.gridx = 18; 153 constraints.gridwidth = 4; 154 birthPanel.add(monthLabel, constraints); 155 156 for (int i = 1; i < 32; i++) { 157 dayBox.addItem(i); 158 } 159 dayBox.setSelectedItem(1); 160 constraints.gridx = 23; 161 constraints.gridwidth = 2; 162 if (student.getBirthDate() == null) { 163 student.setBirth(2000, 1, 1); 164 } 165 birthPanel.add(dayBox, constraints); 166 167 constraints.gridx = 26; 168 constraints.gridy = 0; 169 constraints.gridwidth = 0; 170 birthPanel.add(dayLabel, constraints); 171 172 173 constraints.gridx = 15; 174 constraints.gridy = 0; 175 constraints.gridwidth = 29; 176 p.add(birthPanel, constraints); 177 178 constraints.gridx = 45; 179 constraints.gridwidth = 1; 180 p.add(new JPanel(), constraints); 181 182 constraints.gridx = 46; 183 constraints.gridy = 0; 184 constraints.gridwidth = 3; 185 p.add(genderLabel, constraints); 186 187 188 genderBox.removeAllItems(); 189 genderBox.addItem(Gender.MAN); 190 genderBox.addItem(Gender.FEMALE); 191 genderBox.addItem(Gender.OTHER); 192 constraints.gridx = 50; 193 constraints.gridy = 0; 194 constraints.gridwidth = 0; 195 p.add(genderBox, constraints); 196 197 198 constraints.gridx = 2; 199 constraints.gridy = 2; 200 constraints.gridwidth = 3; 201 p.add(schoolLabel, constraints); 202 203 204 constraints.gridx = 6; 205 constraints.gridwidth = 15; 206 p.add(schoolTextField, constraints); 207 208 constraints.gridx = 22; 209 constraints.gridwidth = 3; 210 p.add(classLabel, constraints); 211 212 constraints.gridx = 26; 213 constraints.gridwidth = 15; 214 p.add(classTextField, constraints); 215 216 constraints.gridx = 42; 217 p.add(idLabel, constraints); 218 219 constraints.gridx = 50; 220 p.add(idTextField, constraints); 221 222 constraints.gridx = 2; 223 constraints.gridy = 4; 224 constraints.gridwidth = 3; 225 constraints.gridheight = 4; 226 p.add(otherLabel, constraints); 227 228 constraints.gridy = 1; 229 constraints.gridheight = 1; 230 p.add(new JPanel(), constraints); 231 constraints.gridy = 3; 232 p.add(new JPanel(), constraints); 233 234 235 constraints.gridx = 6; 236 constraints.gridy = 4; 237 constraints.gridwidth = 0; 238 constraints.gridheight = 4; 239 otherTextField.setRows(4); 240 otherTextField.setColumns(30); 241 otherTextField.setLineWrap(true); 242 otherTextField.setAutoscrolls(true); 243 p.add(otherTextField, constraints); 244 245 JPanel buttonPanel = new JPanel(); 246 buttonPanel.setLayout(new GridLayout(1, 8, 0, 0)); 247 JButton button = new JButton(INSERT_VALUE); 248 button.setBackground(Color.white); 249 buttonC = new JButton(CLEAN_VALUE); 250 buttonC.setBackground(Color.white); 251 buttonPanel.add(button); 252 buttonPanel.add(buttonC); 253 254 255 constraints.gridx = 6; 256 constraints.gridy = 9; 257 constraints.gridheight = 1; 258 p.add(new JPanel(), constraints); 259 constraints.gridy = 10; 260 p.add(buttonPanel, constraints); 261 262 //初始化 263 nameTextField.setText(student.getName()); 264 genderBox.setSelectedItem(student.getGender()); 265 schoolTextField.setText(student.getSchool()); 266 classTextField.setText(student.getStudentClass()); 267 idTextField.setText(student.getStudentID()); 268 otherTextField.setText(student.getOthers()); 269 ageYearBox.setSelectedItem(student.getBirthDate().getY()); 270 monthBox.setSelectedItem(student.getBirthDate().getM()); 271 dayBox.setSelectedItem(student.getBirthDate().getD()); 272 273 274 MAIN_PANEL.setLeftComponent(p); 275 MAIN_PANEL.getLeftComponent().setMinimumSize(new Dimension(500, 600)); 276 277 278 279 ageYearBox.addActionListener(new ActionListener() { 280 @Override 281 public void actionPerformed(ActionEvent e) { 282 if (ageYearBox.getSelectedItem() == null || 283 monthBox.getSelectedItem() == null){ 284 return; 285 } 286 int y = (int) ageYearBox.getSelectedItem(); 287 int m = (int) monthBox.getSelectedItem(); 288 Calendar calendar = Calendar.getInstance(); 289 calendar.set(y - 1900, m - 1, 1); 290 calendar.roll(Calendar.DATE, -1); 291 int max = calendar.get(Calendar.DATE); 292 dayBox.removeAllItems(); 293 for (int i = 1; i <= max; i++) { 294 dayBox.addItem(i); 295 } 296 } 297 }); 298 299 monthBox.addActionListener(new ActionListener() { 300 @Override 301 public void actionPerformed(ActionEvent e) { 302 if (ageYearBox.getSelectedItem() == null || 303 monthBox.getSelectedItem() == null){ 304 return; 305 } 306 int m = (int) monthBox.getSelectedItem();222222222 307 int y = (int) ageYearBox.getSelectedItem(); 308 Calendar calendar = Calendar.getInstance(); 309 calendar.set(y - 1900, m - 1, 1); 310 calendar.roll(Calendar.DATE, -1); 311 int max = calendar.get(Calendar.DATE); 312 dayBox.removeAllItems(); 313 for (int i = 1; i <= max; i++) { 314 dayBox.addItem(i); 315 } 316 } 317 }); 318 319 320 /** 321 * 清空按钮 322 */ 323 buttonC.addActionListener(new ActionListener() { 324 @Override 325 public void actionPerformed(ActionEvent e) { 326 System.out.println("you Click INSERT!"); 327 // if (buttonC.getText().equals("删除")){ 328 // STUDENTLIST.deleteById(student.getStudentID()); 329 // JOptionPane.showConfirmDialog(null, "成功!", "提示", 330 // JOptionPane.PLAIN_MESSAGE); 331 INSERT_VALUE = "插入"; 332 // CLEAN_VALUE = "清空"; 333 // } 334 student = new Student(); 335 refresh(); 336 } 337 }); 338 /** 339 * 插入按钮 340 */ 341 button.addActionListener(new ActionListener() { 342 @Override 343 public void actionPerformed(ActionEvent e) { 344 System.out.println("you press INSERT!"); 345 student.setName(nameTextField.getText()); 346 student.setBirth((int) ageYearBox.getSelectedItem(), (int) monthBox.getSelectedItem(), (int) dayBox.getSelectedItem()); 347 student.setStudentID(idTextField.getText()); 348 student.setGender((String) genderBox.getSelectedItem()); 349 student.setSchool(schoolTextField.getText()); 350 student.setStudentClass(classTextField.getText()); 351 student.setSchool(schoolTextField.getText()); 352 if (allFilled().length() != 0) { 353 JOptionPane.showMessageDialog(null, "请输入:" + allFilled() + "字段", "Error", JOptionPane.ERROR_MESSAGE); 354 return; 355 } 356 if (button.getText().equals("保存")){ 357 JOptionPane.showConfirmDialog(null, "成功!", "提示", 358 JOptionPane.PLAIN_MESSAGE); 359 INSERT_VALUE = "插入"; 360 CLEAN_VALUE = "清空"; 361 student = new Student(); 362 refresh(); 363 TABLE.refresh(); 364 return; 365 } 366 if (STUDENTLIST.getById(student.getStudentID()) != null) { 367 int rs = JOptionPane.showConfirmDialog(null, "ID 已存在,继续将覆盖原来数据,继续??", "警告", 368 JOptionPane.YES_NO_OPTION); 369 if (rs == JOptionPane.YES_OPTION) { 370 STUDENTLIST.updateStudent(student); 371 } 372 return; 373 } 374 System.out.println("\n插入学生为:" + student.toString()); 375 STUDENTLIST.add(student); 376 student = new Student(); 377 refresh(); 378 TABLE.refresh(); 379 } 380 381 382 public String allFilled() { 383 if (student.getGender() == null || student.getGender().replace(" ", "").length() == 0) { 384 return "性别"; 385 } else if (student.getSchool() == null || student.getSchool().replace(" ", "").length() == 0) { 386 return "学校"; 387 } else if (student.getName() == null || student.getName().replace(" ", "").length() == 0) { 388 return "姓名"; 389 } else if (student.getStudentID() == null || student.getStudentID().replace(" ", "").length() == 0) { 390 return "学号"; 391 } else { 392 return ""; 393 } 394 } 395 }); 396 397 otherTextField.addCaretListener(new CaretListener() { 398 @Override 399 public void caretUpdate(CaretEvent e) { 400 student.setOthers(otherTextField.getText()); 401 } 402 }); 403 404 return MAIN_PANEL; 405 } 406 407 public String getINSERT_VALUE() { 408 return INSERT_VALUE; 409 } 410 411 public void setINSERT_VALUE(String INSERT_VALUE) { 412 this.INSERT_VALUE = INSERT_VALUE; 413 } 414 415 public String getCLEAN_VALUE() { 416 return CLEAN_VALUE; 417 } 418 419 public void setCLEAN_VALUE(String CLEAN_VALUE) { 420 this.CLEAN_VALUE = CLEAN_VALUE; 421 } 422 423 public JSplitPane getjSplitPane() { 424 return jSplitPane; 425 } 426 427 public void setjSplitPane(JSplitPane jSplitPane) { 428 this.jSplitPane = jSplitPane; 429 } 430 431 public JLabel getNameLabel() { 432 return nameLabel; 433 } 434 435 public void setNameLabel(JLabel nameLabel) { 436 this.nameLabel = nameLabel; 437 } 438 439 public JLabel getGenderLabel() { 440 return genderLabel; 441 } 442 443 public void setGenderLabel(JLabel genderLabel) { 444 this.genderLabel = genderLabel; 445 } 446 447 public JLabel getBirthLabel() { 448 return birthLabel; 449 } 450 451 public void setBirthLabel(JLabel birthLabel) { 452 this.birthLabel = birthLabel; 453 } 454 455 public JLabel getYearLabel() { 456 return yearLabel; 457 } 458 459 public void setYearLabel(JLabel yearLabel) { 460 this.yearLabel = yearLabel; 461 } 462 463 public JLabel getMonthLabel() { 464 return monthLabel; 465 } 466 467 public void setMonthLabel(JLabel monthLabel) { 468 this.monthLabel = monthLabel; 469 } 470 471 public JLabel getDayLabel() { 472 return dayLabel; 473 } 474 475 public void setDayLabel(JLabel dayLabel) { 476 this.dayLabel = dayLabel; 477 } 478 479 public JLabel getIdLabel() { 480 return idLabel; 481 } 482 483 public void setIdLabel(JLabel idLabel) { 484 this.idLabel = idLabel; 485 } 486 487 public JLabel getClassLabel() { 488 return classLabel; 489 } 490 491 public void setClassLabel(JLabel classLabel) { 492 this.classLabel = classLabel; 493 } 494 495 public JLabel getSchoolLabel() { 496 return schoolLabel; 497 } 498 499 public void setSchoolLabel(JLabel schoolLabel) { 500 this.schoolLabel = schoolLabel; 501 } 502 503 public JLabel getOtherLabel() { 504 return otherLabel; 505 } 506 507 public void setOtherLabel(JLabel otherLabel) { 508 this.otherLabel = otherLabel; 509 } 510 511 public JComboBox getGenderBox() { 512 return genderBox; 513 } 514 515 public void setGenderBox(JComboBox genderBox) { 516 this.genderBox = genderBox; 517 } 518 519 public JComboBox getAgeYearBox() { 520 return ageYearBox; 521 } 522 523 public void setAgeYearBox(JComboBox ageYearBox) { 524 this.ageYearBox = ageYearBox; 525 } 526 527 public JComboBox getMonthBox() { 528 return monthBox; 529 } 530 531 public void setMonthBox(JComboBox monthBox) { 532 this.monthBox = monthBox; 533 } 534 535 public JComboBox getDayBox() { 536 return dayBox; 537 } 538 539 public void setDayBox(JComboBox dayBox) { 540 this.dayBox = dayBox; 541 } 542 543 public JTextField getNameTextField() { 544 return nameTextField; 545 } 546 547 public void setNameTextField(JTextField nameTextField) { 548 this.nameTextField = nameTextField; 549 } 550 551 public JTextField getIdTextField() { 552 return idTextField; 553 } 554 555 public void setIdTextField(JTextField idTextField) { 556 this.idTextField = idTextField; 557 } 558 559 public JTextField getClassTextField() { 560 return classTextField; 561 } 562 563 public void setClassTextField(JTextField classTextField) { 564 this.classTextField = classTextField; 565 } 566 567 public JTextField getSchoolTextField() { 568 return schoolTextField; 569 } 570 571 public void setSchoolTextField(JTextField schoolTextField) { 572 this.schoolTextField = schoolTextField; 573 } 574 575 public JTextArea getOtherTextField() { 576 return otherTextField; 577 } 578 579 public void setOtherTextField(JTextArea otherTextField) { 580 this.otherTextField = otherTextField; 581 } 582 583 public JPanel getBirthPanel() { 584 return birthPanel; 585 } 586 587 public void setBirthPanel(JPanel birthPanel) { 588 this.birthPanel = birthPanel; 589 } 590 591 public MyScreenPanel getScreenPanel() { 592 return screenPanel; 593 } 594 595 public void setScreenPanel(MyScreenPanel screenPanel) { 596 this.screenPanel = screenPanel; 597 } 598 public JSplitPane getMAIN_PANEL() { 599 return MAIN_PANEL; 600 } 601 602 public void setMAIN_PANEL(JSplitPane MAIN_PANEL) { 603 this.MAIN_PANEL = MAIN_PANEL; 604 } 605 606 }View Code
![java 简单 学生管理系统 - 文章图片](/upload/getfiles/0001/2021/5/1/20210501114124442.jpg)
![java 简单 学生管理系统 - 文章图片](/upload/getfiles/0001/2021/5/1/20210501114124479.jpg)
1 package studentGUI; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class StudentList { 7 List<Student> STUDENTS; 8 Screen screen; 9 10 StudentList(){ 11 STUDENTS = new ArrayList<Student>(); 12 } 13 14 StudentList(List<Student> list){ 15 STUDENTS = list; 16 } 17 18 public void deleteById(String id){ 19 List<Student> retList = new ArrayList<Student>(); 20 for (Student student : STUDENTS) { 21 if (!student.getStudentID().equals(id)) { 22 retList.add(student); 23 } 24 } 25 STUDENTS = retList; 26 } 27 28 public void add(Student student){ 29 STUDENTS.add(student); 30 } 31 public void updateStudent(Student student){ 32 Student temp = getById(student); 33 if (temp != null){ 34 temp = new Student(student); 35 } 36 } 37 38 public Student getById(Student student){ 39 for (Student val : STUDENTS) { 40 if (val.getStudentID().equals(student.getStudentID())) { 41 return student; 42 } 43 } 44 return null; 45 } 46 public Student getById(String id){ 47 for (Student val : STUDENTS) { 48 if (val.getStudentID().equals(id)) { 49 return val; 50 } 51 } 52 return null; 53 } 54 55 public List<Student> getSTUDENTS() { 56 return STUDENTS; 57 } 58 59 public void setSTUDENTS(List<Student> STUDENTS) { 60 this.STUDENTS = STUDENTS; 61 } 62 63 }Studentlist
![java 简单 学生管理系统 - 文章图片](/upload/getfiles/0001/2021/5/1/20210501114124442.jpg)
![java 简单 学生管理系统 - 文章图片](/upload/getfiles/0001/2021/5/1/20210501114124479.jpg)
1 package studentGUI; 2 3 4 import javax.swing.*; 5 import javax.swing.event.ListSelectionEvent; 6 import javax.swing.event.ListSelectionListener; 7 import javax.swing.table.DefaultTableModel; 8 import javax.swing.table.TableModel; 9 import javax.swing.table.TableRowSorter; 10 import java.awt.*; 11 import java.awt.event.ActionEvent; 12 import java.awt.event.ActionListener; 13 import java.util.List; 14 15 public class MyTable { 16 JScrollPane MAIN_PANEL; 17 JTable table; 18 DefaultTableModel model; 19 final String[] tableHeader = {"编号", "姓名", "性别", "年龄", "学号", "学校", "班级", "备注", "操作"}; 20 21 private StudentList STUDENTS; 22 private Screen screen; 23 private MyTopPanel topPanel; 24 private MyTable myTable; 25 private MyScreenPanel screenPanel; 26 27 MyTable(){ 28 MAIN_PANEL = new JScrollPane(); 29 model = new DefaultTableModel(); 30 table = new JTable(model); 31 for (String val : tableHeader) { 32 model.addColumn(val); 33 } 34 } 35 36 public void set(MyScreenPanel screenPanel,MyTable myTable,MyTopPanel myTopPanel,StudentList list){ 37 this.screenPanel = screenPanel; 38 this.myTable = myTable; 39 this.topPanel = myTopPanel; 40 this.STUDENTS = list; 41 this.screen = screenPanel.getScreen(); 42 } 43 44 public void setTable(JTable table) { 45 this.table = table; 46 } 47 48 public JScrollPane getPanel(){ 49 MAIN_PANEL = new JScrollPane(); 50 refresh(); 51 return MAIN_PANEL; 52 } 53 54 55 public void refresh(){ 56 57 model = new DefaultTableModel(); 58 table = new JTable(model); 59 for (String val : tableHeader) { 60 model.addColumn(val); 61 } 62 MAIN_PANEL.getViewport().add(getTable()); 63 for (int i = 0 ;i < table.getRowCount(); i++){ 64 System.out.println("正在移除 NO:" + i + "总行:" + model.getRowCount()); 65 } 66 System.out.println("当前list:" + STUDENTS.getSTUDENTS().toString()); 67 List<Student> val = screenPanel.getScreen().screenStudents(STUDENTS.getSTUDENTS()); 68 int i = 0; 69 for (Student student : val) { 70 System.out.println("正在添加第"+ (i++) + "个学生:" + student.toString()); 71 model.addRow(new Object[]{ 72 i + " /" + val.size(), 73 student.getName(), 74 student.getGender(), 75 student.getAge(), 76 student.getStudentID(), 77 student.getSchool(), 78 student.getStudentClass(), 79 student.getOthers() 80 }); 81 //实例化 删除按钮 82 MyButton button0 = new MyButton("删除", student.getStudentID()); 83 MyButton button1 = new MyButton("删除", student.getStudentID()); 84 button0.getButton().addActionListener(new myActionListener(student.getStudentID())); 85 button1.getButton().addActionListener(new myActionListener(student.getStudentID())); 86 //设置删除按钮 87 table.getColumnModel().getColumn(8).setCellEditor(button0);//设置编辑器 88 table.getColumnModel().getColumn(8).setCellRenderer(button1); 89 } 90 //为表添加排序 91 RowSorter<TableModel> rowSorter = new TableRowSorter<TableModel>(model); 92 table.setRowSorter(rowSorter); 93 94 //添加选择监听 95 ListSelectionModel selectionModel = table.getSelectionModel(); 96 selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);// 97 selectionModel.addListSelectionListener(new myListSelectionListener()); 98 99 //添加 编辑监听 100 MyCellEditor cellEditor = new MyCellEditor(new JTextField()); 101 for (int ii = 1; ii < 8; ii++) { 102 // 根据 列名 获取 表格列 103 // TableColumn tableColumn = table.getColumn(tableHeader[ii]); 104 // 设置 表格列 的 单元格编辑器 105 //tableColumn.setCellEditor(cellEditor); 106 } 107 } 108 class MyCellEditor extends DefaultCellEditor { 109 110 public MyCellEditor(JTextField textField) { 111 super(textField); 112 } 113 114 public MyCellEditor(JCheckBox checkBox) { 115 super(checkBox); 116 } 117 118 public MyCellEditor(JComboBox comboBox) { 119 super(comboBox); 120 } 121 122 @Override 123 public boolean stopCellEditing() { 124 Component comp = getComponent(); 125 // 获取当前单元格编辑器输入的 126 Object obj = getCellEditorValue(); 127 if (obj.toString().length() == 0) { 128 JOptionPane.showMessageDialog(null, "不能为空", "ERROR", JOptionPane.ERROR_MESSAGE); 129 return false; 130 } 131 System.out.println(table.getColumnName(table.getSelectedColumn())); 132 return super.stopCellEditing(); 133 } 134 135 } 136 137 138 class myListSelectionListener implements ListSelectionListener { 139 140 @Override 141 public void valueChanged(ListSelectionEvent e) { 142 143 System.out.println("选中!行: "+ table.getSelectedRow()); 144 System.out.println("选中!列: "+table.getSelectedColumn()); 145 int row = table.getSelectedRow(); 146 String columnName = table.getColumnName(table.getSelectedColumn()); 147 System.out.println(columnName); 148 // table.setBackground(Color.red); 149 String id = "aja168520_key"; 150 for (int j = 0;j < 9;j++){ 151 switch (table.getColumnName(j)){ 152 case "编号":System.out.println("zhis is bianhao"); 153 break; 154 case "学号": 155 id = (String)table.getValueAt(row,j); 156 topPanel.setStudent(STUDENTS.getById((String)table.getValueAt(row,j))); 157 topPanel.setINSERT_VALUE("保存"); 158 //topPanel.setCLEAN_VALUE("删除"); 159 System.out.println("当前点击学生是:" + STUDENTS.getById((String)table.getValueAt(row,j)).toString()); 160 topPanel.refresh(); 161 break; 162 default: 163 System.out.println(table.getColumnName(table.getSelectedColumn())); 164 } 165 } 166 // if (columnName.equals("操作")){ 167 // System.out.println("开始操作"); 168 // int flag = JOptionPane.showConfirmDialog(null, "确定删除该条记录?", "警告", JOptionPane.YES_NO_OPTION); 169 // if (flag == JOptionPane.YES_OPTION) { 170 // STUDENTS.deleteById(id); 171 // topPanel.setStudent(new Student()); 172 // JOptionPane.showMessageDialog(null, "删除成功!", "提示", JOptionPane.INFORMATION_MESSAGE); 173 // refresh(); 174 // topPanel.refresh(); 175 // } 176 // } 177 178 // 获取选中的第一行 179 int selectedRow = table.getSelectedRow(); 180 181 // 获取选中的第一列 182 int selectedRow0 = table.getSelectedColumn(); 183 184 // 获取选中的所有行 185 int[] selectedRows = table.getSelectedRows(); 186 187 // 获取选中的所有列 188 int[] selectedColumns = table.getSelectedColumns(); 189 } 190 } 191 192 class myActionListener implements ActionListener { 193 String id ; 194 myActionListener(String id){ 195 this.id = id; 196 } 197 public void setId(String id) { 198 this.id = id; 199 } 200 201 public String getId() { 202 return id; 203 } 204 205 @Override 206 public void actionPerformed(ActionEvent e) { 207 int flag = JOptionPane.showConfirmDialog(null, "确定删除该条记录?", "警告", JOptionPane.YES_NO_OPTION); 208 if (flag == JOptionPane.YES_OPTION) { 209 STUDENTS.deleteById(id); 210 JOptionPane.showMessageDialog(null, "删除成功!", "提示", JOptionPane.INFORMATION_MESSAGE); 211 refresh(); 212 } 213 } 214 } 215 216 public JTable getTable(){ 217 return table; 218 } 219 }Mytable
![java 简单 学生管理系统 - 文章图片](/upload/getfiles/0001/2021/5/1/20210501114124442.jpg)
![java 简单 学生管理系统 - 文章图片](/upload/getfiles/0001/2021/5/1/20210501114124479.jpg)
1 package studentGUI; 2 3 4 import java.util.ArrayList; 5 import java.util.Calendar; 6 import java.util.List; 7 8 public class Screen { 9 private MyDate leftDate; 10 private MyDate rightDate; 11 private String name; 12 private String studentID; 13 private String gender; 14 private String school; 15 private String grade; 16 private String studentClass; 17 18 Screen(){ 19 this.leftDate = new MyDate(MyDate.MIN_YEAR,1,1); 20 this.rightDate = new MyDate( Calendar.getInstance().get(Calendar.YEAR),12,31); 21 this.name = ""; 22 this.studentClass = ""; 23 this.gender = ""; 24 this.studentID = ""; 25 this.grade = ""; 26 this.school = ""; 27 } 28 public List<Student> screenStudents(List<Student> list){ 29 List<Student> retList = new ArrayList<Student>(); 30 System.out.println("正在筛选,条件为:\n" + this.toString()); 31 for (Student student: list){ 32 if (this.name.length() != 0 && !this.name.equals(student.getName())){ continue;} 33 if (this.gender.length() != 0 && !this.gender.equals(student.getGender())){continue;} 34 if (this.studentID.length() != 0 && !this.studentID.equals(student.getStudentID())){continue;} 35 if (this.school.length() != 0 && !this.school.equals(student.getSchool())){continue;} 36 if (this.studentClass.length() != 0 && !this.studentClass.equals(student.getStudentClass())){continue;} 37 if (this.leftDate.myEquals(student.getBirthDate()) >= 0){continue;} 38 if (this.rightDate.myEquals(student.getBirthDate()) < 0){continue;} 39 retList.add(student); 40 } 41 return retList; 42 } 43 44 public String getStudentClass() { 45 return studentClass; 46 } 47 48 public String getSchool() { 49 return school; 50 } 51 52 public String getName() { 53 return name; 54 } 55 56 public String getGrade() { 57 return grade; 58 } 59 60 public String getStudentID() { 61 return studentID; 62 } 63 64 public String getGender() { 65 return gender; 66 } 67 68 public MyDate getLeftDate(){ 69 return leftDate; 70 } 71 public MyDate getRightDate(){ 72 return rightDate; 73 } 74 75 76 77 78 79 public void setStudentID(String studentID) { 80 this.studentID = studentID; 81 } 82 83 public void setStudentClass(String studentClass) { 84 this.studentClass = studentClass; 85 } 86 87 88 public void setLeftDate(MyDate leftDate) { 89 this.leftDate = leftDate; 90 } 91 92 public void setRightDate(MyDate rightDate) { 93 this.rightDate = rightDate; 94 } 95 96 public void setGender(String gender) { 97 this.gender = gender; 98 } 99 100 public void setGrade(String grade) { 101 this.grade = grade; 102 } 103 104 public void setSchool(String school){ 105 this.school = school; 106 } 107 public void setName(String name) { 108 this.name = name; 109 } 110 111 @Override 112 public String toString() { 113 return "ScreenFrame{" + 114 "leftDate=" + leftDate + 115 ", rightDate=" + rightDate + 116 ", name='" + name + '\'' + 117 ", studentID='" + studentID + '\'' + 118 ", gender='" + gender + '\'' + 119 ", school='" + school + '\'' + 120 ", grade='" + grade + '\'' + 121 ", studentClass='" + studentClass + '\'' + 122 '}'; 123 } 124 }Screen
内容总结
以上是互联网集市为您收集整理的java 简单 学生管理系统全部内容,希望文章能够帮你解决java 简单 学生管理系统所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。