In this tutorial we will learn to create multiple frames in java. Here we will be creating two classes for example OldWindow and NewWindow class respectively. OldWindow class will consist the actual logic for frame creation while the NewWindow class will have the design structure of JFrame .

  • So, to learn to create multiple frames in java follow the steps given below :


    1. First of all open up eclipse -> Go to File-> New-> Java Project, just select the Java Project as shown below :


    1. After selecting Java Projet a dialog box will pop up asking for project details. So, fill the respective details and click on Finish button.
creating new class
    1. Now, you can view your project and select it from the package explorer present at the left hand side.
Project creation
    1. Now let’s create two new java classes called as OldWindow and NewWindow respectively. So, for generating these class, select your project -> right click on src -> New -> Class as shown below :
old class
    1. Now insert the respective class details :
Old Class
    1. Similarly, create NewWindow class and now you will be having your both the classes generated in your project in the package explorer.

class created

    1. Now open up class and write the actual logic code, which will create JFrame, JButton and open up a New Frame to display.


    1. Insert the given code in class:



    1. Just follow the comments to understand the code.


    1. Now, open your class and add the given code which creates JFrame, JLabel into the New Frame.



    1. We are all done. Now, just select your project from the package explorer and run it.


    1. You will have the following output:
    1. After clicking the new button you will be navigated to new window as shown below:
new window

Thus, we have successfully learnt to create multiple frames in java and navigated from one frame to another.

Learn to create multiple frames in java was last modified: by