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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对于登录GUI编译器错误:无法找到符号[复制]相关的知识,希望对你有一定的参考价值。

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

我似乎试图编译这个登录界面时,会得到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
答案

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提供这项服务,独立可插拔的外观和感觉。

以上是关于对于登录GUI编译器错误:无法找到符号[复制]的主要内容,如果未能解决你的问题,请参考以下文章

error LNK2019: 无法解析的外部符号

为啥无法使用苹果按钮登录?

无法找到符号.DAYS Java

无法使用 resharper 解决符号问题,但代码可以编译

当代码编译良好时,Resharper“无法解析符号”[重复]

致命错误 LNK1169:找到一个或多个多重定义符号 (C++)