Share on Google+Share on Google+

Create an JApplet with three Jbuttons

In this section, we are going to perform an action on three buttons using applet.

Create an JApplet with three Jbuttons

In this section, we have created an applet with three buttons Morning, Afternoon and Evening. On clicking these buttons, you will get the messages "Good Morning to you", "Good Afternoon to you" and "Good evening to you" respectively. We have used different colors to give different appearances to the messages.

Here is the code:

import java.applet.Applet; 
import java.awt.*; 
import java.awt.event.*;
public class SApplet extends Applet implements ActionListener {
   String st="";
   private Button morningButton, afternoonButton,eveningButton;
   public void init() { 
       morningButton = new Button("Morning")
      afternoonButton = new Button("Afternoon")
    eveningButton = new Button("Evening")
      public void paint(Graphics g) {
      if(st.equals("Good Morning to you")){
  g.setFont(new Font("Helvetica",Font.BOLD,20));
      if(st.equals("Good Afternoon to you")){
  g.setFont(new Font("Arial",Font.BOLD,20));
      if(st.equals("Good Evening to you")){
  g.setFont(new Font("Book Antiqua",Font.BOLD,20));
      public void actionPerformed(ActionEvent event) { 
      if (event.getSource() == morningButton) {
         st ="Good Morning to you";
      if (event.getSource() == afternoonButton){ 
         st ="Good Afternoon to you";
    if (event.getSource() == eveningButton) {
         st ="Good Evening to you";


Posted on: October 23, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.