Swing API
3.setDefaultCloseOperation():- It is used when we want to close our application when user click the close button of application.
4.setBounds():- used to set the size and position of application.
5.add():- used to add UI elements on Frame.
Java provides a rich set of libraries to create Graphical user interface. Swing API is a set of extensible GUI components to ease developers life to create Java based Front End /GUI Application.
It is build upon top of AWT(Abstract Window Toolkit) API and acts as replacement of AWI API as its has almost every control corresponding to AWT controls.
Swing is a little weight, offers rich controls and highly customizable.
Basically Swing API is very important in making GUI based application.It have all the methods of AWT(Abstract Window Toolkit) API and it has its own new methods which helps developer to create GUI based application very easily.
In this tutorial we will learn how to use Swing API to create a GUI based application. At last of this tutorial we will create a simple GUI application .
So Let's begin
Swing is a child of AWT API. which means it have all the features of AWT and it has its own features also that why it is better than AWT API for creating desktop application.
In Swing there are numbers of classes which have no. of methods and these methods are used to create a GUI based application. In this tutorial I will tell you the most important classes and methods that helps to create a GUI application and if you want to understand more about Swing API so pls visit the documentation of Java by clicking here.
For using Swing API we must have to import swing package from javax as shown below:-
Now A application is divided into many frames and frame is divided into pannels.For creating a application first we have create a JFrame then we have to add UI elements to JFrame.
Creating JFrame :-
For creating JFrame we have to create object of JFrame class as shown below:-
It make the Frame visible on screen if you pass the "false" then Frame is not visible on screen.
It is build upon top of AWT(Abstract Window Toolkit) API and acts as replacement of AWI API as its has almost every control corresponding to AWT controls.
Swing is a little weight, offers rich controls and highly customizable.
Basically Swing API is very important in making GUI based application.It have all the methods of AWT(Abstract Window Toolkit) API and it has its own new methods which helps developer to create GUI based application very easily.
In this tutorial we will learn how to use Swing API to create a GUI based application. At last of this tutorial we will create a simple GUI application .
So Let's begin
Swing is a child of AWT API. which means it have all the features of AWT and it has its own features also that why it is better than AWT API for creating desktop application.
In Swing there are numbers of classes which have no. of methods and these methods are used to create a GUI based application. In this tutorial I will tell you the most important classes and methods that helps to create a GUI application and if you want to understand more about Swing API so pls visit the documentation of Java by clicking here.
For using Swing API we must have to import swing package from javax as shown below:-
- import javax.swing.*; //importing all classes of swing package
Now A application is divided into many frames and frame is divided into pannels.For creating a application first we have create a JFrame then we have to add UI elements to JFrame.
Creating JFrame :-
For creating JFrame we have to create object of JFrame class as shown below:-
- Syntax:- JFrame frame_name=new JFrame("Title of application ");
- for ex:- JFrame f=new JFrame("game");
now after creating JFrame we have to call some methods of JFrame class for designing of Frame.some of these methods are:-
1.setVisible():- used to set the visibilty of Frame .
- Syntax:- Frame_name.setVisible(arguments)
- For Example:- f.setVisible(true)
It make the Frame visible on screen if you pass the "false" then Frame is not visible on screen.
2.setSize():- It is used to set the size of frame.
- Syntax:- Frame_name.setSize(width,height);
- example:- f.setSize(500,300) ;
3.setDefaultCloseOperation():- It is used when we want to close our application when user click the close button of application.
- Syntax:- Frame_name setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
- ex:- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
4.setBounds():- used to set the size and position of application.
- Syntax:- Frame_name.setBounds("left_margin,top_margin,width,height")
- ex:- f.setBounds(100,500,500,300);
5.add():- used to add UI elements on Frame.
There are many UI elements present in Swing API for creating the display of GUI based application.Some of them are:-
- JButton()
- JLabel()
- JTextField()
- JPanel()
- JPasswordField()
- JCheckBox()
- JRadioButton()
- JList()
Now we have basic knowledge of using Swing API and GUI Based Application.So let's take a example for understanding working of swing:-
In this example we will create a simple GUI Which shows label on screen.
- //importing module
- import javax.swing.*;
- class example
- {
- public static void main(String args[])
- {
- JFrame f=new JFrame("Programming scoop"); //creating frame
- f.setVisible(true); //visiblity of frame
- f.setSize(500,300); //size of frame
- f.setLayout(null); //default layuout null
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //creating label and settin position
- JLabel name=new JLabel("Programming Scoop");
- name.setBounds(150,100,200,30);
- f.add(name); //adding jlabel on frame
- }
- }
Ouput:-
I hope you must have learn from this tutorial. If you have any question about any swing or any other concept of java so tell me in comments box. Share this post to your friends who want to understand Java Concepts.
Follow programmingscoop to learn programming concepts and programs.
0 Comments