This is a Java program to create 2 frames and switch between the frames using buttons.
We have to write a program in Java such that it creates 2 frames: Frame 1 & Frame 2. Frame 1 consists of two buttons: Next and Close. Frame 2 consists of a Back button. Switching between the frames is done using that buttons.
For switching between frames using buttons, we can have the following sets of input and output.
1. To View Frame 1 :
If the program is being executed, then it is expected that it will create and displays Frame 1 with Next and Close buttons.
2. To Create Frame 2 using Next Button :
If the button Next is clicked on Frame 1, then it is expected that Frame 2 is created.
3. To Close Frame 2 using Back Button :
If the button Back is clicked on Frame 2, then it is expected that Frame 2 is closed and Frame 1 remains activated.
4. To Close Frame 1 using Close Button :
If the button Close is clicked on Frame 1, then it is expected that Frame 1 is closed.
1. Create Frame 1 with buttons Next and Close.
2. Associate ActionListener with the buttons.
3. If button Next is clicked, create Frame 2 with Back button.
4. If button Close is clicked, close Frame 1.
5. If button Back is clicked, close Frame 2.
Here is source code of the Java Program to create 2 Frames and switch between them using buttons. The program is successfully compiled and tested using javac compiler on Fedora 30.
/*Java Program to switch between frames using buttons*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Switch_Frame implements ActionListener
{
static JFrame frame1;
static JFrame frame2;
static JButton next;
static JButton close;
static JButton back;
//Driver function
public static void main(String args[])
{
//Create frame 1
frame1 = new JFrame("Frame 1");
frame1.setSize(250,250);
frame1.setLayout(null);
frame1.setBackground(Color.white);
//Create next and close buttons
next = new JButton("Next");
close = new JButton("Close");
next.setBounds(75,50,100,50);
close.setBounds(75,150,100,50);
//Add the buttons to frame 1
frame1.add(next);
frame1.add(close);
//Create an object
Switch_Frame obj=new Switch_Frame();
//Associate ActionListener with the buttons
next.addActionListener(obj);
close.addActionListener(obj);
//Display frame 1
frame1.setVisible(true);
}
//Function to perform actions related to button clicked
public void actionPerformed(ActionEvent e)
{
String button=e.getActionCommand();
if(button.equals("Next"))
{
create_frame2();
}
if(button.equals("Close"))
{
frame1.dispose();
}
if(button.equals("Back"))
{
frame2.dispose();
}
}
//function to create Frame 2
public static void create_frame2()
{
//Create frame 2
frame2 = new JFrame("Frame 2");
frame2.setSize(250,250);
frame2.setLayout(null);
frame2.setBackground(Color.white);
//Create back button
back = new JButton("Back");
back.setBounds(75,100,100,50);
//Add the button to frame 2
frame2.add(back);
//Create an object
Switch_Frame obj=new Switch_Frame();
//Associate ActionListener with the buttons
back.addActionListener(obj);
//Display frame 2
frame2.setVisible(true);
}
}
1. Create a frame 1 with background color & specific size. frame1.setBackground(Color.white); is used to create the frame background color as white. frame1.setSize(250,250); is used to set the width and height of the frame.
2. Create a next and close buttons with a setBounds() values and add it to the frame.
next = new JButton(“Next”); –> Used to create the next button.
close = new JButton(“Close”); –> Used to create the close button.
next.setBounds(75,50,100,50); –> Sets the position of next button.
close.setBounds(75,150,100,50); –> Sets the position of close button.
frame1.add(next); –> It will add the next button to the frame 1.
frame1.add(close); –> It will add the close button to the frame 1.
3. Switch_Frame obj=new Switch_Frame(); is used to create the object. Once object is created associate ActionListener with the next and close buttons as follows.
next.addActionListener(obj); –> Associates ActionListener with the next button.
close.addActionListener(obj); –> Associates ActionListener with the close button.
4. frame1.setVisible(true); is used to display the frame 1.
5. Create a frame 2 with background color & specific size. frame2.setBackground(Color.white); is used to create the frame background color as white. frame2.setSize(250,250); is used to set the width and height of the frame.
6. Create a next and back button with a setBounds() values and add it to the frame by using frame.add();.
back = new JButton(“Back”); –> Used to create the back button.
back.setBounds(75,100,100,50); –> Sets the position of back button.
frame2.add(back) –> It will add the back button to the frame 2.
7. Switch_Frame obj=new Switch_Frame(); is used to create the object. Once object is created associate ActionListener with the back button.
8. frame2.setVisible(true); is used to display the frame 2.
9. ActionEvent class is used to perform component defined action. In method actionPerformed the following actions will be performed.
i. Initially, Frame 1 is created with Next and Close buttons.
ii. If Next button is clicked on Frame 1, it will create Frame 2 with Back button.
iii. If Back button is clicked on Frame 2, it will close close Frame 2.
iv. If Close button is clicked on Frame 1, it will close Frame 1.
Here’s the run time test case for switching between frames using buttons.
Test case 1 – Here’s the runtime output to view Frame 1. When the program is executed, then it will create and displays Frame 1 with Next and Close buttons.
Test case 2 – Here’s the runtime output to create Frame 2. Suppose if user clicks on Next button on Frame 1,
then it will create Frame 2 with Back button.
Test case 3 – Here’s the runtime output to close Frame 2. Suppose if user clicks on Back button on Frame 2, then it will close the Frame 2 but Frame 1 remains activated.
Sanfoundry Global Education & Learning Series – Java Programs.
- Apply for Computer Science Internship
- Practice Information Technology MCQs
- Practice BCA MCQs
- Check Programming Books
- Check Java Books