Applet Not Giving Desired Results

This is the applet I am trying to write
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class GreetLate extends Applet implements ActionListener
{
   Label greeting = new Label("Hello, world!");
   Font bigFont = new Font("TimesRoman", Font.ITALIC, 24);
   Button pressMe = new Button("Click here!");
   TextField answer = new TextField("", 10);

   public void init()
   {
      greeting.setFont(bigFont);
      greeting.setText("This is new text");
      add(greeting);
      add(answer);
      add(pressMe);
      pressMe.addActionListener(this);
      answer.requestFocus();
   }

   public void actionPerformed(ActionEvent thisEvent)
   {
      String name = answer.getText();
      Label personalGreeting = new Label("");
      personalGreeting.setText("Hi " + name);
      add(personalGreeting);
      }
}

.html
<html>
<applet code="GreetLate.class" width=285 height=65>
</applet>
</html>

I dont see the personalgreeting.settext written out.Why is this?
ramani081399Asked:
Who is Participating?
 
JdoitConnect With a Mentor Commented:
Hi,

If you declare personalGreeting at the class level ie. before init(). it should work
0
 
kalidasCommented:
U R not trapping any action Event where is the objet.getSource() , in ur actionPerformed.

modify  the code in ur acrtionPerformed method , and it will work fine

 public void actionPerformed(ActionEvent thisEvent)
  {
      Object obj = thisEvent.getSource();
       if (obj.equals(pressMe))
        {
             
                 String name = answer.getText();
                         Label personalGreeting = new Label("");
                         personalGreeting.setText("Hi " + name);
                         add(personalGreeting);
          }
0
 
ramani081399Author Commented:
Jdoit,

 Thanks it worked. But I would not like the originaly typed text to remain when I enter a new text.
Suppose ,In the text box I type TOM and click the press button it writes out HiTom. Now When I next enter Peter in the text box and click it writes out Hi peter and the Hi Tom still remains.How Do i get rid of the Hi Tom .
Also Could you please tell me a good web site for learning to write applets which givs lot of explanations and gives a variety of examples.

Ramani
0
All Courses

From novice to tech pro — start learning today.