by Dinesh Thakur Category: Swing

JApplet is a class that represents the Swing applet. It is a subclass of Applet class and must be extended by all the applets that use Swing. It provides all the functionalities of the AWT applet as well as support for menubars and layering of components. Whenever we require to add a component to it, the component is added to the content pane.

The JApplet defines the following constructor.

JApplet ()

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class JHello2JavaExample extends JApplet implements ActionListener
    {
                    JLabel Grtng = new JLabel("Hello. Who are you?");
                    Font FntOne = new Font("Arial", Font.BOLD, 36);
                    Font FntTwo = new Font("Arial", Font.ITALIC, 48);
                    JTextField answr = new JTextField(10);
                    JButton PrsMe = new JButton("Press me");
                    JLabel PrsnlGrtng = new JLabel(" ");
                    Container cntnr = getContentPane();
                    public void init()
                    {
                          Grtng.setFont(FntOne);
                          PrsnlGrtng.setFont(FntTwo);
                          cntnr.add(Grtng);
                          cntnr.add(answr);
                          cntnr.add(PrsMe);
                          cntnr.setLayout(new FlowLayout());
                          cntnr.setBackground(Color.YELLOW);
                          PrsMe.addActionListener(this);
                          answr.addActionListener(this);
                    }
                          public void actionPerformed(ActionEvent e)
                           {
                              String name = answr.getText();
                              cntnr.remove(Grtng);
                              cntnr.remove(PrsMe);
                              cntnr.remove(answr);
                              PrsnlGrtng.setText("Hello, " + name + "! ");
                              cntnr.add(PrsnlGrtng);
                              cntnr.setBackground(Color.CYAN);
                              validate();
                          }
     }

Hello Program in Japplet Java Swing Example

Popular Article