2011-11-10 104 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

password是本地Password2構造。

它應該被傳遞,或實例變量。

0

您的班級沒有password的定義。因此,將其傳遞給equals方法時出現錯誤。

0

它找不到變量password,如您所編碼的那樣,它只存在於Password2構造函數中。您需要將password作爲類成員變量或將其傳遞給Handler類的構造函數,以便它們可以引用它。

0
password 

是密碼2的構造函數聲明的局部變量。它不在您的EnterButtonHandler.actionPerformed method範圍內。讓它成爲一個實例變量來解決。

相關問題