对于登录GUI编译器错误:无法找到符号[复制]

问题描述 投票:-2回答:1

这个问题已经在这里有一个答案:

我似乎试图编译这个登录界面时,会得到2个错误。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Login extends JFrame
{
   final int USER_CONS = 8;
   final int PASS_CONS = 8;
   int ATTEMPTS = 3;

   private JCheckBox userCheck;
   private JCheckBox passCheck;
   private JButton login;
   private JButton exit;
   private JLabel userLabel;
   private JLabel passLabel;
   private JLabel attemptsLabel;
   private JTextField userField;
   private JTextField passField;
   private JTextField attemptsField;


   public static void main (String[] args)
   {
      new Login();
   }

   public Login()
   {
      setTitle("Login Screen");
      setSize(400,400);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
         setLayout(new BorderLayout());
      BuildPanel();
   }
   public void BuildPanel()
   {
      JPanel panel = new JPanel();
      JPanel panelNorth = new JPanel();
      JPanel panelSouth = new JPanel();
      JPanel panelEast = new JPanel();
      JPanel panelWest = new JPanel();
      JPanel panelCenter = new JPanel();

      add(panelNorth, BorderLayout.NORTH);
       add(panelSouth, BorderLayout.SOUTH);
        add(panelEast, BorderLayout.EAST);
         add(panelWest, BorderLayout.WEST);
          add(panelCenter, BorderLayout.CENTER);

      userLabel = new JLabel("Username");
         userField = new JTextField(USER_CONS);
         userCheck = new JCheckBox();

      passLabel = new JLabel ("Password");
         passField = new JTextField(PASS_CONS);
         passCheck = new JCheckBox();

      attemptsLabel = new JLabel("Max attempts");
      attemptsField = new JTextField(4);

      JButton login = new JButton("Login");
      JButton exit = new JButton("Exit");


      exit.addActionListener(new ButtonListener());
      login.addActionListener(new ButtonListener());

      passCheck.addItemListener(new CheckBoxListener());

      panelSouth.add(login, BorderLayout.EAST);
      panelSouth.add(exit, BorderLayout.WEST);

      panelCenter.add(userLabel);
      panelCenter.add(userField);
      panelCenter.add(userCheck);
      panelCenter.add(passLabel);
      panelCenter.add(passField);
      panelCenter.add(passCheck);

      panelWest.add(attemptsLabel);
      panelWest.add(attemptsField);
   }

   private class ButtonListener implements ActionListener
   {
      public void actionPerformed(ActionEvent closing)
      {
         String actionCommand = closing.getActionCommand();
         String userText = userField.getText();
         String passText = passField.getText();


         attemptsField.setText(String.valueOf(ATTEMPTS));

         if (actionCommand.equals("Exit"))
            {
               System.exit(0);
            }
         else if (userText.length() > USER_CONS || passText.length() > PASS_CONS) 
            {            

                --ATTEMPTS;

               JOptionPane.showMessageDialog(null,"Invalid login. Try again.");
                  if( ATTEMPTS == -1)
                  {
                     JOptionPane.showMessageDialog(null,"You have no more attempts. Goodbye.");
                     System.exit(0);
                  }
            }
         else if (userText.length() <= USER_CONS || passText.length() <= PASS_CONS) 
            {            
               JOptionPane.showMessageDialog(null,"You have successfully logged in.");
            }
         }
      }
      private class CheckBoxListener implements ItemListener
      {
         public void itemStateChanged(ItemEvent e)
         {
            String passText = passField.getText();

            if (e.getSource() == passCheck)
               passText.setEchoChar('*');
            else
               passText.setEchoChar((char)0); 
         }
       }
   }

该错误在于我的私有类CheckBoxListener。我试图让这个如果复选框被选中(即在这种情况下隐藏的密码),它将使在文本字段中每个字符*。但由于某些原因,我得到的错误,

Login.java:150: error: cannot find symbol
           passText.setEchoChar('*');
                   ^
  symbol:   method setEchoChar(char)
  location: variable passText of type String
Login.java:152: error: cannot find symbol
               passText.setEchoChar((char)0); 
                       ^
  symbol:   method setEchoChar(char)
  location: variable passText of type String
2 errors
java swing user-interface awt jgrasp
1个回答
1
投票

passText是一个字符串,并且没有方法setEchoChar。它必须是passField.setEchoChar('*')

private class CheckBoxListener implements ItemListener
  {
     public void itemStateChanged(ItemEvent e)
     {
        String passText = passField.getText();

        if (e.getSource() == passCheck)
           passField.setEchoChar('*');
        else
           passField.setEchoChar((char)0); 
     }

   }

更新:

你的领域必须是JPasswordField

从文档JTextField作者:

不直接提供的方法setEchoChar和getEchoChar以避免新的实现可插入的外观和感觉不慎露出密码字符。为了提供密码般的服务单独的类JPasswordField中扩展了JTextField提供这项服务,独立可插拔的外观和感觉。

© www.soinside.com 2019 - 2024. All rights reserved.