Creating Frames using Swings in Java
Last Updated :
17 Feb, 2022
Swing is a part of JFC (Java Foundation Classes). Building Graphical User Interface in Java requires the use of Swings. Swing Framework contains a large set of components that allow a 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, lists, color chooser, etc. In this article, we’ll see how to make frames using Swings in Java. Ways to create a frame:
Methods:
- By creating the object of Frame class (association)
- By extending Frame class (inheritance)
- Create a frame using Swing inside main()
Way 1: By creating the object of Frame class (association)
In this, we will see how to create a JFrame window by instantiating the JFrame class.
Example:
Java
import javax.swing.*;
public class test1
{
JFrame frame;
test1()
{
frame= new JFrame( "first way" );
JButton button = new JButton( "let's see" );
button.setBounds( 200 , 150 , 90 , 50 );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(button);
frame.setSize( 500 , 600 );
frame.setLayout( null );
frame.setVisible( true );
}
public static void main(String[] args)
{
new test1();
}
}
|
Way 2: 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.
Example:
Java
import javax.swing.*;
public class test2 extends JFrame
{
JFrame frame;
test2()
{
setTitle( "this is also a title" );
JButton button = new JButton( "click" );
button.setBounds( 165 , 135 , 115 , 55 );
add(button);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize( 400 , 500 );
setLayout( null );
setVisible( true );
}
public static void main(String[] args)
{
new test2();
}
}
|
Output:
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.
Way 3: Create a frame using Swing inside main()
Example 1:
Java
import javax.swing.*;
public class Swing_example
{
public static void main(String[] args)
{
JFrame frame1 = new JFrame();
JButton button1 = new JButton( "click" );
JButton button2 = new JButton( "again click" );
button1.setBounds( 160 , 150 , 80 , 80 );
button2.setBounds( 190 , 190 , 100 , 200 );
frame1.add(button1);
frame1.add(button2);
frame1.setSize( 400 , 500 ) ;
frame1.setLayout( null );
frame1.setVisible( true );
}
}
|
Output:
Note: You won’t be able to run this code on the online compiler, so I have added an image to show you the output.
Example 2:
Java
import javax.swing.*;
public class Swing_example_2
{
public static void main(String[] args)
{
JFrame frame1 = new JFrame();
JButton button1 = new JButton( "button1" );
JButton button2 = new JButton( "button2" );
button1.setBounds( 180 , 50 , 80 , 80 );
button2.setBounds( 180 , 140 , 80 , 80 );
frame1.add(button1);
frame1.add(button2);
frame1.setSize( 500 , 300 ) ;
frame1.setLayout( null );
frame1.setVisible( true );
}
}
|
Output:
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...