Week 13 - GUI Programming
Week 13 - GUI Programming
GUI
What is Swing?
Java Swing is a lightweight Graphical User Interface (GUI) toolkit that includes a rich set of
widgets. It includes package lets you make GUI components for your Java applications, and It
is platform independent.
The Swing library is built on top of the Java Abstract Widget Toolkit (AWT), an older, platform
dependent GUI toolkit. You can use the Java GUI components like button, textbox, etc. from
the library and do not have to create the components from scratch.
All components in swing are JComponent which can be added to container classes.
Container classes are classes that can have other components on it. So for creating a GUI, we
need at least one container object. There are 3 types of containers.
1. Panel: It is a pure container and is not a window in itself. The sole purpose of a Panel is
to organize the components on to a window.
2. Frame: It is a fully functioning window with its title and icons.
1|P ag e
Object Oriented Programming
GUI
3. Dialog: It can be thought of like a pop-up window that pops out when a message has to
be displayed. It is not a fully functioning window like the Frame.
import javax.swing.*;
class gui{
public static void main(String args[]){
JFrame frame = new JFrame("My First GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
JButton button = new JButton("Press");
frame.getContentPane().add(button); // Adds Button to content pane of frame
frame.setVisible(true);
}
}
import javax.swing.*;
class gui{
public static void main(String args[]){
JFrame frame = new JFrame("My First GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
JButton button1 = new JButton("Press");
frame.getContentPane().add(button1);
frame.setVisible(true);
}
}
2|P ag e
Object Oriented Programming
GUI
Step 5) How about adding two buttons? Copy the following code into an editor.
import javax.swing.*;
class gui{
public static void main(String args[]){
JFrame frame = new JFrame("My First GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
frame.getContentPane().add(button1);
frame.getContentPane().add(button2);
frame.setVisible(true);
}
}
The Layout manager is used to layout (or arrange) the GUI java components inside a
container.There are many layout managers, but the most frequently used are-
Java BorderLayout
A BorderLayout places components in up to five areas: top, bottom, left, right, and center. It
is the default layout manager for every java JFrame
3|P ag e
Object Oriented Programming
GUI
Java FlowLayout
FlowLayout is the default layout manager for every JPanel. It simply lays out components in
a single row one after the other.
Java GridBagLayout
It is the more sophisticated of all layouts. It aligns components by placing them within a grid of cells,
allowing components to span more than one cell.
4|P ag e
Object Oriented Programming
GUI
5|P ag e
Object Oriented Programming
GUI
Source:
6|P ag e