什么原因导致“无法找到符号”和如何解决它? [英] What causes "Can't find Symbol" and how to fix it?

查看:315
本文介绍了什么原因导致“无法找到符号”和如何解决它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力想出这一点,我在不同的程序运行它,所以它绝对在代码中。也许很容易。错误提示


Password2.java:90:错误:找不到符号
if(pw.equals(password))
^
symbol:variable password
location:class Password2.EnterButtonHandler
1错误


这里是代码:

  // Password1.java 

import javax.swing。
import java.awt。*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Password2 extends JFrame //继承JFrame类
{
//静态最终变量来保存框架尺寸(以像素为单位)
private static final int WIDTH = 400;
private static final int HEIGHT = 120;

//声明标签,字段,按钮等。
private JLabel enterLabel,validLabel,resultLabel;
private JTextField pwTextField;
private JButton enterB,clearB;

private EnterButtonHandler ebHandler;
private ClearButtonHandler cbHandler;

public Password2()//构造函数定义框架
{
setTitle(Password Checker); //设置框架的标题
setSize(WIDTH,HEIGHT); //设置框架大小

//准备容器
容器窗格= getContentPane();
GridLayout aGrid = new GridLayout(3,2,5,5); // create a 3 row 2 column layout
pane.setLayout(aGrid); //设置框架的布局

String password =hello;

//实例化JLabels
enterLabel = new JLabel(Enter Password:);
validLabel = new JLabel(Validation:);
resultLabel = new JLabel();

//实例化文本字段
pwTextField = new JPasswordField(30);

//实例化按钮
enterB = new JButton(Enter);
clearB = new JButton(Clear);

//初始化按钮处理程序
ebHandler = new EnterButtonHandler();
enterB.addActionListener(ebHandler);

//初始化按钮处理程序
cbHandler = new ClearButtonHandler();
clearB.addActionListener(cbHandler);


pane.add(enterLabel);
pane.add(pwTextField);
pane.add(validLabel);
pane.add(resultLabel);
pane.add(enterB);
pane.add(clearB);

//调用中心框架方法
centerFrame(WIDTH,HEIGHT);

} //结构构造函数

//在屏幕上显示中心GUI
public void centerFrame(int frameWidth,int frameHeight)
{
//创建工具箱对象
Toolkit aToolkit = Toolkit.getDefaultToolkit();

//创建带有用户屏幕信息的维对象
尺寸屏幕= aToolkit.getScreenSize();

//分配框架左上角的x,y位置
int xUpperLeft =(screen.width - frameWidth)/ 2;
int yUpperLeft =(screen.height - frameHeight)/ 2;

//在用户屏幕上定位框架的方法
setBounds(xUpperLeft,yUpperLeft,frameWidth,frameHeight);
}

private class EnterButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String pw = pwTextField.getText );

if(pw.equals(password))
{
resultLabel.setText(Password Accepted);
pwTextField.setText();
}
else
{
resultLabel.setText(Password Rejected);
pwTextField.setText();
}
}
}
private class ClearButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
resultLabel .setText();
pwTextField.setText();
}

}
public static void main(String [] args)
{
JFrame aPassword2 = new Password2(); //创建JFrame对象
aPassword2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aPassword2.setVisible(true);
}
} //类结束


解决方案

阅读错误消息,喜欢错误消息。



这需要一些练习,但是一段时间后,它更容易看清楚: )

错误:找不到符号 [...]



符号:变量密码



位置: [in] Password2。 EnterButtonHandler


没有名为的密码 EnterButtonHandler )。



p>




提示:在不同的变量中有一个 em> scope / context ...也许它不应该是一个本地变量?有关更多信息,请参见 Java教程:变量)。


I've been trying to figure this out, I've run it in different programs so it's definitely in the code. Probably something easy too. The error says

Password2.java:90: error: cannot find symbol if(pw.equals(password)) ^ symbol: variable password location: class Password2.EnterButtonHandler 1 error

Here is the code:

// Password1.java

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

public class Password2 extends JFrame // inherits from the JFrame class 
{
    // static final variables to hold frame dimensions (in pixels) 
    private static final int WIDTH = 400;
    private static final int HEIGHT = 120;

    //declare labels, fields, buttons, etc.
    private JLabel enterLabel, validLabel, resultLabel;
    private JTextField pwTextField;
    private JButton enterB, clearB;

    private EnterButtonHandler ebHandler;
    private ClearButtonHandler cbHandler;

    public Password2() // constructor defines frame 
    { 
            setTitle( "Password Checker" ); // set the title of the frame
        setSize( WIDTH, HEIGHT ); // set the frame size

        // prepare the container 
        Container pane = getContentPane();
        GridLayout aGrid = new GridLayout( 3, 2, 5, 5 ); // create a 3 row 2 column layout
        pane.setLayout( aGrid ); // set the layout for the frame

        String password = "hello";

        //instantiate JLabels
        enterLabel = new JLabel("Enter Password: ");
        validLabel = new JLabel("Validation: ");
        resultLabel = new JLabel("");

        //instantiate text fields
        pwTextField = new JPasswordField( 30 );

        //instantiate buttons
        enterB = new JButton("Enter");
        clearB = new JButton("Clear");

        //initialize button handler
        ebHandler = new EnterButtonHandler();
        enterB.addActionListener(ebHandler);

        //initialize button handler
        cbHandler = new ClearButtonHandler();
        clearB.addActionListener(cbHandler);


        pane.add(enterLabel);
        pane.add(pwTextField);
        pane.add(validLabel);
        pane.add(resultLabel);
        pane.add(enterB);
        pane.add(clearB);

        //calls center frame method
        centerFrame( WIDTH, HEIGHT );

    }// end constructor

    //methood to center GUI on screen
    public void centerFrame( int frameWidth, int frameHeight)
    {
        //create toolkit object
        Toolkit aToolkit = Toolkit.getDefaultToolkit();

        //create a dimension object with user screen information
        Dimension screen = aToolkit.getScreenSize();

        //assign x, y position of upper left corner of frame
        int xUpperLeft = ( screen.width - frameWidth ) / 2;
        int yUpperLeft = ( screen.height - frameHeight ) / 2;

        //method to position frame on user's screen
        setBounds( xUpperLeft, yUpperLeft, frameWidth, frameHeight );
    }

    private class EnterButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String pw = pwTextField.getText();

            if(pw.equals(password))
            {
                resultLabel.setText("Password Accepted");
                pwTextField.setText("");
            }
            else
            {
                resultLabel.setText("Password Rejected");
                pwTextField.setText("");
            }
        }
    }
    private class ClearButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            resultLabel.setText("");
            pwTextField.setText("");
        }

    }
    public static void main(String [] args) 
    {
        JFrame aPassword2 = new Password2(); // create the JFrame object
        aPassword2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        aPassword2.setVisible(true);
    }
    } // end of class

解决方案

Read the error message, love the error message.

It takes some practice, but after awhile it's easy to see it more clearly: just read across the bold text below as a sentence :)

error: cannot find symbol [...]

symbol: variable password

location: [in] class Password2.EnterButtonHandler

There is nothing named password in that scope/context (EnterButtonHandler).

Happy coding.


Hint: there is a local variable with the same name in a different scope/context... perhaps it shouldn't be a local variable? See The Java Tutorial: Variables for more :)

这篇关于什么原因导致“无法找到符号”和如何解决它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆