---
title: "Java Swing"  
description: "If you are excited to coding for creating a windows based application then Java Swing is the best way ."  
author: "Anupam Mishra"  
published: 2015-12-08  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/11936/java-swing  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Java Swing

If you are excited to coding for creating a windows based [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) then Java Swing is best way for creating desktop [development](https://yourviews.mindstick.com/view/83465/china-s-development-near-indian-border-and-how-it-is-a-threat-to-india) applications.

Java Swing is used to create window-based applications. Swing is a part of Java [Foundation](https://www.mindstick.com/blog/159/mfc-microsoft-foundation-class) Classes (JFC). It is entirely written in java.

Unlike AWT, Java Swing provides platform-[independent](https://answers.mindstick.com/qa/98599/what-is-the-longest-river-in-the-commonwealth-of-independent-states) and lightweight [components](https://www.mindstick.com/blog/74096/understanding-the-role-of-some-integral-ac-components).

AWT provides less components than Swing. AWT doesn't follows MVC (Model [View Controller](https://www.mindstick.com/forum/33709/how-to-use-table-view-controller-with-small-sized-in-ios)) where model represents data, view represents presentation and controller [acts as](https://answers.mindstick.com/qa/51373/what-were-president-johnson-s-first-official-acts-as-president) an [interface](https://www.mindstick.com/articles/12036/interface-in-c-sharp) between model and view. Swing is follows MVC.

The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.

Java Foundation Classes (JFC) are a set of GUI components which simplify the development of desktop applications.

##### The hierarchy of java swing API is given below:

\

![Java Swing](https://www.mindstick.com/mindstickarticle/29d78a7f-c2a7-4eab-8d66-37b8f8440ad5/images/935693d6-25fd-4c96-b13f-3de8ce7f5f9e.png)\

##### There are two ways to create a Frame:

1.By creating the object of Frame class

2.By extending Frame class

Program for creating the object of Frame class is:

```
import javax.swing.*;
public class
UsingJframeObject {
JFrame f;
UsingJframeObject(){
f=new JFrame(" By
Creating Object of JFrame");//creating instance of JFrame

JButton b=new
JButton("click Me!");//creating instance of JButton
b.setBounds(130,100,100, 40); //Set Position

f.add(b);//adding button in
JFrame

f.setSize(350,350);//500 width and 500 height
f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}
public static void main(String[] args) {
new UsingJframeObject();
}
}  
```

##### Output:

> \
>
> ## \
>
> ![Java Swing](https://www.mindstick.com/mindstickarticle/29d78a7f-c2a7-4eab-8d66-37b8f8440ad5/images/7a8fadca-a71b-41cc-a1e3-6f4d2c59eb34.png)

\

##### Program for extending Frame class:

> ```
> import javax.swing.*;  public class
> UsingExtendInJframe extends JFrame{//inheriting JFrame   JFrame f;  UsingExtendInJframe("Frame By Extending JFrame"){ JButton b=new
> JButton("click Me!");//create button
> b.setBounds(130,100,100,
> 40);            add(b);//adding button on frame  setSize(350,350);  setLayout(null);  setVisible(true);  }  public static void main(String[] args) {  new UsingExtendInJframe();  }}
> ```
>
> ##### Output:
>
> ## \
>
> ![Java Swing](https://www.mindstick.com/mindstickarticle/29d78a7f-c2a7-4eab-8d66-37b8f8440ad5/images/8b57a45c-a8d1-41e5-8480-9e2bf2b673a2.png)

---

Original Source: https://www.mindstick.com/articles/11936/java-swing

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
