You can run an applet in two ways:
- using an Applet viewer that goes with your Java software
- using an HTML page
An applet is created quite similar to creating an ordinary Java program but we only use the parent class Applet to support the applet application. Further, we can include GUI components and events into our applet. The program below illustrates this use.
[+/-] show/hide
/**
*
* @author Rose
*/
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class AppletGreetEvent extends Applet implements ActionListener {
JLabel greeting1 = new JLabel("Hello. Who are you?");
JLabel greeting2 = new JLabel();
Font bigFont = new Font("Times New Roman", Font.ITALIC,24);
JButton pressMe = new JButton("Press Me");
JTextField answer = new JTextField(" ",10);
public void init()
{
add(greeting1);
add(answer);
add(pressMe);
answer.requestFocus();// focus for the textfield
pressMe.addActionListener(this);
greeting2.setFont(bigFont);
add(greeting2);
}
public void start()
{ greeting2.setText("This is a sample applet.");}
public void actionPerformed(ActionEvent thisEvent)
{
String name=answer.getText();
greeting2.setText("Hi "+name);
validate();
}
}
No comments:
Post a Comment