📄 person.java
字号:
package system;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class Person extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel msg = new JLabel("个人信息修改");
private JLabel infoLabel = new JLabel("请输入修改信息:");
private JLabel idLabel = new JLabel("编号");
private JLabel nameLabel = new JLabel("姓名");
private JLabel sexLabel = new JLabel("性别");
private JLabel dateLabel = new JLabel("出生年月");
private JLabel addressLabel = new JLabel("住址");
private JLabel phoneLabel = new JLabel("电话");
private JLabel homeLabel = new JLabel("籍贯");
private JLabel wenhuaLabel = new JLabel("文化水平");
private JLabel zhiwuLabel = new JLabel("职务");
private JLabel partLabel = new JLabel("部门");
private JLabel salaryLabel = new JLabel("工资");
private JLabel passLabel = new JLabel("密码");
private JLabel repassLabel = new JLabel("密码确认");
private String name;
private String password;
private JPasswordField passText = new JPasswordField();
private JPasswordField repassText = new JPasswordField();
private JTextField idText = new JTextField(20);
private JTextField nameText = new JTextField(20);
JComboBox sexBox = new JComboBox();
private JTextField dateText = new JTextField(20);
private JTextField addressText = new JTextField(20);
private JTextField phoneText = new JTextField(20);
private JTextField homeText = new JTextField(20);
private JComboBox wenhuaBox = new JComboBox();
private JComboBox zhiwuBox = new JComboBox();
private JComboBox partBox = new JComboBox();
private JTextField salaryText = new JTextField(20);
private JButton addButton = new JButton("修改");
private JButton cancelButton = new JButton("退出");
public Person() {
super("个人信息修改");
initialize();
}
public void initialize() {
this.getContentPane().setLayout(null);
setSize(600, 400);
setLocation(250, 200);
msg.setBounds(250, 10, 100, 20);
infoLabel.setBounds(10, 40, 140, 20);
nameLabel.setBounds(80, 70, 40, 20);
nameText.setBounds(120, 70, 100, 20);
idLabel.setBounds(80, 100, 40, 20);
idText.setBounds(120, 100, 100, 20);
sexLabel.setBounds(80, 130, 40, 20);
sexBox.setBounds(120, 130, 100, 20);
sexBox.addItem("男");
sexBox.addItem("女");
passLabel.setBounds(80, 160, 40, 20);
passText.setBounds(120, 160, 100, 20);
repassLabel.setBounds(55, 190, 60, 20);
repassText.setBounds(120, 190, 100, 20);
phoneLabel.setBounds(80, 220, 40, 20);
phoneText.setBounds(120, 220, 100, 20);
addressLabel.setBounds(80, 250, 40, 20);
addressText.setBounds(120, 250, 100, 20);
this.getContentPane().add(msg);
this.getContentPane().add(infoLabel);
this.getContentPane().add(nameLabel);
this.getContentPane().add(nameText);
this.getContentPane().add(idLabel);
this.getContentPane().add(idText);
this.getContentPane().add(sexLabel);
this.getContentPane().add(sexBox);
this.getContentPane().add(addressLabel);
this.getContentPane().add(addressText);
this.getContentPane().add(phoneLabel);
this.getContentPane().add(phoneText);
this.getContentPane().add(passLabel);
this.getContentPane().add(passText);
this.getContentPane().add(repassLabel);
this.getContentPane().add(repassText);
homeLabel.setBounds(330, 70, 40, 20);
homeText.setBounds(380, 70, 100, 20);
wenhuaLabel.setBounds(315, 100, 60, 20);
wenhuaBox.setBounds(380, 100, 100, 20);
wenhuaBox.addItem("大学");
wenhuaBox.addItem("研究生");
wenhuaBox.addItem("博士");
wenhuaBox.addItem("教授");
zhiwuLabel.setBounds(330, 130, 40, 20);
zhiwuBox.setBounds(380, 130, 100, 20);
zhiwuBox.addItem("员工");
zhiwuBox.addItem("经理");
partLabel.setBounds(330, 160, 40, 20);
partBox.setBounds(380, 160, 100, 20);
partBox.addItem("人事管理部");
partBox.addItem("后勤部");
partBox.addItem("财政部");
salaryLabel.setBounds(330, 190, 40, 20);
salaryText.setBounds(380, 190, 100, 20);
dateLabel.setBounds(305, 220, 60, 20);
dateText.setBounds(380, 220, 100, 20);
this.getContentPane().add(homeLabel);
this.getContentPane().add(homeText);
this.getContentPane().add(wenhuaLabel);
this.getContentPane().add(wenhuaBox);
this.getContentPane().add(zhiwuLabel);
this.getContentPane().add(zhiwuBox);
this.getContentPane().add(partLabel);
this.getContentPane().add(partBox);
this.getContentPane().add(salaryLabel);
this.getContentPane().add(salaryText);
this.getContentPane().add(dateLabel);
this.getContentPane().add(dateText);
addButton.setBounds(210, 290, 60, 20);
cancelButton.setBounds(280, 290, 60, 20);
this.getContentPane().add(addButton);
this.getContentPane().add(cancelButton);
cancelButton.addActionListener(this);
addButton.addActionListener(this);
ActionListener s = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
update();
}
};
this.getRootPane().registerKeyboardAction(s,
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
JComponent.WHEN_IN_FOCUSED_WINDOW);
}
public void actionPerformed(ActionEvent evt) {
Object ob = evt.getSource();
if (ob.equals(cancelButton)) {
dispose();
} else if (ob.equals(addButton)) {
update();
}
}
public void update() {
String sql = "";
String pass = new String(passText.getPassword());
String repass = new String(repassText.getPassword());
if (nameText.getText().equals("")) {
JOptionPane.showMessageDialog(null, "名字不能为空");
nameText.requestFocus();
return;
}
if (idText.getText().equals("")) {
JOptionPane.showMessageDialog(null, "编号不能为空");
idText.requestFocus();
return;
}
if (pass.equals("")) {
JOptionPane.showMessageDialog(null, "密码不能为空");
passText.requestFocus();
return;
}
if (!pass.equals(repass)) {
JOptionPane.showMessageDialog(null, "密码不相符");
passText.requestFocus();
passText.setText("");
repassText.setText("");
return;
}
if (addressText.getText().equals("")) {
JOptionPane.showMessageDialog(null, "地址不能为空");
addressText.requestFocus();
return;
}
if (dateText.getText().equals("")) {
JOptionPane.showMessageDialog(null, "出生日期不能为空");
dateText.requestFocus();
return;
}
sql = "update user set id='" + idText.getText() + "',name='"
+ nameText.getText() + "',sex='" + sexBox.getSelectedItem()
+ "',date='" + dateText.getText() + "',address='"
+ addressText.getText() + "',phone='" + phoneText.getText()
+ "',home='" + homeText.getText() + "',wenhua='"
+ wenhuaBox.getSelectedItem() + "',zhiwu='"
+ zhiwuBox.getSelectedItem() + "',part='"
+ partBox.getSelectedItem() + "',salary='"
+ salaryText.getText() + "' where name='" + this.getName()
+ "' and password='" + this.getPassword() + "';";
sql = changeChar.gbToiso(sql);
System.out.println(sql);
DataBase db = new DataBase();
try {
Statement stmt = db.conn.createStatement();
int rs = stmt.executeUpdate(sql);
if (rs == 0) {
JOptionPane.showMessageDialog(null, "修改失败");
}else{
JOptionPane.showMessageDialog(null, "修改成功");
dispose();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setName(String newname) {
name = newname;
}
public String getName() {
return name;
}
public void setPassword(String newpassword) {
password = newpassword;
}
public String getPassword() {
return password;
}
public static void main(String args[]) {
addScreen screen = new addScreen();
screen.repaint();
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -