You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// TAdminPanel.java
package com.WR.StudentMS.view;
import javax.swing.*;
import com.WR.StudentMS.dao.mysql.TAdmindaoimplzh;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TAdminPanel extends JPanel {
private JTextField txtUserName;
private JPasswordField txtOldPassword;
private JPasswordField txtNewPassword;
private JButton btnModify;
private JButton btnReset;
private TAdmindaoimplzh adminDao; // 数据访问对象
public TAdminPanel() {
adminDao = new TAdmindaoimplzh(); // 初始化数据访问对象
// 设置面板布局
setLayout(new GridLayout(4, 2, 5, 5));
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// 初始化组件
txtUserName = new JTextField(20);
txtOldPassword = new JPasswordField(20);
txtNewPassword = new JPasswordField(20);
btnModify = new JButton("修改");
btnReset = new JButton("重置");
// 添加组件到面板
add(new JLabel("用户名:"));
add(txtUserName);
add(new JLabel("原密码:"));
add(txtOldPassword);
add(new JLabel("新密码:"));
add(txtNewPassword);
add(btnModify);
add(btnReset);
// 为按钮添加事件监听器
btnModify.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
modifyPassword();
}
});
btnReset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
resetForm();
}
});
}
private void modifyPassword() {
String userName = txtUserName.getText();
String oldPassword = new String(txtOldPassword.getPassword());
String newPassword = new String(txtNewPassword.getPassword());
if (adminDao.verifyCredentials(userName, oldPassword)) {
// 如果凭证验证成功,调用 changePassword 方法更新密码
int result = adminDao.changePassword(userName, newPassword);
if (result > 0) {
JOptionPane.showMessageDialog(null, "密码修改成功!");
resetForm(); // 清空表单
} else {
JOptionPane.showMessageDialog(null, "密码更新失败,请重试。");
}
} else {
// 如果凭证验证失败,显示错误信息并清空原密码输入框
showError("用户名或原密码错误!");
txtOldPassword.setText(""); // 清空原密码
}
}
private void resetForm() {
txtUserName.setText("");
txtOldPassword.setText("");
txtNewPassword.setText("");
}
private void showError(String message) {
// 显示错误信息的方法可能需要更新UI来显示红色提醒
JOptionPane.showMessageDialog(null, message);
}
}