0% found this document useful (0 votes)
9 views11 pages

Understanding Swing in Java GUI

Swing is a lightweight GUI toolkit in Java that provides various widgets for building window-based applications. It includes components like JButton, JTextField, JScrollBar, JPanel, JMenu, JList, JLabel, and JComboBox, each serving specific functions in GUI design. Additionally, layout managers such as BorderLayout, FlowLayout, and GridBagLayout are used to arrange components within containers.

Uploaded by

ahmedkhwaja193
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views11 pages

Understanding Swing in Java GUI

Swing is a lightweight GUI toolkit in Java that provides various widgets for building window-based applications. It includes components like JButton, JTextField, JScrollBar, JPanel, JMenu, JList, JLabel, and JComboBox, each serving specific functions in GUI design. Additionally, layout managers such as BorderLayout, FlowLayout, and GridBagLayout are used to arrange components within containers.

Uploaded by

ahmedkhwaja193
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

What is Swing?

Swing in Java is a lightweight GUI toolkit which has a wide variety of widgets for
building optimized window based applications.

It becomes easier to build applications since we already have GUI components like
button, checkbox etc. This is helpful because we do not have to start from the scratch.

JButton Class
It is used to create a labelled button.

Example:

1 import [Link].*;
2
3 public class example{
4 public static void main(String args[]) {
5 JFrame a = new JFrame("example");
6 JButton b = new JButton("click me");
[Link](40,90,85,20);
7 [Link](b);
8 [Link](300,300);
9 [Link](null);
10 [Link](true);
}
11 }
12

Output:
JTextField Class

1
import [Link].*;
2
3 public class example{
4 public static void main(String args[]) {
5 JFrame a = new JFrame("example");
JTextField b = new JTextField("edureka");
6 [Link](50,100,200,30);
7 [Link](b);
8 [Link](300,300);
9 [Link](null);
10 [Link](true);
}
11 }
12

Output:
JScrollBar Class

It is used to add scroll bar, both horizontal and vertical.

Example:

1
2 import [Link].*;
3 class example{
4 example(){
5 JFrame a = new JFrame("example");
6 JScrollBar b = new JScrollBar();
7 [Link](90,90,40,90);
[Link](b);
8 [Link](300,300);
9 [Link](null);
10 [Link](true);
11 }
public static void main(String args[]){
12 new example();
13 }
14 }
15

Output:
JPanel Class

1
2 import [Link].*;
3 import [Link].*;
4
5 public class Example{
Example(){
6 JFrame a = new JFrame("example");
7 JPanel p = new JPanel();
8 [Link](40,70,200,200);
9 JButton b = new JButton("click me");
[Link](60,50,80,40);
10 [Link](b);
11 [Link](p);
12 [Link](400,400);
13 [Link](null);
14 [Link](true);
}
15 public static void main(String args[])
16 {
17 new Example();
18 }
}
19
20

Output:
JMenu Class

1
2 import [Link].*;
3
4 class Example{
JMenu menu;
5
JMenuItem a1,a2;
6
7 Example()
8 {
9 JFrame a = new JFrame("Example");
menu = new JMenu("options");
10 JMenuBar m1 = new JMenuBar();
11 a1 = new JMenuItem("example");
12 a2 = new JMenuItem("example1");
13 [Link](a1);
14 [Link](a2);
[Link](menu);
15 [Link](m1);
16 [Link](400,400);
17 [Link](null);
18 [Link](true);
}
19 public static void main(String args[])
20 {
21 new Example();
22 }
23 }
24
Output:
JList Class

1
2 import [Link].*;
3
4 public class Example
5 {
Example(){
6 JFrame a = new JFrame("example");
7 DefaultListModel<String> l = new DefaultListModel< >();
8 [Link]("first item");
9 [Link]("second item");
JList<String> b = new JList< >(l);
10 [Link](100,100,75,75);
11 [Link](b);
12 [Link](400,400);
13 [Link](true);
14 [Link](null);
}
15 public static void main(String args[])
16 {
17 new Example();
18 }
19 }
20
Output:
JLabel Class

It is used for placing text in a container.

1
2 import [Link].*;
3 public class Example{
4 public static void main(String args[])
5 {
6 JFrame a = new JFrame("example");
JLabel b1;
7 b1 = new JLabel("edureka");
8 [Link](40,40,90,20);
9 [Link](b1);
10 [Link](400,400);
11 [Link](null);
[Link](true);
12 }
13 }
14
Output:
JComboBox Class

It show pop up menu of choices.

1
2 import [Link].*;
3 public class Example{
4 JFrame a;
5
6 Example(){
a = new JFrame("example");
7 string courses[] = { "core java","advance java", "java servlet"};
8 JComboBox c = new JComboBox(courses);
9 [Link](40,40,90,20);
10 [Link](c);
11 [Link](400,400);
[Link](null);
12 [Link](true);
13 }
14 public static void main(String args[])
15 {
new Example();
16 }
17 }
18
Output:
Layout Manager
To arrange the components inside a container we use the layout manager. Following
are several layout managers:

1. Border layout
2. Flow layout
3. GridBag layout

Border Layout

The default layout manager for every JFrame is BorderLayout. It places components in
upto five places which is top, bottom, left, right and center.

Flow Layout

FlowLayout simply lays the components in a row one after the other, it is the default
layout manager for every JPanel.
GridBag Layout

GridBagLayout places the components in a grid which allows the components to span
more than one cell.

You might also like