Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Event Handling - (compilation error)

  Asked By: Bailey    Date: Nov 27    Category: Java    Views: 953
  

Kindly help me for the execution of the following codes

The following codes not compiling.

================================


import java.awt.*;
import java.awt.event.*;

public class Test2 implements ActionListener{


public static void main(String[]args){

Frame frame = new Frame("Hullo");

frame.setBounds(0,0,200,200);

FlowLayout flow = new FlowLayout();

frame.setLayout(flow);

TextField text1 = new TextField("This is .....");

TextField text2 = new TextField("Second one");

Button button = new Button("First");


frame.add(text1);
frame.add(text2);
frame.add(button);

frame.setVisible(true);

Test2 test = new Test2();
button.addActionListener(test);

}

public void actionPerformed(ActionEvent ee){

String msg = new String("Button pressed");

if(ee.getSource() == button){
text2=setText(msg);
}
}

}

=======================================

error:

D:\ceba_java\Test2.java:42: cannot resolve symbol
symbol : variable button
location: class Test2
if(ee.getSource() == button){
^
D:\ceba_java\Test2.java:43: cannot resolve symbol
symbol : variable text2
location: class Test2
text2=setText(msg);
^
D:\ceba_java\Test2.java:43: cannot resolve symbol
symbol : method setText (java.lang.String)
location: class Test2
text2=setText(msg);
^
3 errors

Tool completed with exit code 1

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Alyssa Campbell     Answered On: Nov 27

Check this code it should compile


import java.awt.*;
import java.awt.event.*;

public class  Test2 implements ActionListener{
Frame frame  = null;
FlowLayout flow = null;
TextField text1 = null;
TextField text2 = null;
Button button = null;

public Test2(){
frame = new Frame("Hullo");
frame.setBounds(0,0,200,200);
flow = new FlowLayout();
frame.setLayout(flow);
text1 = new TextField("This is .....");
text2 = new TextField("Second one");
button = new Button("First");

frame.add(text1);
frame.add(text2);
frame.add(button);
frame.setVisible(true);
button.addActionListener(this);
}

public static  void main(String[]args){
Test2 test = new Test2();

}

public void  actionPerformed(ActionEvent ee){

String msg = new String("Button pressed");

if(ee.getSource() == button){
text2.setText(msg);
}
}

}

 
Didn't find what you were looking for? Find more on Event Handling - (compilation error) Or get search suggestion and latest updates.




Tagged: