1-Java Swing
1-Java Swing
1-Java Swing
Swing in java is part of the Java foundation class which is lightweight and platform-independent.
It is used for creating window-based applications. It includes components like a button, a scroll
bar, text fields, etc. Putting together all these components makes a graphical user interface.
The methods of a Component class that are widely used in java swing are given below.
Method Description
public void add(Component c) add a component on another component.
public void setLayout(LayoutManager m) sets the layout manager for the component.
Container Class
Any class which has other components in it is called a Container class. For building GUI
applications at least one container class is necessary.
Following are the three types of container classes:
• Panel – It is used to organize component onto a window
• Frame – A fully functioning window with icons and titles
• Dialog – It is like a pop-up window but not fully functional like the frame
All the components in swing like JButton, JComboBox, JList, JLabel are inherited from the
JComponent class which can be added to the container classes. Containers are the windows like
frame and dialog boxes. Basic swing components are the building blocks of any GUI application.
Methods like setLayout override the default layout in each container. Containers like JFrame and
JDialog can only add a component to itself.
Sample Program#1
import javax.swing.*;
public class FirstSwingExample {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame
TASK
Write a program to add JLabel, JTextField and JComboBox classes on a Frame and also set its
size, Layout, and visibility as well.