歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux下PasswordField暫時無法輸入

Linux下PasswordField暫時無法輸入

日期:2017/3/1 10:07:12   编辑:Linux編程

今天做練習時遇到的問題:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. /*@author HuangYangyang
  6. *@version 2012.11.15
  7. * */
  8. publicclass BankTest {
  9. JTextField userNameInput;
  10. JPasswordField userPasswordInput;
  11. JFrame loginFrame;
  12. JFrame userCenter;
  13. JTextField inputValue;
  14. int pwNumber;
  15. boolean accountLock;
  16. boolean accountError = true;
  17. boolean passwordError = true;
  18. boolean success;
  19. boolean exceptionB;
  20. boolean exceptionT;
  21. staticfinallong TOP = 10000;
  22. long userDeposit =50000;
  23. publicvoid setLoginGui(){
  24. loginFrame = new JFrame();
  25. loginFrame.setTitle("Bank Login");
  26. JLabel userNameLabel = new JLabel(" Account ");
  27. userNameInput = new JTextField(15);
  28. JLabel userPasswordLabel = new JLabel("Password");
  29. userPasswordInput = new JPasswordField(15);
  30. JButton loginSubmit = new JButton("submit");
  31. JButton loginClear = new JButton("reset");
  32. JPanel panel01 = new JPanel();
  33. panel01.add(new JLabel("Bank Login" ));
  34. JPanel panel02 = new JPanel();
  35. panel02.add(userNameLabel);
  36. panel02.add(userNameInput);
  37. JPanel panel03 = new JPanel();
  38. panel03.add(userPasswordLabel);
  39. panel03.add(userPasswordInput);
  40. JPanel panel04 = new JPanel();
  41. panel04.add(loginSubmit,BorderLayout.WEST);
  42. panel04.add(loginClear,BorderLayout.EAST);
  43. loginFrame.add(panel01,BorderLayout.NORTH);
  44. loginFrame.add(panel02);
  45. loginFrame.add(panel03);
  46. JPanel panel05 = new JPanel();
  47. panel05.add(panel02);
  48. panel05.add(panel03);
  49. loginFrame.add(panel04,BorderLayout.SOUTH);
  50. loginFrame.add(panel05,BorderLayout.CENTER);
  51. loginFrame.setLocationRelativeTo(null);
  52. loginFrame.setSize(320,240);
  53. loginFrame.setVisible(true);
  54. loginSubmit.addActionListener(new SubmitAction());
  55. loginClear.addActionListener(new ClearAction());
  56. }
  57. class SubmitAction implements ActionListener{
  58. publicvoid actionPerformed(ActionEvent event){
  59. checkUser();
  60. }
  61. }
  62. class ClearAction implements ActionListener{
  63. publicvoid actionPerformed(ActionEvent event){
  64. userNameInput.setText("");
  65. userPasswordInput.setText("");
  66. }
  67. }
  68. privateenum OutAccount{
  69. S10699371,SABER14192,WANGYU3
  70. }
  71. privateenum OutPassword{
  72. S1419238911,s1419238912,SABERANIMEHTC3
  73. }
  74. publicvoid checkUser(){
  75. String middlePassword = "";
  76. String middleUser = userNameInput.getText();
  77. char [] password = userPasswordInput.getPassword();
  78. for(char a : password){
  79. middlePassword =middlePassword + a;
  80. }
  81. String middleEnumUser = "";
  82. String middleEnumPassword = "";
  83. for(OutAccount a : OutAccount.values()){
  84. middleEnumUser = a.toString();
  85. if(middleEnumUser.equals(middleUser + middleEnumUser.charAt(middleEnumUser.length() - 1))){
  86. accountError = false;
  87. break;
  88. }
  89. }
  90. for(OutPassword a : OutPassword.values()){
  91. middleEnumPassword = a.toString();
  92. if(middleEnumPassword.equals(middlePassword + middleEnumPassword.charAt(middleEnumPassword.length() - 1))){
  93. passwordError = false;
  94. break;
  95. }
  96. }
  97. if(accountError == false & passwordError == false & middleEnumPassword.charAt(middleEnumPassword.length() - 1) == middleEnumUser.charAt(middleEnumUser.length() - 1)){
  98. loginFrame.dispose();
  99. setBankCenterGui();
  100. }
  101. elseif(accountError == true){
  102. JOptionPane.showMessageDialog(null, "Account error!");
  103. userNameInput.setText("");
  104. userPasswordInput.setText("");
  105. }
  106. elseif(accountError == false & passwordError == true){
  107. if(pwNumber == 3){
  108. JOptionPane.showMessageDialog(null,"Account locked!");
  109. loginFrame.dispose();
  110. }
  111. else{
  112. JOptionPane.showMessageDialog(null,"Password error! Pealse check password!");
  113. userPasswordInput.setText("");
  114. pwNumber = pwNumber + 1;
  115. }
  116. }
  117. else{
  118. JOptionPane.showMessageDialog(null,"Account or Password error!");
  119. userNameInput.setText("");
  120. userPasswordInput.setText("");
  121. }
  122. }
  123. privatevoid setBankCenterGui() {
  124. userCenter = new JFrame("Bank Center");
  125. JPanel panel01 = new JPanel();
  126. panel01.add(new JLabel("Bank Center"));
  127. JPanel panel02 = new JPanel();
  128. inputValue = new JTextField(5);
  129. panel02.add(inputValue);
  130. JButton outputDeposit = new JButton("Withdrawal");
  131. panel02.add(outputDeposit);
  132. JButton inputDeposit = new JButton(" Deposit ");
  133. panel02.add(inputDeposit);
  134. userCenter.add(BorderLayout.NORTH,panel01);
  135. userCenter.add(BorderLayout.CENTER,panel02);
  136. outputDeposit.addActionListener(new InputDepositAction());
  137. inputDeposit.addActionListener(new OutputDepositAction());
  138. userCenter.setLocationRelativeTo(null);
  139. userCenter.setSize(350,200);
  140. userCenter.setVisible(true);
  141. }
  142. class OutputDepositAction implements ActionListener{
  143. publicvoid actionPerformed(ActionEvent event){
  144. setOutput();
  145. JOptionPane.showMessageDialog(null,"Deposit success!");
  146. inputValue.setText("");
  147. }
  148. }
  149. class InputDepositAction implements ActionListener{
  150. publicvoid actionPerformed(ActionEvent event){
  151. try {
  152. setInput();
  153. } catch (BalanceLackException e) {
  154. e.printStackTrace();
  155. } catch (OverTopException e) {
  156. e.printStackTrace();
  157. }
  158. finally{
  159. if(success){
  160. JOptionPane.showMessageDialog(null,"Withdrawal success!");
  161. success = false;
  162. inputValue.setText("");
  163. }
  164. else{
  165. JOptionPane.showMessageDialog(null,"Withdrawal failed!");
  166. if(exceptionB){
  167. BalanceLackException.tip();
  168. }
  169. elseif(exceptionT){
  170. OverTopException.tip();
  171. }
  172. inputValue.setText("");
  173. }
  174. }
  175. }
  176. }
  177. publicvoid setOutput(){
  178. long middleValue = Long.parseLong(inputValue.getText());
  179. userDeposit = userDeposit + middleValue;
  180. JOptionPane.showMessageDialog(null,"Deposit success !");
  181. }
  182. publicvoid setInput() throws BalanceLackException , OverTopException{
  183. long middleValue = Long.parseLong(inputValue.getText());
  184. if(userDeposit - middleValue < 0 )
  185. exceptionB = true;
  186. if(Long.parseLong(inputValue.getText()) > 30000)
  187. exceptionT = true;
  188. if(userDeposit - middleValue > 0 & Long.parseLong(inputValue.getText()) < 30000 ){
  189. userDeposit = userDeposit - middleValue;
  190. success = true;
  191. }
  192. }
  193. publicstaticvoid main(String [] args){
  194. BankTest go = new BankTest();
  195. go.setLoginGui();
  196. }
  197. }

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).

Copyright © Linux教程網 All Rights Reserved