Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

1-Java Swing

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

JAVA SWING

OBJECTIVE: To study how to create GUI using 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.

Commonly used Methods of the Component class

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 setSize(int width,int height) sets size of the component.

public void setLayout(LayoutManager m) sets the layout manager for the component.

public void setVisible(boolean b) sets the visibility of the component. It is by


default false.

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.

Simple Swing Example


Let's see a simple swing example where we are creating one button and adding it on the JFrame
object inside the main() method.

Sample Program#1
import javax.swing.*;
public class FirstSwingExample {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame

JButton b=new JButton("click");//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,500);//400 width and 500 height


f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}
}

Simple Swing Example by Inheritance


We can also inherit the JFrame class, so there is no need to create the instance of JFrame class
explicitly.
Sample Program #2
import javax.swing.*;
public class Simple2 extends JFrame{//inheriting JFrame
JFrame f;
Simple2(){
JButton b=new JButton("click");//create button
b.setBounds(130,100,100, 40);

add(b);//adding button on frame


setSize(400,500);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new Simple2();
}
}

TASK
Write a program to add JLabel, JTextField and JComboBox classes on a Frame and also set its
size, Layout, and visibility as well.

You might also like