Link to home
Start Free TrialLog in
Avatar of ramani081399
ramani081399

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of Jdoit
Jdoit

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kalidas
kalidas

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);
          }
Avatar of ramani081399

ASKER

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