Swing is a part of JFC (Java Foundation Classes). Building Graphical User Interface in Java requires the use of Swings. Swing Framework contain a large set of components which allow high level of customization and provide rich functionalities, and is used to create window based applications.
Java swing components are lightweight, platform independent, provide powerful components like tables, scroll panels, buttons, list, color chooser, etc.
In this article, we’ll see how to make frames using Swings in Java. Ways to create a frame:
- By creating the object of Frame class (association): In this, we will see how to create JFrame window by instantiating JFrame class.
// Java program to create frames// using associationimportjavax.swing.*;publicclasstest1{JFrame frame;test1(){// creating instance of JFrame with name "first way"frame=newJFrame("first way");// creates instance of JButtonJButton button =newJButton("let's see");button.setBounds(200,150,90,50);// setting close operationframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// adds button in JFrameframe.add(button);// sets 500 width and 600 heightframe.setSize(500,600);// uses no layout managersframe.setLayout(null);// makes the frame visibleframe.setVisible(true);}publicstaticvoidmain(String[] args){newtest1();}}chevron_rightfilter_none - By extending Frame class (inheritance): In this example, we will be inheriting JFrame class to create JFrame window and hence it won’t be required to create an instance of JFrame class explicitly.
// Java program to create a// frame using inheritance().importjavax.swing.*;// inheriting JFramepublicclasstest2extendsJFrame{JFrame frame;test2(){setTitle("this is also a title");// create buttonJButton button =newJButton("click");button.setBounds(165,135,115,55);// adding button on frameadd(button);// setting close operationsetDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(400,500);setLayout(null);setVisible(true);}publicstaticvoidmain(String[] args){newtest2();}}chevron_rightfilter_noneOutput :

Note : You won’t be able to run this code on an online compiler, so I have added an image to show you the output. - Create a frame using Swing inside main():
// Java program to create a frame// using Swings in main().importjavax.swing.*;publicclassSwing_example{publicstaticvoidmain(String[] args){// creates instance of JFrameJFrame frame1 =newJFrame();// creates instance of JButtonJButton button1 =newJButton("click");JButton button2 =newJButton("again click");// x axis, y axis, width, heightbutton1.setBounds(160,150,80,80);button2.setBounds(190,190,100,200);// adds button1 in Frame1frame1.add(button1);// adds button2 in Frame1frame1.add(button2);// 400 width and 500 height of frame1frame1.setSize(400,500) ;// uses no layout managersframe1.setLayout(null);// makes the frame visibleframe1.setVisible(true);}}chevron_rightfilter_noneOutput :

Note : You wont’t be able to run this code on online compiler, so I have added an image to show you the output.
Another example:// Java program to create a frame// using Swings in main().importjavax.swing.*;publicclassSwing_example_2{publicstaticvoidmain(String[] args){// creates instance of JFrameJFrame frame1 =newJFrame();// creates instance of JButtonJButton button1 =newJButton("button1");// "button2" appears on the buttonJButton button2 =newJButton("button2");// x axis, y axis, width, heightbutton1.setBounds(180,50,80,80);button2.setBounds(180,140,80,80);//adds button1 in Frame1frame1.add(button1);//adds button2 in Frame1frame1.add(button2);//400 width and 500 height of frame1frame1.setSize(500,300) ;//uses no layout managersframe1.setLayout(null);//makes the frame visibleframe1.setVisible(true);}}chevron_rightfilter_noneOutput :

Note : You wont’t be able to run this code on online compiler, so I have added an image to show you the output.
This article is contributed by Shitij Chawla. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

