2011-11-10 11 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 में दायरे में नहीं है। इसे हल करने के लिए इसे एक आवृत्ति चर बनाओ।

संबंधित मुद्दे

 संबंधित मुद्दे