2011-11-10 27 views
6

私はこれを理解しようとしてきました。私はさまざまなプログラムで実行しましたので、間違いなくコード内にあります。おそらく簡単なことでしょう。エラーはここ「シンボルを見つけることができません」の原因とその修正方法

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

を言うのコードです:

// 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 
+1

@RobWそれが気違いかどうかを判断しようとしています - > –

答えて

11

エラーメッセージをお読みください。エラーメッセージが大好きです。

それはいくつかの練習がかかりますが、しばらくした後、より明確にそれを見るのは簡単です:ちょうど文として以下の太字テキスト全体に読み:)

error: cannot find symbol [...]

symbol: variable password

location: [in] class Password2.EnterButtonHandler

passwordという名前のものがそのスコープ/コンテキストではありませんEnterButtonHandler)。

ハッピーコーディング。


ヒント:ローカル変数は、おそらくそれはローカル変数であってはならない... 異なるスコープ/コンテキストに同じ名前で存在しているのですか?多くのためThe Java Tutorial: Variablesを参照してください:)

+1

+1愛することを推奨します:-) – kleopatra

0

passwordPassword2コンストラクタにローカルです。

これは、インスタンス変数またはインスタンス変数のいずれかに渡す必要があります。

0

クラスにはpasswordの定義がありません。したがって、それをequalsメソッドに渡すときのエラー。

0

変数passwordが見つかりません。これはコード化したとおり、Password2コンストラクタにのみ存在します。 passwordをクラスメンバ変数にするか、クラスHandlerのコンストラクタに渡す必要があります。そのため、参照を持つことができます。

0
password 

がPassword2というのコンストラクタで宣言されたローカル変数です。 EnterButtonHandler.actionPerformed methodの範囲外です。解決するインスタンス変数にする。

関連する問題