📄 personalsettingframe.java
字号:
package myprojects.Account;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
//------------------
import myprojects.Account.*;
/**
* @(#)NewAccountFrame.java
*
* 修改个人信息
*
* @author
* @version 1.00 05/12/25
*/
public class PersonalSettingFrame extends JFrame {
JPanel contentPane;
JTabbedPane personSettingTabbedPane = new JTabbedPane();
JPanel pwdModifyPanel = new JPanel();
JPanel baseInfoModifyPanel = new JPanel();
ButtonGroup groupRadioB = new ButtonGroup();
JLabel accountIdLabel = new JLabel();
JLabel accountIdTextLabel = new JLabel();
JLabel accountKindLabel = new JLabel();
JLabel accountKindTextLabel = new JLabel();
JLabel nameLabel = new JLabel();
JTextField nameTextField = new JTextField();
JLabel sexLabel = new JLabel();
JRadioButton maleRadioButton = new JRadioButton();
JRadioButton femaleRadioButton = new JRadioButton();
JLabel wageLabel = new JLabel();
JLabel wageTextLabel = new JLabel();
JLabel addressLabel = new JLabel();
JTextField addressTextField = new JTextField();
JButton modifyButton = new JButton();
JButton modifyNoExitButton = new JButton();
JButton exitButton = new JButton();
JLabel oldPwdLabel = new JLabel();
JPasswordField oldPasswordField = new JPasswordField();
JLabel newPwdLabel = new JLabel();
JPasswordField newPasswordField = new JPasswordField();
JLabel affrimNewPwdLabel = new JLabel();
JPasswordField affrimNewPasswordField = new JPasswordField();
JLabel hintLabel = new JLabel();
JButton exitPwdButton = new JButton();
JButton modifyPwdNoExitButton = new JButton();
JButton modifyPwdButton = new JButton();
////////////////////////////////////////////
PersonalSetting personalSetting = new PersonalSetting();
//保存登录的用户的Old Info
Account oldAccount = new Account();
//保存新的个人信息
Account newPersonalInfo = new Account();
//标志是否成功修改过密码,如果是,标志为1
int changePwdMark=0;
//Construct the frame
public PersonalSettingFrame(Account acc) {
//从数据库得到新的个人信息,以便可以多次修改信息能及时反映出来
oldAccount = personalSetting.getAccountInfo(acc.getAccountId().trim());
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setForeground(Color.black);
contentPane.setDebugGraphicsOptions(0);
this.setSize(new Dimension(400-5, 310));
this.setLocation(300,250);
this.setResizable(false); //使之不能改变大小
this.setTitle("个人设置");
baseInfoModifyPanel.setLayout(null);
accountIdLabel.setDebugGraphicsOptions(0);
accountIdLabel.setText("帐号");
accountIdLabel.setBounds(new Rectangle(50, 23, 34, 16));
accountIdTextLabel.setBackground(Color.white);
accountIdTextLabel.setText("");
accountIdTextLabel.setBounds(new Rectangle(87, 20, 57, 22));
accountKindLabel.setText("类型");
accountKindLabel.setBounds(new Rectangle(182, 25, 34, 16));
accountKindTextLabel.setText("");
accountKindTextLabel.setBounds(new Rectangle(225, 20, 60, 22));
nameLabel.setText("姓名");
nameLabel.setBounds(new Rectangle(50, 64, 34, 16));
nameTextField.setText("");
nameTextField.setBounds(new Rectangle(87, 61, 57, 22));
sexLabel.setRequestFocusEnabled(true);
sexLabel.setText("性别");
sexLabel.setBounds(new Rectangle(182, 64, 34, 16));
maleRadioButton.setSelectedIcon(null);
maleRadioButton.setText("男");
maleRadioButton.setSelected(true);
maleRadioButton.setBounds(new Rectangle(224, 60, 39, 25));
femaleRadioButton.setBounds(new Rectangle(262, 60, 47, 25));
femaleRadioButton.setText("女");
femaleRadioButton.setSelectedIcon(null);
groupRadioB.add(maleRadioButton);
groupRadioB.add(femaleRadioButton);
wageLabel.setHorizontalAlignment(SwingConstants.LEADING);
wageLabel.setIconTextGap(4);
wageLabel.setText("工资");
wageLabel.setBounds(new Rectangle(50, 106, 34, 16));
wageTextLabel.setText("");
wageTextLabel.setBounds(new Rectangle(87, 102, 57, 22));
addressLabel.setText("地址");
addressLabel.setBounds(new Rectangle(50, 147, 34, 16));
addressTextField.setText("");
addressTextField.setBounds(new Rectangle(84, 143, 216, 22));
modifyButton.setBounds(new Rectangle(58, 193, 62, 25));
modifyButton.setText("修改");
modifyNoExitButton.setText("应用");
modifyNoExitButton.setBounds(new Rectangle(148, 193, 62, 25));
exitButton.setText("返回");
exitButton.setBounds(new Rectangle(234, 193, 62, 25));
oldPwdLabel.setText("旧密码");
oldPwdLabel.setBounds(new Rectangle(100, 69, 34+20, 16));
pwdModifyPanel.setLayout(null);
oldPasswordField.setText("");
oldPasswordField.setBounds(new Rectangle(148+30, 66, 94, 22));
newPwdLabel.setBounds(new Rectangle(100, 113, 34+20, 16));
newPwdLabel.setText("新密码");
newPasswordField.setText("");
newPasswordField.setBounds(new Rectangle(150+30, 110, 94, 22));
affrimNewPwdLabel.setText("确认密码");
affrimNewPwdLabel.setBounds(new Rectangle(89, 156, 45+20, 16));
affrimNewPasswordField.setBounds(new Rectangle(151+30, 153, 94, 22));
affrimNewPasswordField.setText("");
hintLabel.setText("请妥善保存你的密码");
hintLabel.setBounds(new Rectangle(79, 17, 237, 38));
exitPwdButton.setBounds(new Rectangle(246, 197, 62, 25));
exitPwdButton.setText("返回");
modifyPwdNoExitButton.setBounds(new Rectangle(160, 197, 62, 25));
modifyPwdNoExitButton.setText("应用");
modifyPwdButton.setText("修改");
modifyPwdButton.setBounds(new Rectangle(70, 197, 62, 25));
pwdModifyPanel.add(oldPasswordField, null);
pwdModifyPanel.add(oldPwdLabel, null);
pwdModifyPanel.add(newPwdLabel, null);
pwdModifyPanel.add(newPasswordField, null);
pwdModifyPanel.add(hintLabel, null);
pwdModifyPanel.add(affrimNewPasswordField, null);
pwdModifyPanel.add(affrimNewPwdLabel, null);
pwdModifyPanel.add(modifyPwdNoExitButton, null);
pwdModifyPanel.add(modifyPwdButton, null);
pwdModifyPanel.add(exitPwdButton, null);
contentPane.add(personSettingTabbedPane, null);
personSettingTabbedPane.add(baseInfoModifyPanel, "基本信息");
baseInfoModifyPanel.add(nameTextField, null);
baseInfoModifyPanel.add(accountIdLabel, null);
baseInfoModifyPanel.add(addressTextField, null);
baseInfoModifyPanel.add(wageLabel, null);
baseInfoModifyPanel.add(nameLabel, null);
baseInfoModifyPanel.add(addressLabel, null);
baseInfoModifyPanel.add(wageTextLabel, null);
baseInfoModifyPanel.add(accountIdTextLabel, null);
baseInfoModifyPanel.add(accountKindLabel, null);
baseInfoModifyPanel.add(maleRadioButton, null);
baseInfoModifyPanel.add(sexLabel, null);
baseInfoModifyPanel.add(femaleRadioButton, null);
baseInfoModifyPanel.add(accountKindTextLabel, null);
baseInfoModifyPanel.add(modifyButton, null);
baseInfoModifyPanel.add(modifyNoExitButton, null);
baseInfoModifyPanel.add(exitButton, null);
personSettingTabbedPane.add(pwdModifyPanel, "修改密码");
LineBorder lineBorderId = (LineBorder)BorderFactory.createLineBorder(Color.BLUE);
accountIdTextLabel.setBorder(lineBorderId);
LineBorder lineBorderKind = (LineBorder)BorderFactory.createLineBorder(Color.BLUE);
accountKindTextLabel.setBorder(lineBorderKind);
LineBorder lineBorderWage = (LineBorder)BorderFactory.createLineBorder(Color.BLUE);
wageTextLabel.setBorder(lineBorderWage);
////////////////////////////////////////////////////////////
//显示该用户的信息,以便修改
accountIdTextLabel.setText(oldAccount.getAccountId().trim());
if(oldAccount.getAccountClass()==0)
accountKindTextLabel.setText("接待员");
else
accountKindTextLabel.setText("经理");
nameTextField.setText(oldAccount.getAccountName().trim());
if(oldAccount.getAccountSex()==1)
maleRadioButton.setSelected(true);
else
femaleRadioButton.setSelected(true);
wageTextLabel.setText(oldAccount.getAccountWage()+"");
addressTextField.setText(oldAccount.getAccountAddress().trim());
///////////////////////////////
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
closeFrame();
}
});
modifyNoExitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
modifyInfoNoExit_applyButtonClick();
}
});
//保存信息,并且退出
modifyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
modifyInfoNoExit_applyButtonClick();
closeFrame();
}
});
///////////////////////////////
exitPwdButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
closeFrame();
}
});
modifyPwdNoExitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
modifyPwdNoExit_applyButtonClick();
}
});
//保存信息,并且退出
modifyPwdButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
modifyPwdNoExit_applyButtonClick();
}
});
////////////////////////////////////////////
this.setVisible(true);
////////////////////////////////////////////
}
protected void modifyPwdNoExit_applyButtonClick(){
//将密码框中字符数组转化为字符串
String oldpasswd=new String(oldPasswordField.getPassword());
// System.out.println ("oldpasswd= "+oldpasswd);
String newpasswd=new String(newPasswordField.getPassword());
// System.out.println ("newpasswd= "+newpasswd);
String newpasswd2=new String(affrimNewPasswordField.getPassword());
// System.out.println ("affrimNewPassword = "+newpasswd2);
//比较输入的旧密码和原来
String perpassword = oldAccount.getPassword().trim();
if(!oldpasswd.trim().equals(perpassword))
{
// System.out.println ("perpassword"+perpassword);
System.out.println ("旧密码输入错误");
JOptionPane.showMessageDialog(this,"旧密码输入错误,请重新输入","ok",
JOptionPane.ERROR_MESSAGE);
// modifypwdsucceful = 0;
return ;
}
if(newpasswd.equals(""))
{
JOptionPane.showMessageDialog(this,"新密码不能为空,请重新输入","ok",
JOptionPane.ERROR_MESSAGE);
//modifypwdsucceful = 0;
return;
}
//看两次输入新密码是否一致
if(newpasswd.equals(newpasswd2))
perpassword = newpasswd;
else{
JOptionPane.showMessageDialog(this,"两次新密码不一致,请重新输入","ok",
JOptionPane.ERROR_MESSAGE);
//modifypwdsucceful = 0;
return;
}
changePwdMark=1;
//出于安全性,修改完密码后确认或者应用都关闭界面,释放对象
modifyInfoNoExit_applyButtonClick();
closeFrame();
}
//应用按钮作用:修改信息,但是不退出该界面
//如果是点击修改按钮,则保存信息后就退出该界面
protected void modifyInfoNoExit_applyButtonClick(){
//personalSetting
//帐号&类别&工资是不让改的
newPersonalInfo.setAccountId(oldAccount.getAccountId().trim());
newPersonalInfo.setAccountClass(oldAccount.getAccountClass());
newPersonalInfo.setAccountWage(oldAccount.getAccountWage());
//如果成功修改过密码
//将密码框中字符数组转化为字符串
String newPwd="";
if(changePwdMark==1){
newPwd=new String(newPasswordField.getPassword());
System.out.println(newPwd);
}else{ //如果没有修改密码,则用旧密码
newPwd=oldAccount.getPassword().trim();
}
newPersonalInfo.setPassword(newPwd);
newPersonalInfo.setAccountName(nameTextField.getText().trim());
if(maleRadioButton.isSelected())
newPersonalInfo.setAccountSex(1);
else
newPersonalInfo.setAccountSex(0);
newPersonalInfo.setAccountAddress(addressTextField.getText().trim());
System.out.println("新的个人信息为:");
newPersonalInfo.printAccountInfo();
//保存信息
try{
personalSetting.savePersonalSettingInfo(newPersonalInfo);
}catch(Exception e){
JOptionPane.showMessageDialog(this,"修改失败!","ok",
JOptionPane.INFORMATION_MESSAGE);
}
JOptionPane.showMessageDialog(this,"修改成功!","ok",
JOptionPane.INFORMATION_MESSAGE);
}
//close this frame when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
closeFrame();
}
}
void closeFrame() {
this.dispose();
}
}
/**
* LineBorder lineBorder = (LineBorder)BorderFactory.createLineBorder(Color.BLUE);
accountIdTextLabel.setBorder(lineBorder);
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -