GUI Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kid Programmer
    New Member
    • Mar 2008
    • 176

    GUI Error

    Hello guys. I am receiving an error while creating a program that will capitalize words entered in a text box. I have 2 files with code. Here they are:
    File No. 1:
    Code:
    package capitalizeme;
    
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import java.awt.Container;
    import java.awt.FlowLayout;
    
    public class CapitalizeMe {
    
        public static void main(String[] args) {
            JFrame frame;
            Container contentPane;
            JTextField textfield;
            JButton button;
            FlowLayout layout;
            
            frame = new JFrame();
            frame.setTitle("Handy capitalization service");
            
            contentPane = frame.getContentPane();
            
            textfield = new JTextField("Type your text here.", 20);
            
            button = new JButton("Capitalize");
            button.addActionListener(new MyActionListener(textfield));
            
            contentPane.add(textfield);
            contentPane.add(button);
            layout = new FlowLayout();
            
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    }
    File No. 2:
    Code:
    import javax.swing.JTextField;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    class MyActionListener implements ActionListener {
    
    	JTextField textfield;
    	
    	MyActionListener(JTextField textfield) {
    		this.textfield = textfield;
    	}
    
    	public void actionPerformed(ActionEvent e) {
    		textfield.setText(textfield.getText().toUpperCase());
    	}
    }
    Here is my error:
    symbol : class MyActionListene r
    location: class capitalizeme.Ma in
    button.addActio nListener(new MyActionListene r(textfield));
    1 error
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    What's the actual error it's giving you? You've told us what line, but not what it's telling you is wrong with that line.

    Comment

    • Kid Programmer
      New Member
      • Mar 2008
      • 176

      #3
      Originally posted by Laharl
      What's the actual error it's giving you? You've told us what line, but not what it's telling you is wrong with that line.
      The error is:

      cannot find symbol

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        I think this is a package/scope error. Add a package statement to the second file and that should help.

        Comment

        • Kid Programmer
          New Member
          • Mar 2008
          • 176

          #5
          Originally posted by Laharl
          I think this is a package/scope error. Add a package statement to the second file and that should help.
          Thanks that fixed it.

          Comment

          Working...