1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
| package 学籍管理系统视窗;
import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.sql.Connection; import java.sql.SQLException;
import javax.swing.ButtonGroup; import javax.swing.ComboBoxModel; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField;
import 数据库.Test;
import java.awt.event.ActionListener; import java.awt.event.ActionEvent;
public class 录入面板 extends JPanel { private JTextField textField; private JTextField textField_1; private JTextField textField_2; private JLabel lblNewLabel;
/** * Create the panel. */ public 录入面板() { setLayout(null); lblNewLabel = new JLabel("\u5B66\u53F7"); lblNewLabel.setBounds(72, 55, 30, 18); add(lblNewLabel); JLabel label = new JLabel("\u59D3\u540D"); label.setBounds(72, 24, 30, 18); add(label); textField = new JTextField("请输入学号"); textField.setBounds(116, 52, 130, 24); textField.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { textField.setText(""); } }); add(textField); textField.setColumns(10); textField_1 = new JTextField("请输入姓名"); textField_1.setBounds(116, 21, 130, 24); textField_1.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { textField_1.setText(""); } }); add(textField_1); textField_1.setColumns(10); JLabel label_1 = new JLabel("\u6027\u522B"); label_1.setBounds(72, 89, 30, 18); add(label_1); JRadioButton radioButton = new JRadioButton("\u7537"); radioButton.setBounds(126, 85, 43, 27); add(radioButton); JRadioButton radioButton_1 = new JRadioButton("\u5973"); radioButton_1.setBounds(175, 85, 55, 27); add(radioButton_1); //创建ButtonGroup对象 ButtonGroup btgSex = new ButtonGroup(); btgSex.add(radioButton); btgSex.add(radioButton_1); JLabel label_2 = new JLabel("\u5B66\u9662"); label_2.setBounds(72, 120, 30, 18); add(label_2); ComboBoxModel aModel1=new DefaultComboBoxModel(new String[]{"航海技术","航海技术1","航海技术2"}); ComboBoxModel aModel2=new DefaultComboBoxModel(new String[]{"轮机工程学","轮机工程学1","轮机工程学2"}); ComboBoxModel aModel3=new DefaultComboBoxModel(new String[]{"计算机科学与技术","电子商务(本科)","软件技术","计算机应用技术","电子商务"}); ComboBoxModel aModel4=new DefaultComboBoxModel(new String[]{"英语","日语","外语学"}); JComboBox comboBox_1 = new JComboBox(); comboBox_1.setBounds(110, 151, 168, 24); comboBox_1.setToolTipText(""); add(comboBox_1); JComboBox comboBox = new JComboBox(); comboBox.setBounds(110, 117, 164, 24); comboBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==ItemEvent.SELECTED) { int a=comboBox.getSelectedIndex(); if(a==0) { comboBox_1.setModel(aModel1); comboBox_1.setSelectedIndex(-1); } else if(a==1) { comboBox_1.setModel(aModel2); comboBox_1.setSelectedIndex(-1); } else if(a==2) { comboBox_1.setModel(aModel3); comboBox_1.setSelectedIndex(-1); } else if(a==3) { comboBox_1.setModel(aModel4); comboBox_1.setSelectedIndex(-1); } } } }); comboBox.setModel(new DefaultComboBoxModel(new String[] {"海运学院", "轮机工程学院", "信息与通信工程学院", "外语学院"})); comboBox.setSelectedIndex(-1); add(comboBox); JButton button = new JButton("\u5F55\u5165"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String sex=""; if(radioButton.isSelected()) { sex=radioButton.getText(); }else { sex=radioButton_1.getText(); } if(textField_1.getText().equals("")||textField_1.getText().equals("请输入姓名")) { JOptionPane.showMessageDialog(null,"请输入姓名","错误",JOptionPane.ERROR_MESSAGE); return; } if(textField.getText().equals("")||textField.getText().equals("请输入学号")) { JOptionPane.showMessageDialog(null,"请输入学号","错误",JOptionPane.ERROR_MESSAGE); return; } if(comboBox.getSelectedIndex()==-1) { JOptionPane.showMessageDialog(null,"请选择学院","错误",JOptionPane.ERROR_MESSAGE); return; } if(comboBox_1.getSelectedIndex()==-1) { JOptionPane.showMessageDialog(null,"请选择专业","错误",JOptionPane.ERROR_MESSAGE); return; } if(textField_2.getText().equals("")) { JOptionPane.showMessageDialog(null,"请输入班级","错误",JOptionPane.ERROR_MESSAGE); return; } //保存数据 String sno=textField.getText(); String name=textField_1.getText(); String ssex; if(radioButton.isSelected()) { ssex="男"; }else ssex="女"; String department=comboBox.getSelectedItem().toString(); String major=comboBox_1.getSelectedItem().toString(); String myclass=textField_2.getText(); //调用自己链接数据库操作方法test Connection sin=Test.getconnectin(); boolean flag; flag=Test.chongfu(sin,sno); System.out.println(flag); if(flag==false) { Object str[]={sno,name,ssex,department,major,myclass}; Test.insert(sin, str); JOptionPane.showMessageDialog(null,"录入成功","提示",JOptionPane.INFORMATION_MESSAGE); radioButton.setSelected(false); radioButton_1.setSelected(false); textField.setText("请输入学号"); textField_1.setText("请输入姓名"); textField_2.setText(""); comboBox.setSelectedIndex(-1); comboBox_1.setSelectedIndex(-1); } else JOptionPane.showMessageDialog(null,"失败,学号重复","提示",JOptionPane.INFORMATION_MESSAGE); } }); button.setBounds(203, 232, 63, 27); add(button); JButton button_1 = new JButton("\u91CD\u7F6E"); button_1.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { radioButton.setSelected(false); radioButton_1.setSelected(false); textField.setText("请输入学号"); textField_1.setText("请输入姓名"); textField_2.setText(""); comboBox.setSelectedIndex(-1); comboBox_1.setSelectedIndex(-1); JOptionPane.showMessageDialog(null,"重置成功","提示",JOptionPane.INFORMATION_MESSAGE); } }); button_1.setBounds(106, 232, 63, 27); add(button_1); JLabel label_3 = new JLabel("\u4E13\u4E1A"); label_3.setBounds(72, 154, 30, 18); add(label_3); JLabel label_4 = new JLabel("\u73ED\u7EA7"); label_4.setBounds(72, 191, 30, 18); add(label_4); textField_2 = new JTextField(); textField_2.setBounds(110, 188, 164, 24); add(textField_2); textField_2.setColumns(10); }
}
|