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

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content







Popular Article