Home Tutorial Java Swing Create an JApplet with three Jbuttons

 
 

Share on Google+Share on Google+
Create an JApplet with three Jbuttons
Posted on: October 23, 2009 at 12:00 AM
Advertisement
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")
      add(morningButton)
      morningButton.addActionListener(this)
      afternoonButton = new Button("Afternoon")
      add(afternoonButton)
      afternoonButton.addActionListener(this)
    eveningButton = new Button("Evening")
      add(eveningButton)
      eveningButton.addActionListener(this)
   }
      public void paint(Graphics g) {
      if(st.equals("Good Morning to you")){
  g.setColor(Color.red);
  g.setFont(new Font("Helvetica",Font.BOLD,20));
      }
      if(st.equals("Good Afternoon to you")){
  g.setColor(Color.blue);
  g.setFont(new Font("Arial",Font.BOLD,20));
      }
      if(st.equals("Good Evening to you")){
  g.setColor(Color.green);
  g.setFont(new Font("Book Antiqua",Font.BOLD,20));
      }
  g.drawString(st,100,200) 
   }
      public void actionPerformed(ActionEvent event) { 
      if (event.getSource() == morningButton) {
         st ="Good Morning to you";
     repaint();
    }
      if (event.getSource() == afternoonButton){ 
         st ="Good Afternoon to you";
         repaint();
    }
    if (event.getSource() == eveningButton) {
         st ="Good Evening to you";
         repaint()
    }
   
Advertisement

Related Tags for Create an JApplet with three Jbuttons:


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.

Posted on: October 23, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com