This section contains the detail about the basic operators in java.
Introduction to Swing Applet
Any applet that contains Swing components must be implemented with a subclass of JApplet. JApplet is a subclass of java.applet.Applet .
Example
In this example, We embed a Swing applet into web browser. This applet contains two buttons, when you click on any one of it, it shows which one is clicked.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwingApplet extends JApplet {
JButton jbtnOne;
JButton jbtnTwo;
JLabel jlab;
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable () {
public void run() {
guiInit(); // initialize the GUI
}
});
} catch(Exception exc) {
System.out.println("Can't create because of "+ exc);
}
}
// Called second, after init(). Also called
// whenever the applet is restarted.
public void start() {
// Not used by this applet.
}
// Called when the applet is stopped.
public void stop() {
// Not used by this applet.
}
// Called when applet is terminated. This is
// the last method executed.
public void destroy() {
// Not used by this applet.
}
// Setup and initialize the GUI.
private void guiInit() {
// Set the applet to use flow layout.
setLayout(new FlowLayout());
// Create two buttons and a label.
jbtnOne = new JButton("One");
jbtnTwo = new JButton("Two");
jlab = new JLabel("Press a button.");
// Add action listeners for the buttons.
jbtnOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jlab.setText("Button One pressed.");
}
});
jbtnTwo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jlab.setText("Button Two pressed.");
}
});
// Add the components to the applet's content pane.
getContentPane().add(jbtnOne);
getContentPane().add(jbtnTwo);
getContentPane().add(jlab);
}
}
For invoking it into a browser, the following Html code is needed :
<html> <title>The Applet Demo</title> <hr> <applet code="SwingApplet.class" width=240 height=100> This message only appears if your browser is not java enabled or there is some error. </applet> <hr> </html>
Output
When you click on the first button, it will display :
If you click on second button, it will display :


[ 0 ] Comments