今天做練習時遇到的問題:
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- /*@author HuangYangyang
- *@version 2012.11.15
- * */
- publicclass BankTest {
- JTextField userNameInput;
- JPasswordField userPasswordInput;
- JFrame loginFrame;
- JFrame userCenter;
- JTextField inputValue;
- int pwNumber;
- boolean accountLock;
- boolean accountError = true;
- boolean passwordError = true;
- boolean success;
- boolean exceptionB;
- boolean exceptionT;
- staticfinallong TOP = 10000;
- long userDeposit =50000;
- publicvoid setLoginGui(){
- loginFrame = new JFrame();
- loginFrame.setTitle("Bank Login");
- JLabel userNameLabel = new JLabel(" Account ");
- userNameInput = new JTextField(15);
- JLabel userPasswordLabel = new JLabel("Password");
- userPasswordInput = new JPasswordField(15);
- JButton loginSubmit = new JButton("submit");
- JButton loginClear = new JButton("reset");
- JPanel panel01 = new JPanel();
- panel01.add(new JLabel("Bank Login" ));
- JPanel panel02 = new JPanel();
- panel02.add(userNameLabel);
- panel02.add(userNameInput);
- JPanel panel03 = new JPanel();
- panel03.add(userPasswordLabel);
- panel03.add(userPasswordInput);
- JPanel panel04 = new JPanel();
- panel04.add(loginSubmit,BorderLayout.WEST);
- panel04.add(loginClear,BorderLayout.EAST);
- loginFrame.add(panel01,BorderLayout.NORTH);
- loginFrame.add(panel02);
- loginFrame.add(panel03);
- JPanel panel05 = new JPanel();
- panel05.add(panel02);
- panel05.add(panel03);
- loginFrame.add(panel04,BorderLayout.SOUTH);
- loginFrame.add(panel05,BorderLayout.CENTER);
- loginFrame.setLocationRelativeTo(null);
- loginFrame.setSize(320,240);
- loginFrame.setVisible(true);
- loginSubmit.addActionListener(new SubmitAction());
- loginClear.addActionListener(new ClearAction());
- }
- class SubmitAction implements ActionListener{
- publicvoid actionPerformed(ActionEvent event){
- checkUser();
- }
- }
- class ClearAction implements ActionListener{
- publicvoid actionPerformed(ActionEvent event){
- userNameInput.setText("");
- userPasswordInput.setText("");
- }
- }
- privateenum OutAccount{
- S10699371,SABER14192,WANGYU3
- }
- privateenum OutPassword{
- S1419238911,s1419238912,SABERANIMEHTC3
- }
- publicvoid checkUser(){
- String middlePassword = "";
- String middleUser = userNameInput.getText();
- char [] password = userPasswordInput.getPassword();
- for(char a : password){
- middlePassword =middlePassword + a;
- }
- String middleEnumUser = "";
- String middleEnumPassword = "";
- for(OutAccount a : OutAccount.values()){
- middleEnumUser = a.toString();
- if(middleEnumUser.equals(middleUser + middleEnumUser.charAt(middleEnumUser.length() - 1))){
- accountError = false;
- break;
- }
- }
- for(OutPassword a : OutPassword.values()){
- middleEnumPassword = a.toString();
- if(middleEnumPassword.equals(middlePassword + middleEnumPassword.charAt(middleEnumPassword.length() - 1))){
- passwordError = false;
- break;
- }
- }
- if(accountError == false & passwordError == false & middleEnumPassword.charAt(middleEnumPassword.length() - 1) == middleEnumUser.charAt(middleEnumUser.length() - 1)){
- loginFrame.dispose();
- setBankCenterGui();
- }
- elseif(accountError == true){
- JOptionPane.showMessageDialog(null, "Account error!");
- userNameInput.setText("");
- userPasswordInput.setText("");
- }
- elseif(accountError == false & passwordError == true){
- if(pwNumber == 3){
- JOptionPane.showMessageDialog(null,"Account locked!");
- loginFrame.dispose();
- }
- else{
- JOptionPane.showMessageDialog(null,"Password error! Pealse check password!");
- userPasswordInput.setText("");
- pwNumber = pwNumber + 1;
- }
- }
- else{
- JOptionPane.showMessageDialog(null,"Account or Password error!");
- userNameInput.setText("");
- userPasswordInput.setText("");
- }
- }
- privatevoid setBankCenterGui() {
- userCenter = new JFrame("Bank Center");
- JPanel panel01 = new JPanel();
- panel01.add(new JLabel("Bank Center"));
- JPanel panel02 = new JPanel();
- inputValue = new JTextField(5);
- panel02.add(inputValue);
- JButton outputDeposit = new JButton("Withdrawal");
- panel02.add(outputDeposit);
- JButton inputDeposit = new JButton(" Deposit ");
- panel02.add(inputDeposit);
- userCenter.add(BorderLayout.NORTH,panel01);
- userCenter.add(BorderLayout.CENTER,panel02);
- outputDeposit.addActionListener(new InputDepositAction());
- inputDeposit.addActionListener(new OutputDepositAction());
- userCenter.setLocationRelativeTo(null);
- userCenter.setSize(350,200);
- userCenter.setVisible(true);
- }
- class OutputDepositAction implements ActionListener{
- publicvoid actionPerformed(ActionEvent event){
- setOutput();
- JOptionPane.showMessageDialog(null,"Deposit success!");
- inputValue.setText("");
- }
- }
- class InputDepositAction implements ActionListener{
- publicvoid actionPerformed(ActionEvent event){
- try {
- setInput();
- } catch (BalanceLackException e) {
- e.printStackTrace();
- } catch (OverTopException e) {
- e.printStackTrace();
- }
- finally{
- if(success){
- JOptionPane.showMessageDialog(null,"Withdrawal success!");
- success = false;
- inputValue.setText("");
- }
- else{
- JOptionPane.showMessageDialog(null,"Withdrawal failed!");
- if(exceptionB){
- BalanceLackException.tip();
- }
- elseif(exceptionT){
- OverTopException.tip();
- }
- inputValue.setText("");
- }
- }
- }
- }
- publicvoid setOutput(){
- long middleValue = Long.parseLong(inputValue.getText());
- userDeposit = userDeposit + middleValue;
- JOptionPane.showMessageDialog(null,"Deposit success !");
- }
- publicvoid setInput() throws BalanceLackException , OverTopException{
- long middleValue = Long.parseLong(inputValue.getText());
- if(userDeposit - middleValue < 0 )
- exceptionB = true;
- if(Long.parseLong(inputValue.getText()) > 30000)
- exceptionT = true;
- if(userDeposit - middleValue > 0 & Long.parseLong(inputValue.getText()) < 30000 ){
- userDeposit = userDeposit - middleValue;
- success = true;
- }
- }
- publicstaticvoid main(String [] args){
- BankTest go = new BankTest();
- go.setLoginGui();
- }
- }
Linux下PasswordField無法輸入,只有把光標從密碼框移開,讓密碼框重新獲得焦點,可以輸入了。一開始以為是java的bug。後來知道是因為PasswordField組件默認不獲取系統輸入法支持,需要自己設置。
加入 userPasswordInput.enableInputMethods(true); 運行程序ok,可以輸入了。
詳見文檔說明:
By default, JPasswordField disables input methods; otherwise, input characters could be visible while they were composed using input methods. If an application needs the input methods support, please use the inherited method, enableInputMethods(true).