Java Swing Tutorial:
JAVA provides a rich set of libraries to create Graphical User Interface in a platform independent way. In this tutorial, we'll look at SWING GUI controls.
Java Swing tutorial is a part of Java Foundation Classes (JFC) that is used to create window-based applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely written in java.
Swing API is a set of extensible GUI Components to ease the developer's life to create JAVA based Front End/GUI Applications. It is build on top of AWT API and acts as a replacement of AWT API, since it has almost every control corresponding to AWT controls. Swing component follows a Model-View-Controller architecture to fulfill the following basis.
- A single API is to be sufficient to support multiple look and feel.
- API is to be model driven so that the highest level API is not required to have data.
- API is to use the Java Bean model so that Builder Tools and IDE can provide better services to the developers for use.
Java Swing provides platform-independent and lightweight components.
Hierarchy of Java Swing classes
Difference between AWT and Swing:
Java Swing Examples:
There are two ways to create a frame:
- By creating the object of Frame class (association)
- By extending Frame class (inheritance)
Simple Java Swing Example:-
import javax.swing.*;
public class SwingExample {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame
JButton b=new JButton("Press");//creating instance of JButton
b.setBounds(130,100,100, 40);//x axis, y axis, width, height
f.add(b);//adding button in JFrame
f.setSize(400,400);//400 width and 400 height
f.setLayout(null);
f.setVisible(true);
}
}
JButton – A button is an instance of JButton class. In the above example we have a button " Press".
Recommanded