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(ActionEven t 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?
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(
answer.requestFocus();
}
public void actionPerformed(ActionEven
{
String name = answer.getText();
Label personalGreeting = new Label("");
personalGreeting.setText("
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
modify the code in ur acrtionPerformed method , and it will work fine
public void actionPerformed(ActionEven
{
Object obj = thisEvent.getSource();
if (obj.equals(pressMe))
{
String name = answer.getText();
Label personalGreeting = new Label("");
personalGreeting.setText("
add(personalGreeting);
}