GUI Examples 04 10
GUI Examples 04 10
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.
Container Class
Any class which has other components in it is called as a container class. For building
GUI applications at least one container class is necessary.
Explanation: 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. Following are a few components with examples to understand how we can use
them.
JButton Class
It is used to create a labelled button. Using the ActionListener it will result in some
action when the button is pushed. It inherits the AbstractButton class and is platform
independent.
Example:
1
2 import javax.swing.*;
3 public class example{
public static void main(String args[]) {
4 JFrame a = new JFrame("example");
5 JButton b = new JButton("click me");
6 b.setBounds(40,90,85,20);
7 a.add(b);
a.setSize(300,300);
8
a.setLayout(null);
9 a.setVisible(true);
10 }
11 }
12
Output:
JTextField Class
It inherits the JTextComponent class and it is used to allow editing of single line text.
Example:
Java Certification Training Course
Instructor-led Sessions
Real-life Case Studies
Assignments
Lifetime Access
Explore Curriculum
1
2 import javax.swing.*;
3 public class example{
public static void main(String args[]) {
4 JFrame a = new JFrame("example");
5 JTextField b = new JTextField("edureka");
6 b.setBounds(50,100,200,30);
7 a.add(b);
a.setSize(300,300);
8
a.setLayout(null);
9 a.setVisible(true);
10 }
11 }
12
Output:
JScrollBar Class
Example:
1
2 import javax.swing.*;
3 class example{
4 example(){
JFrame a = new JFrame("example");
5 JScrollBar b = new JScrollBar();
6 b.setBounds(90,90,40,90);
7 a.add(b);
8 a.setSize(300,300);
9 a.setLayout(null);
a.setVisible(true);
10 }
11 public static void main(String args[]){
12 new example();
13 }
}
14
15
Output:
JPanel Class
It inherits the JComponent class and provides space for an application which can attach any other
component.
1
2
import java.awt.*;
3 import javax.swing.*;
4 public class Example{
5 Example(){
6 JFrame a = new JFrame("example");
7 JPanel p = new JPanel();
p.setBounds(40,70,200,200);
8 JButton b = new JButton("click me");
9 b.setBounds(60,50,80,40);
10 p.add(b);
11 a.add(p);
a.setSize(400,400);
12
a.setLayout(null);
13 a.setVisible(true);
14 }
15 public static void main(String args[])
16 {
new Example();
17 }
18 }
19
20
Output:
JMenu Class
It inherits the JMenuItem class, and is a pull down menu component which is displayed from the menu
bar.
1
2
3 import javax.swing.*;
4 class Example{
JMenu menu;
5 JMenuItem a1,a2;
6 Example()
7 {
8 JFrame a = new JFrame("Example");
9 menu = new JMenu("options");
JMenuBar m1 = new JMenuBar();
10 a1 = new JMenuItem("example");
11 a2 = new JMenuItem("example1");
12 menu.add(a1);
13 menu.add(a2);
m1.add(menu);
14
a.setJMenuBar(m1);
15 a.setSize(400,400);
16 a.setLayout(null);
17 a.setVisible(true);
18 }
public static void main(String args[])
19 {
20 new Example();
21 }
22 }
23
24
Output:
JList Class
It inherits JComponent class, the object of JList class represents a list of text items.
1
2
import javax.swing.*;
3 public class Example
4 {
5 Example(){
6 JFrame a = new JFrame("example");
7 DefaultListModel<String> l = new DefaultListModel< >();
l.addElement("first item");
8 l.addElement("second item");
9 JList<String> b = new JList< >(l);
10 b.setBounds(100,100,75,75);
11 a.add(b);
a.setSize(400,400);
12
a.setVisible(true);
13 a.setLayout(null);
14 }
15 public static void main(String args[])
16 {
new Example();
17 }
18 }
19
20
Output:
JLabel Class
It is used for placing text in a container. It also inherits JComponent class.
1
2 import javax.swing.*;
3 public class Example{
4 public static void main(String args[])
{
5 JFrame a = new JFrame("example");
6 JLabel b1;
7 b1 = new JLabel("edureka");
8 b1.setBounds(40,40,90,20);
a.add(b1);
9
a.setSize(400,400);
10 a.setLayout(null);
11 a.setVisible(true);
12 }
13 }
14
Output:
JComboBox Class
It inherits the JComponent class and is used to show pop up menu of choices.
1 import javax.swing.*;
public class Example{
2
JFrame a;
3 Example(){
4 a = new JFrame("example");
5 string courses[] = { "core java","advance java", "java servlet"};
6 JComboBox c = new JComboBox(courses);
c.setBounds(40,40,90,20);
7 a.add(c);
8
9
a.setSize(400,400);
10 a.setLayout(null);
11 a.setVisible(true);
12 }
13 public static void main(String args[])
14 {
new Example();
15 }
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.
Example: Chat Frame
1 import javax.swing.*;
import java.awt.*;
2
class Example {
3 public static void main(String args[]) {
4
5
6 JFrame frame = new JFrame("Chat Frame");
7 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
8 frame.setSize(400, 400);
9
JMenuBar ob = new JMenuBar();
10 JMenu ob1 = new JMenu("FILE");
11 JMenu ob2 = new JMenu("Help");
12 ob.add(ob1);
13 ob.add(ob2);
14 JMenuItem m11 = new JMenuItem("Open");
JMenuItem m22 = new JMenuItem("Save as");
15 ob1.add(m11);
16 ob1.add(m22);
17
18
19 JPanel panel = new JPanel(); // the panel is not visible in output
20 JLabel label = new JLabel("Enter Text");
21 JTextField tf = new JTextField(10); // accepts upto 10 characters
JButton send = new JButton("Send");
22 JButton reset = new JButton("Reset");
23 panel.add(label); // Components Added using Flow Layout
24 panel.add(label); // Components Added using Flow Layout
25 panel.add(tf);
panel.add(send);
26 panel.add(reset);
27 JTextArea ta = new JTextArea();
28
29 frame.getContentPane().add(BorderLayout.SOUTH, panel);
30 frame.getContentPane().add(BorderLayout.NORTH, tf);
31 frame.getContentPane().add(BorderLayout.CENTER, ta);
frame.setVisible(true);
32 }
33 }
34
35
36
37
38
39
If you’re just beginning, then watch at this Java Tutorial to Understand the Fundamental
Java Concepts.