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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JdoitCommented:
Hi,

If you declare personalGreeting at the class level ie. before init(). it should work
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.