linux下PasswordField暂时无法输入

来源:互联网 发布:mac天津伊势丹 编辑:程序博客网 时间:2024/05/01 18:51
今天做练习时遇到的问题:import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;/*@author HuangYangyang *@version  2012.11.15  * */public class 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;static final long TOP = 10000;long userDeposit =50000;public void 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{public void actionPerformed(ActionEvent event){checkUser();}}class ClearAction implements ActionListener{public void actionPerformed(ActionEvent event){userNameInput.setText("");userPasswordInput.setText("");}}private enum OutAccount{S10699371,SABER14192,WANGYU3}private enum OutPassword{S1419238911,s1419238912,SABERANIMEHTC3}public void 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();}else if(accountError == true){JOptionPane.showMessageDialog(null, "Account error!");userNameInput.setText("");userPasswordInput.setText("");}else if(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("");}}private void 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{public void actionPerformed(ActionEvent event){setOutput();JOptionPane.showMessageDialog(null,"Deposit success!");inputValue.setText("");}}class InputDepositAction implements ActionListener{public void 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();}else if(exceptionT){OverTopException.tip();}inputValue.setText("");}}}}public void setOutput(){long middleValue = Long.parseLong(inputValue.getText());userDeposit = userDeposit + middleValue;JOptionPane.showMessageDialog(null,"Deposit success !");}public void 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;}}public static void 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).