📄 editadminpass.java
字号:
package employee;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.sql.*;
public class EditAdminPass extends JFrame implements KeyListener,ActionListener{
Container contentPane;
JPanel centerPanel=new JPanel();
JPanel upPanel=new JPanel();
JPanel downPanel=new JPanel();
String userid;
String key1;
String key2;
ResultSet rs;
//框架大小
Dimension faceSize=new Dimension(400,300);
JLabel jLabel1=new JLabel();
JLabel jLabel2=new JLabel();
JLabel jLabel3=new JLabel();
JLabel jLabel4=new JLabel();
JTextField user=new JTextField(10);
JPasswordField password1=new JPasswordField(10);
JPasswordField password2=new JPasswordField(10);
JButton jButton1=new JButton();
JButton jButton2=new JButton();
JButton jButton3=new JButton();
GridBagLayout gridBag=new GridBagLayout();
GridBagConstraints gridBagCon;
Database DB = new Database();
public EditAdminPass(){
this.setSize(faceSize);
this.setTitle("修改密码");
this.setResizable(false);
try{
Init();
upInit();
downInit();
}
catch(Exception e){e.printStackTrace();}
//设置运行位置,使对话框居中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation( (int) (screenSize.width - 400) / 2 ,
(int) (screenSize.height - 300) / 2 + 45);
}
public void Init() throws Exception{
contentPane=this.getContentPane();
contentPane.setLayout(new BorderLayout());
//中间面板的布局
centerPanel.setLayout(gridBag);
jLabel1.setText("用户名:");
jLabel1.setFont(new Font("Dialog",0,12));
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 0;
gridBagCon.insets = new Insets(10,10,10,1);
gridBag.setConstraints(jLabel1,gridBagCon);
centerPanel.add(jLabel1);
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 1;
gridBagCon.gridy = 0;
gridBagCon.insets = new Insets(10,1,10,10);
gridBag.setConstraints(user,gridBagCon);
centerPanel.add(user);
jLabel2.setText("新密码:");
jLabel2.setFont(new Font("Dialog",0,12));
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(10,15,10,1);
gridBag.setConstraints(jLabel2,gridBagCon);
centerPanel.add(jLabel2);
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 1;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(10,1,10,10);
gridBag.setConstraints(password1,gridBagCon);
centerPanel.add(password1);
jLabel3.setText("确认密码:");
jLabel3.setFont(new Font("Dialog",0,12));
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 2;
gridBagCon.insets = new Insets(10,15,10,1);
gridBag.setConstraints(jLabel3,gridBagCon);
centerPanel.add(jLabel3);
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 1;
gridBagCon.gridy = 2;
gridBagCon.insets = new Insets(10,1,10,10);
gridBag.setConstraints(password2,gridBagCon);
centerPanel.add(password2);
contentPane.add(centerPanel,BorderLayout.CENTER);
user.setEditable(true);
password1.setEditable(true);
password2.setEditable(true);
}
//上部面板的布局
public void upInit(){
jLabel4.setText("管理员修改密码");
jLabel4.setFont(new Font("Dialog",0,12));
upPanel.add(jLabel4);
contentPane.add(upPanel,BorderLayout.NORTH);
}
//下部面板的布局
public void downInit(){
jButton1.setText("确定");
jButton1.setFont(new Font("Dialog",0,12));
downPanel.add(jButton1);
jButton2.setText("清空");
jButton2.setFont(new Font("Dialog",0,12));
downPanel.add(jButton2);
jButton3.setText("退出");
jButton3.setFont(new Font("Dialog",0,12));
downPanel.add(jButton3);
contentPane.add(downPanel,BorderLayout.SOUTH);
user.addKeyListener(this);
password1.addKeyListener(this);
password2.addKeyListener(this);
jButton1.addKeyListener(this);
jButton2.addKeyListener(this);
jButton3.addKeyListener(this);
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
}
//事件处理
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){//按下回车键
Object obj=e.getSource();
if(obj==user){
password1.requestFocus();//使新密码框获得焦点
}
else if(obj==password1){
password2.requestFocus();//使确认密码框获得焦点
}
else if(obj==password2){
jButton1.requestFocus();//使"确认"按钮获得焦点
}
else if(obj==jButton1){
jButton1.doClick();//触发"确认"按钮
}
else if(obj==jButton2){
jButton2.doClick();
}
else if(obj==jButton3){
jButton2.doClick();
}
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void actionPerformed(ActionEvent e){
boolean packFrame = false;
Object obj=e.getSource();
if(obj==jButton1){//确定
userid=user.getText();
key1=new String(password1.getPassword());
key2=new String(password2.getPassword());
//截取字符串的头尾空格
userid.trim();
key1.trim();
key2.trim();
if(userid.equals("sa")&&key1.equals(key2)&&(!key1.equals(""))&&(!key2.equals(""))){
try{
DB.OpenConn();
String sql="update password set password='"+key1+"'";
rs = DB.executeQuery(sql);
}
catch(Exception e1){
System.out.println(e1);
JOptionPane.showMessageDialog(null, "更新失败", "错误", JOptionPane.ERROR_MESSAGE);
}
finally {
DB.closeStmt();
DB.closeConn();
}
JOptionPane.showMessageDialog(null, "密码修改成功!");
this.dispose();
}
else if(!userid.equals("sa")){
JOptionPane.showMessageDialog(null,"用户名错误,请输入正确的用户名!");
user.setText("");//清空用户名文本框
user.requestFocus();//将光标停在用户名文本框中
}
else if(!key1.equals(key2)){
JOptionPane.showMessageDialog(null,"两次输入的密码不同,请重新输入密码!");
password1.setText("");//清空密码文本框
password2.setText("");
password1.requestFocus();//将光标停在用户名文本框中
}
else if(key1.equals("")&&key2.equals("")){
JOptionPane.showMessageDialog(null,"密码不能为空值!");
password1.requestFocus();//将光标停在用户名文本框中
}
}
else if(obj==jButton2){//清空
setNull();
}
else if(obj==jButton3){//退出
this.dispose();
}
}
void setNull(){
user.setText(null);
password1.setText(null);
password2.setText(null);
user.requestFocus();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -