
Write a java program that displays multiple frames : Step 1: Design a frame with three buttons: ?Fruit?, ?Bird? and ?Animal? Step 2: On clicking ?Fruit? button display another frame that shows a picture of a fruit of your interest. On clicking ?Bird? button display another frame that shows a picture of a bird of your interest. On clicking ?Animal? button display another frame that shows a picture of a animal of your interest. Have icons for your buttons

Here is an example that displays three buttons and display another frame when they are clicked.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MultipleFrame{
public static void main(String[] args){
JFrame f = new JFrame("");
f.setLayout(new FlowLayout());
JButton button1=new JButton("Fruit");
Icon icon1 = new ImageIcon("c:/fruit.png");
JButton button2=new JButton("Bird");
Icon icon2 = new ImageIcon("c:/bird.png");
JButton button3=new JButton("Animal");
Icon icon3 = new ImageIcon("c:/animal.png");
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFrame f1=new JFrame("Mango");
Icon icon = new ImageIcon("c:/mango.jpg");
JLabel lab=new JLabel();
lab.setIcon(icon);
f1.add(lab);
f1.setVisible(true);
f1.setSize(300,300);
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFrame f1=new JFrame("Peacock");
Icon icon = new ImageIcon("c:/peacock.jpg");
JLabel lab=new JLabel();
lab.setIcon(icon);
f1.add(lab);
f1.setVisible(true);
f1.setSize(500,300);
}
});
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFrame f1=new JFrame("Giraffe");
Icon icon = new ImageIcon("c:/giraffe.jpg");
JLabel lab=new JLabel();
lab.setIcon(icon);
f1.add(lab);
f1.setVisible(true);
f1.setSize(300,300);
}
});
f.add(button1);
f.add(button2);
f.add(button3);
f.setSize(300,150);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

In the previous code, we forget to add an icon to button. So we are sending the updated code.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MultipleFrame{
public static void main(String[] args){
JFrame f = new JFrame("");
f.setLayout(new FlowLayout());
JButton button1=new JButton("Fruit");
Icon icon1 = new ImageIcon("c:/fruit.png");
button1.setIcon(icon1);
JButton button2=new JButton("Bird");
Icon icon2 = new ImageIcon("c:/bird.png");
button2.setIcon(icon2);
JButton button3=new JButton("Animal");
Icon icon3 = new ImageIcon("c:/animal.png");
button3.setIcon(icon3);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFrame f1=new JFrame("Mango");
Icon icon = new ImageIcon("c:/mango.jpg");
JLabel lab=new JLabel();
lab.setIcon(icon);
f1.add(lab);
f1.setVisible(true);
f1.setSize(300,300);
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFrame f1=new JFrame("Peacock");
Icon icon = new ImageIcon("c:/peacock.jpg");
JLabel lab=new JLabel();
lab.setIcon(icon);
f1.add(lab);
f1.setVisible(true);
f1.setSize(500,300);
}
});
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFrame f1=new JFrame("Giraffe");
Icon icon = new ImageIcon("c:/giraffe.jpg");
JLabel lab=new JLabel();
lab.setIcon(icon);
f1.add(lab);
f1.setVisible(true);
f1.setSize(300,300);
}
});
f.add(button1);
f.add(button2);
f.add(button3);
f.setSize(300,150);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.