problems in a simple GUI application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stmfc
    New Member
    • May 2007
    • 65

    #1

    problems in a simple GUI application

    i have two classes located in default package, and i got the following errors:
    for the 1st class on line 22: ActionClass cannot be resolved
    for the 2nd class on line 8: text cannot be resolved

    for the first error, the two classes are public and stays in the same package so why cannot see each other?

    for the second error, text variable has no access modifier (friendly access),
    so it should be seen in the second class

    can you help me about these issues? which points i am missing?

    Code:
    import java.awt.*;
    import java.awt.event.*;
    
    public class GuiClass extends Frame {
    	TextField text = new TextField(20);
    	Button b;
    	
    
    	public static void main(String[] args) {
    		GuiClass myWindow = new GuiClass("My first window");
    		myWindow.setSize(350,100);
    		myWindow.setVisible(true);
    	}
    
    	public GuiClass(String title) {
    
    		super(title);
    		setLayout(new FlowLayout());
    		b = new Button("Click me");
    		add(b);
    		add(text);
    		b.addActionListener(ActionClass);
    		
    	}
    
    	
    }
    *************** *************** *************** *******

    Code:
    import java.awt.*;
    import java.awt.event.*;
    
    public class ActionClass implements ActionListener{
    	private int numClicks = 0;
    	public void actionPerformed(ActionEvent e) {
    		numClicks++;
    		text.setText("Button Clicked " + numClicks + " times");
    	}
    	
    	}
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by stmfc
    i have two classes located in default package, and i got the following errors:
    for the 1st class on line 22: ActionClass cannot be resolved
    for the 2nd class on line 8: text cannot be resolved

    for the first error, the two classes are public and stays in the same package so why cannot see each other?

    for the second error, text variable has no access modifier (friendly access),
    so it should be seen in the second class

    can you help me about these issues? which points i am missing?

    Code:
    import java.awt.*;
    import java.awt.event.*;
    
    public class GuiClass extends Frame {
    	TextField text = new TextField(20);
    	Button b;
    	
    
    	public static void main(String[] args) {
    		GuiClass myWindow = new GuiClass("My first window");
    		myWindow.setSize(350,100);
    		myWindow.setVisible(true);
    	}
    
    	public GuiClass(String title) {
    
    		super(title);
    		setLayout(new FlowLayout());
    		b = new Button("Click me");
    		add(b);
    		add(text);
    		b.addActionListener(ActionClass);
    		
    	}
    
    	
    }
    *************** *************** *************** *******

    Code:
    import java.awt.*;
    import java.awt.event.*;
    
    public class ActionClass implements ActionListener{
    	private int numClicks = 0;
    	public void actionPerformed(ActionEvent e) {
    		numClicks++;
    		text.setText("Button Clicked " + numClicks + " times");
    	}
    	
    	}
    Your line #22 should look like this:

    [code=java]
    b.addActionList ener(new ActionClass());
    [/code]

    ... but that's not your problem. Type 'javac' on the command line and see the
    explanation of all the flags. Pay special attention to the -cp flag, the -sourcepath
    flag and the -d flag. The -sourcepath flag tells javac where to find source files.
    The -d flag tells javac where to store the generated class files. The -cp flag tells
    javac where to find compiled class files. If you set all three flags to '.' (dot) you
    can compile both your .java files as an argument '*.java' to javac.

    kind regards,

    Jos

    Comment

    • stmfc
      New Member
      • May 2007
      • 65

      #3
      thanks a lot for the quick reply.
      i corrected line 22, now its ok,
      but still i have the "text cannot be resolved" error on line 8 of the code for the 2nd class. whats the problem here?

      and i am using eclipse, do you think that i should worry about compiler flags?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by stmfc
        thanks a lot for the quick reply.
        i corrected line 22, now its ok,
        but still i have the "text cannot be resolved" error on line 8 of the code for the 2nd class. whats the problem here?

        and i am using eclipse, do you think that i should worry about compiler flags?
        You did not declare a variable called text in that class so it's not known. Is your first class compiling OK now? What does it look like now?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by stmfc
          thanks a lot for the quick reply.
          i corrected line 22, now its ok,
          but still i have the "text cannot be resolved" error on line 8 of the code for the 2nd class. whats the problem here?

          and i am using eclipse, do you think that i should worry about compiler flags?
          Erm, no, if you're using Eclipse you don't have to worry about that; (you should've
          told us that in your original post). You have defined a member 'text' in your GuiClass,
          not in your ActionClass; so an object of ActionClass needs a reference to a GuiClass
          to be able to use a 'text' variable in that first class.

          kind regards,

          Jos

          Comment

          Working...