Java Program to Create 2 Frames and Switch Between the Frames using Buttons

«
»

This is a Java program to create 2 frames and switch between the frames using buttons.

Problem Description

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.

Expected Input and Output

For switching between frames using buttons, we can have the following sets of input and output.

advertisement

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 :

advertisement
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.
Problem Solution

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.

advertisement
Program/Source Code

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.

  1. /*Java Program to switch between frames using buttons*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. class Switch_Frame implements ActionListener
  6. {
  7. 	static JFrame frame1;
  8. 	static JFrame frame2;
  9. 	static JButton next;
  10. 	static JButton close;
  11. 	static JButton back;
  12. 	//Driver function
  13. 	public static void main(String args[])
  14. 	{
  15. 		//Create frame 1
  16. 		frame1 = new JFrame("Frame 1");
  17. 		frame1.setSize(250,250);
  18. 		frame1.setLayout(null);
  19. 		frame1.setBackground(Color.white);
  20. 		//Create next and close buttons
  21. 		next = new JButton("Next"); 
  22. 		close = new JButton("Close");
  23. 		next.setBounds(75,50,100,50);
  24. 		close.setBounds(75,150,100,50);
  25. 		//Add the buttons to frame 1
  26. 		frame1.add(next);
  27. 		frame1.add(close);
  28. 		//Create an object
  29. 		Switch_Frame obj=new Switch_Frame();
  30. 		//Associate ActionListener with the buttons
  31. 		next.addActionListener(obj);
  32. 		close.addActionListener(obj);
  33. 		//Display frame 1
  34. 		frame1.setVisible(true);
  35. 	}
  36. 	//Function to perform actions related to button clicked
  37. 	public void actionPerformed(ActionEvent e)
  38. 	{
  39. 		String button=e.getActionCommand();
  40. 		if(button.equals("Next"))
  41. 		{
  42. 			create_frame2();
  43. 		}
  44. 		if(button.equals("Close"))
  45. 		{
  46. 			frame1.dispose();
  47. 		}
  48. 		if(button.equals("Back"))
  49. 		{
  50. 			frame2.dispose();
  51. 		}
  52. 	}
  53. 	//function to create Frame 2
  54. 	public static void create_frame2()
  55. 	{
  56. 		//Create frame 2
  57. 		frame2 = new JFrame("Frame 2");
  58. 		frame2.setSize(250,250);
  59. 		frame2.setLayout(null);
  60. 		frame2.setBackground(Color.white);
  61. 		//Create back button
  62. 		back = new JButton("Back"); 
  63. 		back.setBounds(75,100,100,50);
  64. 		//Add the button to frame 2
  65. 		frame2.add(back);
  66. 		//Create an object
  67. 		Switch_Frame obj=new Switch_Frame();
  68. 		//Associate ActionListener with the buttons
  69. 		back.addActionListener(obj);
  70. 		//Display frame 2
  71. 		frame2.setVisible(true);
  72. 	}
  73. }
Program Explanation

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.

advertisement

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.

Runtime Test Cases

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.
java-program-switch-frame-create-frame1

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.
java-program-switch-frame-next

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.
java-program-switch-frame-back

Sanfoundry Global Education & Learning Series – Java Programs.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn