?? changepasswordframe.java
字號:
package baseinforinterface;import java.awt.*;import java.awt.event.*;import javax.swing.*;import data.StockManagementData;import user.User;import maininterface.*;public class ChangePasswordFrame extends JFrame implements ActionListener { JPanel contentPane; JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JLabel jLabel3 = new JLabel(); JPasswordField jPasswordField1 = new JPasswordField(); JPasswordField jPasswordField2 = new JPasswordField(); JPasswordField jPasswordField3 = new JPasswordField(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); //創建字體類 Font dialog13 = new java.awt.Font("Dialog", 0, 13); //聲明數據類 StockManagementData stockManagementData = null; //聲明用戶類 User user = null; //聲明主窗口類 StockManagementMainFrame stockManagementMainFrame = null; public ChangePasswordFrame(StockManagementMainFrame stockManagementMainFrame) { this.stockManagementMainFrame = stockManagementMainFrame; //取得主窗口的數據類 stockManagementData = stockManagementMainFrame.getStockManagementData(); //取得主窗口的用戶類 user = stockManagementMainFrame.getUser(); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { contentPane = (JPanel) this.getContentPane(); //設置窗口面板布局,窗口大小和標題 contentPane.setLayout(null); this.setSize(new Dimension(400, 300)); this.setTitle("修改密碼窗口"); //設置標簽控件的屬性 jLabel1.setText("舊密碼:"); jLabel1.setBounds(new Rectangle(71, 50, 61, 14)); jLabel2.setText("新密碼:"); jLabel2.setBounds(new Rectangle(71, 95, 56, 16)); jLabel3.setText("確認新密碼:"); jLabel3.setBounds(new Rectangle(71, 137, 99, 16)); //設置密碼編輯框控件的屬性 jPasswordField1.setBounds(new Rectangle(183, 46, 132, 22)); jPasswordField2.setBounds(new Rectangle(183, 89, 132, 22)); jPasswordField3.setBounds(new Rectangle(183, 131, 132, 22)); //設置按鈕的屬性 jButton1.setText("修改密碼"); jButton1.setActionCommand("changePassword"); jButton1.setBounds(new Rectangle(71, 200, 101, 25)); jButton1.addActionListener(this); jButton2.setText("退出"); jButton2.setActionCommand("exit"); jButton2.setBounds(new Rectangle(214, 200, 101, 25)); jButton2.addActionListener(this); //為窗口面板加入各個控件 contentPane.add(jLabel1, null); contentPane.add(jLabel2, null); contentPane.add(jLabel3, null); contentPane.add(jPasswordField1, null); contentPane.add(jPasswordField2, null); contentPane.add(jPasswordField3, null); contentPane.add(jButton1, null); contentPane.add(jButton2, null); //設置窗口類的字體 setupFont(); } //設置窗口類的字體 public void setupFont(){ Component[] components = contentPane.getComponents(); for(int i = 0; i < components.length; i++){ components[i].setFont(dialog13); } } protected void processWindowEvent(WindowEvent e) { if (e.getID() == WindowEvent.WINDOW_CLOSING) { exit(); } } //單擊事件處理代碼 public void actionPerformed(ActionEvent e) { //取得按鈕的動作字符串 String actionCommand = e.getActionCommand().trim(); //取得舊密碼 String oldUserPassword = new String(jPasswordField1.getPassword()); //取得新密碼 String newUserPassword = new String(jPasswordField2.getPassword()); //取得確認新密碼 String confirmUserPassword = new String(jPasswordField3.getPassword()); if(actionCommand.equals("changePassword")){ //檢查舊密碼是否正確 if(!oldUserPassword.equals(user.getUserPassword())){ JOptionPane.showMessageDialog(null, "舊密碼輸入錯誤."); return; }else{ //檢查新密碼是否為空 if(newUserPassword.trim().length() == 0){ JOptionPane.showMessageDialog(null, "新密碼不能為空."); return; } //檢查新密碼和確認新密碼是否相同 if(!newUserPassword.equals(confirmUserPassword)){ JOptionPane.showMessageDialog(null, "新密碼和確認新密碼不相同."); return; } //設置用戶類的密碼 user.setUserPassword(newUserPassword); //更改用戶類的密碼 stockManagementData.updateUser(user); //將用戶類傳入主窗口 stockManagementMainFrame.setUser(user); } JOptionPane.showMessageDialog(null, "成功修改密碼."); exit(); } if(actionCommand.equals("exit")){ exit(); } } //退出方法 public void exit(){ //將輸入框的值設為空值 jPasswordField1.setText(""); jPasswordField2.setText(""); jPasswordField3.setText(""); //隱藏窗口 this.setVisible(false); } //設置用戶的方法 public void setUser(User user) { this.user = user; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -