📄 adduser.java
字号:
package library.iframe;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import library.DBConnectOperation.Dbo;
import library.util.CreateIcon;
public class AddUser extends JInternalFrame{
private JTextField name;
private JComboBox sex;
private JTextField identityCardNumber;
private JTextField tel;
private JFormattedTextField workDate;
private JComboBox admin;
private JPasswordField password;
private JPasswordField passwordConfirm;
private JButton saveButton;
private JButton cancelButton;
private final String tempSex[]={"男","女"};
private final String tempAdmin[]={"系统管理员","图书管理员"};
public AddUser(){
super();
setBounds(120, 120, 490, 320);
setTitle("添加新管理员");
setResizable(false);
setIconifiable(true);
setClosable(true);
//添加顶部图片
JLabel labelTop=new JLabel();
labelTop.setIcon(CreateIcon.add("operatorAdd.jpg"));
getContentPane().add(labelTop,BorderLayout.NORTH);
((BorderLayout) getContentPane().getLayout()).setVgap(5);
//设置中央面板,用于添加管理员信息
JPanel panelCenter=new JPanel();
GridLayout gridLayout=new GridLayout(0,4);
gridLayout.setVgap(5);
gridLayout.setHgap(5);
panelCenter.setLayout(gridLayout);
getContentPane().add(panelCenter);
JLabel labelName=new JLabel();
labelName.setText("昵称:");
labelName.setHorizontalAlignment(SwingConstants.RIGHT);
panelCenter.add(labelName);
name=new JTextField();
name.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent e){
if(name.getText().length()>20){
JOptionPane.showMessageDialog(null,"昵称不能超过20个字符!");
name.setText("");
return;
}
else if(Dbo.selectOperatorName(name.getText())!=null){
JOptionPane.showMessageDialog(null,"该昵称与现有昵称重复,请更换!");
name.setText("");
return;
}
}
});
panelCenter.add(name);
JLabel labelSex=new JLabel();
labelSex.setText("性别:");
labelSex.setHorizontalAlignment(SwingConstants.RIGHT);
panelCenter.add(labelSex);
sex=new JComboBox();
sex.addItem(tempSex[0]);
sex.addItem(tempSex[1]);
panelCenter.add(sex);
JLabel labelIdentityCard=new JLabel();
labelIdentityCard.setText("身份证号:");
labelIdentityCard.setHorizontalAlignment(SwingConstants.RIGHT);
panelCenter.add(labelIdentityCard);
identityCardNumber=new JTextField();
identityCardNumber.addFocusListener(new FocusAdapter(){
public void focusLost(FocusEvent fe){
if(identityCardNumber.getText().length()!=18){
JOptionPane.showMessageDialog(null,"身份证号应为18个字符!");
identityCardNumber.setText("");
return;
}
}
});
panelCenter.add(identityCardNumber);
JLabel labelTel=new JLabel();
labelTel.setText("电话号码:");
labelTel.setHorizontalAlignment(SwingConstants.RIGHT);
panelCenter.add(labelTel);
tel=new JTextField();
tel.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent te){
try{
Long.parseLong(tel.getText());
}
catch(NumberFormatException tne){
JOptionPane.showMessageDialog(null, "电话号码应为数字!");
tel.setText("");
return;
}
if(tel.getText().length()>15){
JOptionPane.showMessageDialog(null, "电话号码长度不应超过15位");
tel.setText("");
return;
}
}
});
panelCenter.add(tel);
JLabel labelWorkDate=new JLabel();
labelWorkDate.setText("参加工作时间:");
labelWorkDate.setHorizontalAlignment(SwingConstants.RIGHT);
panelCenter.add(labelWorkDate);
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
workDate=new JFormattedTextField(format);
workDate.setValue(new Date());
panelCenter.add(workDate);
JLabel labelAdmin=new JLabel();
labelAdmin.setText("管理员类型:");
labelAdmin.setHorizontalAlignment(SwingConstants.RIGHT);
panelCenter.add(labelAdmin);
admin=new JComboBox();
admin.addItem(tempAdmin[0]);
admin.addItem(tempAdmin[1]);
panelCenter.add(admin);
JLabel labelPassword=new JLabel();
labelPassword.setText("密码:");
labelPassword.setHorizontalAlignment(SwingConstants.RIGHT);
panelCenter.add(labelPassword);
password=new JPasswordField();
password.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent ie){
if(password.getText().length()>10){
JOptionPane.showMessageDialog(null, "密码长度不应超过10位");
password.setText("");
return;
}
}
});
panelCenter.add(password);
JLabel labelPasswordConfirm=new JLabel();
labelPasswordConfirm.setText("再次输入密码:");
labelPasswordConfirm.setHorizontalAlignment(SwingConstants.RIGHT);
panelCenter.add(labelPasswordConfirm);
passwordConfirm=new JPasswordField();
passwordConfirm.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent ie){
if(passwordConfirm.getText().length()>10){
JOptionPane.showMessageDialog(null, "密码长度不应超过10位");
passwordConfirm.setText("");
return;
}
}
});
panelCenter.add(passwordConfirm);
//添加底部按钮面板
JPanel panelBottom=new JPanel();
FlowLayout flowLayout=new FlowLayout();
flowLayout.setHgap(30);
panelBottom.setLayout(flowLayout);
getContentPane().add(panelBottom,BorderLayout.SOUTH);
saveButton=new JButton();
saveButton.setText("保存");
saveButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(name.getText().length()==0){
JOptionPane.showMessageDialog(null, "管理员昵称不能为空!");
return;
}
if(identityCardNumber.getText().length()==0){
JOptionPane.showMessageDialog(null, "身份证号不能为空!");
return;
}
if(password.getText().length()==0){
JOptionPane.showMessageDialog(null, "密码不能为空!");
return;
}
if(!passwordConfirm.getText().equals(password.getText())){
JOptionPane.showMessageDialog(null,"两次密码输入不一致!");
password.setText("");
passwordConfirm.setText("");
return;
}
int i;
if(admin.getSelectedItem().equals("系统管理员"))
i=1;
else i=0;
String sql="insert into tb_operator(name,sex,identityCard,tel,workdate,admin,password) " +
"values('"+name.getText()+"','"+(String)sex.getSelectedItem()
+"','"+identityCardNumber.getText()+"','"+tel.getText()+"','"
+workDate.getText()+"',"+i+",'"+password.getText()+"')";
i=Dbo.executeUpdate(sql);
if(i!=-1){
JOptionPane.showMessageDialog(null, "添加成功");
name.setText("");
identityCardNumber.setText("");
tel.setText("");
workDate.setText("");
password.setText("");
passwordConfirm.setText("");
Dbo.close();
}
else{
JOptionPane.showMessageDialog(null, "添加失败,请检查数据源");
return;
}
}
});
panelBottom.add(saveButton);
cancelButton=new JButton();
cancelButton.setText("返回");
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
dispose();
}
});
panelBottom.add(cancelButton);
setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -