top

Search

Java Tutorial

Swing Framework includes a number of classes that provide GUI components that are more strong and versatile than AWT. Swing is an official Java GUI tool kit published by Sun Microsystems that offers the look and feel of contemporary Java GUI. It is used to build Java's graphical user interface. In javax.swing package and its sub-packages, swing classes are described. Main Features of Swing Toolkit Platform Independent Customizable Extensible Configurable Lightweight Rich Controls Pluggable Look and FeelSwing and JFC JFC is an abbreviation for Java Foundation classes that includes a set of characteristics to build Graphical User Interfaces (GUI) and add wealthy graphical characteristics and interactivity to Java apps. Java Swing belongs to the Java Classes Foundation (JFC). Features of JFC Swing GUI components. Look and Feel support. Java 2D. AWT and Swing HierarchyIntroduction to Swing Classes: JPanel :  JPanel is Swing's version of AWT class Panel and uses the same default layout, FlowLayout. JPanel is descended directly from JComponent. JFrame :  JFrame is Swing's version of Frame and is descended directly from Frame class. The component which is added to the Frame, is referred to as its Content. JWindow :  This is Swing's version of Window and has descended directly from Window class. Like Window it uses BorderLayout by default. JLabel :  JLabel has descended from JComponent, and is used to create text labels. JButton :  JButton class provides the functioning of push button. JButton allows an icon, string or both associated with a button. JTextField :  JTextFields allow editing of a single line of text. Creating a JFrame in java There are 2 ways we can create a JFrame Window. By instantiating JFrame class. By extending JFrame class. Creating JFrame window by Instantiating JFrame class import javax.swing.*;  //importing swing package  import java.awt.*;     //importing awt package  public class First  {  JFrame jf;  public First() {  jf = new JFrame("MyWindow");//Creating a JFrame with name MyWindow  JButton btn = new JButton("Say Hello");//Creating a Button named Say Hello  jf.add(btn);             //adding button to frame  jf.setLayout(new FlowLayout());//setting layout using FlowLayout object  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//setting close  operation.  jf.setSize(400, 400);            //setting size  jf.setVisible(true);            //setting frame visibility  }  public static void main(String[] args)  {  new First();  }  } Creating JFrame window by extending JFrame class in javaimport javax.swing.*; //importing swing package  import java.awt.*; //importing awt package  public class Second extends JFrame  {  public Second()  {  setTitle("MyWindow"); //setting title of frame as  MyWindow  JLabel lb = new JLabel("Welcome to My Second Window");//Creating a label named Welcome to My Second Window  add(lb);//adding label to frame.  setLayout(new FlowLayout());//setting layout using FlowLayout object.  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting close operation.  setSize(400, 400);//setting size  setVisible(true);//setting frame visibility  }  public static void main(String[] args)  {  new Second();  }  } Points to note: Import the javax.swing and java.awt package to use the classes and methods of Swing. While creating a frame (either by instantiating or extending Frame class), the following two attributes are a must for visibility of the frame: setSize(int width, int height);           setVisible(true); When you create objects of other components like Buttons, TextFields, etc. Then you need to add it to the frame by using the method - add(Component's Object); You can add the following method also for resizing the frame - setResizable(true); 
logo

Java Tutorial

Java Swings

Swing Framework includes a number of classes that provide GUI components that are more strong and versatile than AWT. Swing is an official Java GUI tool kit published by Sun Microsystems that offers the look and feel of contemporary Java GUI. It is used to build Java's graphical user interface. 

In javax.swing package and its sub-packages, swing classes are described. 

Main Features of Swing Toolkit 

  • Platform Independent 
  • Customizable 
  • Extensible 
  • Configurable 
  • Lightweight 
  • Rich Controls 
  • Pluggable Look and Feel

Swing and JFC 

JFC is an abbreviation for Java Foundation classes that includes a set of characteristics to build Graphical User Interfaces (GUI) and add wealthy graphical characteristics and interactivity to Java apps. Java Swing belongs to the Java Classes Foundation (JFC). 

Features of JFC 

  • Swing GUI components. 
  • Look and Feel support. 
  • Java 2D. 

AWT and Swing HierarchyAWT and Swing Hierarchy

Introduction to Swing Classes: 

  • JPanel :  JPanel is Swing's version of AWT class Panel and uses the same default layout, FlowLayout. JPanel is descended directly from JComponent. 
  • JFrame :  JFrame is Swing's version of Frame and is descended directly from Frame class. The component which is added to the Frame, is referred to as its Content.
  • JWindow :  This is Swing's version of Window and has descended directly from Window class. Like Window it uses BorderLayout by default.
  • JLabel :  JLabel has descended from JComponent, and is used to create text labels. 
  • JButton :  JButton class provides the functioning of push button. JButton allows an icon, string or both associated with a button. 
  • JTextField :  JTextFields allow editing of a single line of text. 

Creating a JFrame in java 

There are 2 ways we can create a JFrame Window. 

  • By instantiating JFrame class. 
  • By extending JFrame class. 

Creating JFrame window by Instantiating JFrame class 

import javax.swing.*;  //importing swing package 
import java.awt.*;     //importing awt package 
public class First 
{ 
JFrame jf; 
public First() { 
jf = new JFrame("MyWindow");//Creating a JFrame with name MyWindow 
JButton btn = new JButton("Say Hello");//Creating a Button named Say Hello 
jf.add(btn);             //adding button to frame 
jf.setLayout(new FlowLayout());//setting layout using FlowLayout object 
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//setting close  operation. 
jf.setSize(400, 400);            //setting size 
jf.setVisible(true);            //setting frame visibility 
} 
public static void main(String[] args) 
{ 
new First(); 
} 
} 

 Instantiating JFrame class

Creating JFrame window by extending JFrame class in java

import javax.swing.*; //importing swing package 
import java.awt.*; //importing awt package 
public class Second extends JFrame 
{ 
public Second() 
{ 
setTitle("MyWindow"); //setting title of frame as  MyWindow 
JLabel lb = new JLabel("Welcome to My Second Window");//Creating a label named Welcome to My Second Window 
add(lb);//adding label to frame. 
setLayout(new FlowLayout());//setting layout using FlowLayout object. 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting close operation. 
setSize(400, 400);//setting size 
setVisible(true);//setting frame visibility 
} 
public static void main(String[] args) 
{ 
new Second(); 
} 
} 

 JFrame class in java

Points to note: 

  • Import the javax.swing and java.awt package to use the classes and methods of Swing. 
  • While creating a frame (either by instantiating or extending Frame class), the following two attributes are a must for visibility of the frame: 
setSize(int width, int height); 
          setVisible(true); 
  • When you create objects of other components like Buttons, TextFields, etc. Then you need to add it to the frame by using the method - add(Component's Object); 
  • You can add the following method also for resizing the frame - setResizable(true); 

Leave a Reply

Your email address will not be published. Required fields are marked *