/* This program create a Java Frame with 5 rows of button with captioned image on each button
When user clicks on any of the button it shows the name of image that is used Label for Title of
the Java Frame is created as My Translator
Event handling is used with the help of actionlistner interface */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Demo extends JFrame implements ActionListener
{
JFrame f;
JButton btn1,btn2,btn3,btn4,btn5;
Demo(){
f=new JFrame("My Translator"); //creating frame object
btn1=new JButton(new ImageIcon("rsc//a.jpg")); //creating button with image as caption
btn2=new JButton(new ImageIcon("rsc//b.jpg"));
btn3=new JButton(new ImageIcon("rsc//c.jpg"));
btn4=new JButton(new ImageIcon("rsc//d.jpg"));
btn5=new JButton(new ImageIcon("rsc//e.jpg"));
//adding button to actionlistener for even handling
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
//adding button to frame
f.add(btn1);f.add(btn2);f.add(btn3);f.add(btn4);f.add(btn5);
f.setLayout(new GridLayout(5,1));
//setting grid layout of 5 rows and 1 columns
f.setSize(300,300);
f.setVisible(true);
//Close the execution of Java Program if user close the screen
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
//managing event of click on button when a user click on any button it show image name that
is used to show on button
if(ae.getSource()==btn1)
JOptionPane.showMessageDialog( null,"A.JPG" );
if(ae.getSource()==btn2)
JOptionPane.showMessageDialog( null,"B.JPG" );
if(ae.getSource()==btn3)
JOptionPane.showMessageDialog( null,"C.JPG" );
if(ae.getSource()==btn4)
JOptionPane.showMessageDialog( null,"D.JPG" );
if(ae.getSource()==btn5)
JOptionPane.showMessageDialog( null,"E.JPG" );
}
public static void main(String[] args) {
new Demo();
}
}
Solution
/* This program create a Java Frame with 5 rows of button with captioned image on each button
When user clicks on any of the button it shows the name of image that is used Label for Title of
the Java Frame is created as My Translator
Event handling is used with the help of actionlistner interface */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Demo extends JFrame implements ActionListener
{
JFrame f;
JButton btn1,btn2,btn3,btn4,btn5;
Demo(){
f=new JFrame("My Translator"); //creating frame object
btn1=new JButton(new ImageIcon("rsc//a.jpg")); //creating button with image as caption
btn2=new JButton(new ImageIcon("rsc//b.jpg"));
btn3=new JButton(new ImageIcon("rsc//c.jpg"));
btn4=new JButton(new ImageIcon("rsc//d.jpg"));
btn5=new JButton(new ImageIcon("rsc//e.jpg"));
//adding button to actionlistener for even handling
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
//adding button to frame
f.add(btn1);f.add(btn2);f.add(btn3);f.add(btn4);f.add(btn5);
f.setLayout(new GridLayout(5,1));
//setting grid layout of 5 rows and 1 columns
f.setSize(300,300);
f.setVisible(true);
//Close the execution of Java Program if user close the screen
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
//managing event of click on button when a user click on any button it show image name that
is used to show on button
if(ae.getSource()==btn1)
JOptionPane.showMessageDialog( null,"A.JPG" );
if(ae.getSource()==btn2)
JOptionPane.showMessageDialog( null,"B.JPG" );
if(ae.getSource()==btn3)
JOptionPane.showMessageDialog( null,"C.JPG" );
if(ae.getSource()==btn4)
JOptionPane.showMessageDialog( null,"D.JPG" );
if(ae.getSource()==btn5)
JOptionPane.showMessageDialog( null,"E.JPG" );
}
public static void main(String[] args) {
new Demo();
}
}

This program create a Java Frame with 5 rows of button with capti.pdf

  • 1.
    /* This programcreate a Java Frame with 5 rows of button with captioned image on each button When user clicks on any of the button it shows the name of image that is used Label for Title of the Java Frame is created as My Translator Event handling is used with the help of actionlistner interface */ import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Demo extends JFrame implements ActionListener { JFrame f; JButton btn1,btn2,btn3,btn4,btn5; Demo(){ f=new JFrame("My Translator"); //creating frame object btn1=new JButton(new ImageIcon("rsc//a.jpg")); //creating button with image as caption btn2=new JButton(new ImageIcon("rsc//b.jpg")); btn3=new JButton(new ImageIcon("rsc//c.jpg")); btn4=new JButton(new ImageIcon("rsc//d.jpg")); btn5=new JButton(new ImageIcon("rsc//e.jpg")); //adding button to actionlistener for even handling btn1.addActionListener(this); btn2.addActionListener(this); btn3.addActionListener(this); btn4.addActionListener(this); btn5.addActionListener(this); //adding button to frame f.add(btn1);f.add(btn2);f.add(btn3);f.add(btn4);f.add(btn5); f.setLayout(new GridLayout(5,1)); //setting grid layout of 5 rows and 1 columns
  • 2.
    f.setSize(300,300); f.setVisible(true); //Close the executionof Java Program if user close the screen f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent ae) { //managing event of click on button when a user click on any button it show image name that is used to show on button if(ae.getSource()==btn1) JOptionPane.showMessageDialog( null,"A.JPG" ); if(ae.getSource()==btn2) JOptionPane.showMessageDialog( null,"B.JPG" ); if(ae.getSource()==btn3) JOptionPane.showMessageDialog( null,"C.JPG" ); if(ae.getSource()==btn4) JOptionPane.showMessageDialog( null,"D.JPG" ); if(ae.getSource()==btn5) JOptionPane.showMessageDialog( null,"E.JPG" ); } public static void main(String[] args) { new Demo(); } } Solution /* This program create a Java Frame with 5 rows of button with captioned image on each button When user clicks on any of the button it shows the name of image that is used Label for Title of the Java Frame is created as My Translator Event handling is used with the help of actionlistner interface */ import java.awt.*; import javax.swing.*; import java.awt.event.*;
  • 3.
    public class Demoextends JFrame implements ActionListener { JFrame f; JButton btn1,btn2,btn3,btn4,btn5; Demo(){ f=new JFrame("My Translator"); //creating frame object btn1=new JButton(new ImageIcon("rsc//a.jpg")); //creating button with image as caption btn2=new JButton(new ImageIcon("rsc//b.jpg")); btn3=new JButton(new ImageIcon("rsc//c.jpg")); btn4=new JButton(new ImageIcon("rsc//d.jpg")); btn5=new JButton(new ImageIcon("rsc//e.jpg")); //adding button to actionlistener for even handling btn1.addActionListener(this); btn2.addActionListener(this); btn3.addActionListener(this); btn4.addActionListener(this); btn5.addActionListener(this); //adding button to frame f.add(btn1);f.add(btn2);f.add(btn3);f.add(btn4);f.add(btn5); f.setLayout(new GridLayout(5,1)); //setting grid layout of 5 rows and 1 columns f.setSize(300,300); f.setVisible(true); //Close the execution of Java Program if user close the screen f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent ae) { //managing event of click on button when a user click on any button it show image name that is used to show on button if(ae.getSource()==btn1)
  • 4.
    JOptionPane.showMessageDialog( null,"A.JPG" ); if(ae.getSource()==btn2) JOptionPane.showMessageDialog(null,"B.JPG" ); if(ae.getSource()==btn3) JOptionPane.showMessageDialog( null,"C.JPG" ); if(ae.getSource()==btn4) JOptionPane.showMessageDialog( null,"D.JPG" ); if(ae.getSource()==btn5) JOptionPane.showMessageDialog( null,"E.JPG" ); } public static void main(String[] args) { new Demo(); } }