articles

Java Swing

Anupam Mishra5196 08-Dec-2015

If you are excited to coding for creating a windows based application then Java Swing is best way for creating desktop development applications.

Java Swing is used to create window-based applications. Swing is a part of Java Foundation Classes (JFC). It is entirely written in java.

Unlike AWT, Java Swing provides platform-independent and lightweight components.

AWT provides less components than Swing. AWT doesn't follows MVC (Model View Controller) where model represents data, view represents presentation and controller acts as an interface 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

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


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


Updated 07-Sep-2019

Leave Comment

Comments

Liked By