package com.student; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.HashMap; import java.util.Map; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import org.bson.Document; import com.mongodb.client.MongoCollection; public class Add { public static void AddStudent(MongoCollection collection) { JFrame jf = new JFrame(); Container c=jf.getContentPane(); jf.setTitle("添加学生信息"); JLabel no = new JLabel("学号:"); JLabel name = new JLabel("姓名:"); JLabel gender = new JLabel("性别:"); JLabel age = new JLabel("年龄:"); JLabel academy = new JLabel("学院:"); JLabel grade = new JLabel("成绩:"); JTextField jtno = new JTextField(20); JTextField jtname = new JTextField(20); JTextField jtgender = new JTextField(20); JTextField jtage = new JTextField(20); JTextField jtacademy = new JTextField(20); JTextField jtgrade = new JTextField(20); JButton jbadd = new JButton("添加"); JButton jbback = new JButton("返回"); jf.setLayout(new GridLayout(8,2)); c.add(no);c.add(jtno); c.add(name);c.add(jtname); c.add(gender);c.add(jtgender); c.add(age);c.add(jtage); c.add(academy);c.add(jtacademy); c.add(grade);c.add(jtgrade); c.add(jbadd);c.add(jbback); jbadd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 try { Map<String, Object> map = new HashMap<>(); map.put("no", Integer.parseInt(jtno.getText())); map.put("name",jtname.getText()); map.put("gender",jtgender.getText()); map.put("age", Integer.parseInt(jtage.getText()));//将从JTextField获取的String类型转化成int型 map.put("academy",jtacademy.getText()); map.put("grade", Integer.parseInt(jtgrade.getText())); Document doc = new Document(map); collection.insertOne(doc); System.out.println("插入数据成功"); JOptionPane.showMessageDialog(c, "数据添加成功!", "信息提示框", JOptionPane.WARNING_MESSAGE); } catch(Exception ex) { ex.getMessage(); JOptionPane.showMessageDialog(c, "学号冲突,数据添加失败!", "信息提示框", JOptionPane.WARNING_MESSAGE); } } }); jbback.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 jf.dispose(); new Stu_List().SearchAll(collection); } }); jf.setSize(650,500); jf.setLocation(100, 100); jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); jf.setVisible(true); } }
最近下载更多
husiyu LV3
2023年9月7日
1362440352 LV1
2022年11月27日
asdfasfsf LV1
2022年6月15日
testuser1234567 LV24
2022年5月31日
syj991023 LV2
2022年5月13日
wyx065747 LV67
2022年5月7日
wanglinddad LV53
2022年4月2日
zydandzjf LV3
2022年1月6日
希雨逍遥 LV6
2021年6月22日
xerxes_xxi LV1
2021年6月17日
最近浏览更多
xinnnnn LV1
2月26日
颜伟峰 LV1
2023年12月20日
sunshine9920 LV12
2023年10月19日
husiyu LV3
2023年9月7日
卢本伟不开挂 LV4
2023年9月3日
Google刘总的
2023年7月5日
暂无贡献等级
hongdongdong LV12
2023年6月30日
KAIzx11 LV7
2023年6月25日
微信网友_6184005683466240 LV11
2023年6月11日
tianyuboy111 LV3
2023年5月20日