in Swing, applet imlementation with Japplet, that inheritance from Applet class(Japplet) and has a Container , with method called getContentPane(), with a declaration
Container getContentPane()
next to add another components , we need method add(), with a declaration
void add(comp)
example:
Demo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DemoAppletSwing extends JApplet
implements ActionListener {
public JTextField textField;
public JButton button;
public void init() {
// mendapatkan container
Container cont = getContentPane();
cont.setLayout(new FlowLayout());
// menambahkan komponen JTextField
textField = new JTextField(25);
cont.add(textField, BorderLayout.CENTER);
// menambahkan komponen JButton
button = new JButton("Tombol Applet");
button.addActionListener(this);
cont.add(button, BorderLayout.CENTER);
}
// menangani event klik pada komponen JButton
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button) {
textField.setText("Anda telah melakukan klik " +
"pada tombol Applet");
}
}
}
import java.awt.event.*;
import javax.swing.*;
public class DemoAppletSwing extends JApplet
implements ActionListener {
public JTextField textField;
public JButton button;
public void init() {
// mendapatkan container
Container cont = getContentPane();
cont.setLayout(new FlowLayout());
// menambahkan komponen JTextField
textField = new JTextField(25);
cont.add(textField, BorderLayout.CENTER);
// menambahkan komponen JButton
button = new JButton("Tombol Applet");
button.addActionListener(this);
cont.add(button, BorderLayout.CENTER);
}
// menangani event klik pada komponen JButton
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button) {
textField.setText("Anda telah melakukan klik " +
"pada tombol Applet");
}
}
}
and with same directory
Test.html
<html>
<head>
</head>
<body>
<applet code="Test.class" width="300" height="100" >
</applet>
</body>
</html>
<head>
</head>
<body>
<applet code="Test.class" width="300" height="100" >
</applet>
</body>
</html>