Java Unit 4
Java Unit 4
Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface (GUI)
or windows-based applications in Java.
Java AWT components are platform-dependent i.e. components are displayed according to the
view of operating system. AWT is heavy weight i.e. its components are using the resources of
underlying operating system (OS).
The java.awt package provides classes for AWT API such as TextField, Label, TextArea,
RadioButton, CheckBox, Choice, List etc.
Java AWT calls the native platform calls the native platform (operating systems) subroutine for
creating API components like TextField, ChechBox, button, etc.
For example, an AWT GUI with components like TextField, label and button will have different
look and feel for the different platforms like Windows, MAC OS, and Unix. The reason for this
is the platforms have different view for their native components and AWT directly calls the
native subroutine that creates those components.
In simple words, an AWT application will look like a windows application in Windows OS
whereas it will look like a Mac application in the MAC OS.
All the elements like the button, text fields, scroll bars, etc. are called components. In Java AWT,
there are classes for each component as shown in above diagram. In order to place every
component in a particular position on a screen, we need to add them to a container.
Container
The Container is a component in AWT that can contain another components like buttons,
textfields, labels etc. The classes that extends Container class are known as container such
as Frame, Dialog and Panel.
It is basically a screen where the where the components are placed at their specific locations.
Thus it contains and controls the layout of components.
Note: A container itself is a component (see the above diagram), therefore we can add a
container inside container.
1. Window
2. Panel
3. Frame
4. Dialog
Window
The window is the container that have no borders and menu bars. You must use frame, dialog or
another window for creating a window. We need to create an instance of Window class to create
this container.
Panel
The Panel is the container that doesn't contain title bar, border or menu bar. It is generic
container for holding the components. It can have other components like button, text field etc.
An instance of Panel class creates a container, in which we can add components.
Frame
The Frame is the container that contain title bar and border and can have menu bars. It can have
other components like button, text field, scrollbar etc. Frame is most widely used container while
developing an AWT application.
public void setSize(int width,int height) Sets the size (width and height) of the component.
public void setLayout(LayoutManager Defines the layout manager for the component.
m)
To create simple AWT example, you need a frame. There are two ways to create a GUI using
Frame in AWT.
package application;
import java.awt.event.*;
import java.awt.*;
first(){
setTitle("BANK");
setSize(500,500);
setLayout(null);
l1.setBounds(30, 100,80,30);
add(l1);
add(l2);
add(t1);
add(t2);
add(b1);
setVisible(true);
addWindowListener(this);
new first();
@Override
@Override
dispose();
@Override
@Override
@Override
@Override
@Override
O/P:
The javax.swing package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
There are many differences between java awt and swing that are given below.
3) AWT doesn't support pluggable look and feel. Swing supports pluggable look and
4) AWT provides less components than Swing. Swing provides more powerful
components such as tables, lists,
scrollpanes, colorchooser, tabbedpane
etc.
What is JFC
The Java Foundation Classes (JFC) are a set of GUI components which simplify the
development of desktop applications.
The methods of Component class are widely used in java swing that are given below.
Method Description
public void setLayout(LayoutManager sets the layout manager for the component.
m)
We can write the code of swing inside the main(), constructor or any other method.
Java JButton
The JButton class is used to create a labeled button that has platform independent
implementation. The application result in some action when the button is pushed. It inherits
AbstractButton class.
Constructor Description
Description
Methods
1. import javax.swing.*;
2. public class ButtonExample {
3. public static void main(String[] args) {
4. JFrame f=new JFrame("Button Example");
5. JButton b=new JButton("Click Here");
6. b.setBounds(50,100,95,30);
7. f.add(b);
8. f.setSize(400,400);
9. f.setLayout(null);
10. f.setVisible(true);
Output:
Java JLabel
The object of JLabel class is a component for placing text in a container. It is used to display a
single line of read only text. The text can be changed by an application but a user cannot edit it
directly. It inherits JComponent class.
Constructor Description
JLabel() Creates a JLabel instance with no image and with an empty string
for the title.
JLabel(String s, Icon i, int Creates a JLabel instance with the specified text, image, and
horizontalAlignment) horizontal alignment.
Methods Description
void setText(String text) It defines the single line of text this component will
display.
void setHorizontalAlignment(int It sets the alignment of the label's contents along the X
alignment) axis.
Icon getIcon() It returns the graphic image that the label displays.
int getHorizontalAlignment() It returns the alignment of the label's contents along the
X axis.
Output:
Java JTextField
The object of a JTextField class is a text component that allows the editing of a single line text. It
inherits JTextComponent class.
Constructor Description
JTextField(String text) Creates a new TextField initialized with the specified text.
JTextField(String text, int columns) Creates a new TextField initialized with the specified text and columns.
JTextField(int columns) Creates a new empty TextField with the specified number of columns.
Methods Description
Action getAction() It returns the currently set Action for this ActionEvent
source, or null if no Action is set.
Output:
The object of a JTextArea class is a multi line region that displays text. It allows the editing of
multiple line text. It inherits JTextComponent class
Constructor Description
JTextArea(int row, int column) Creates a text area with the specified number of rows and columns
that displays no text initially.
JTextArea(String s, int row, int Creates a text area with the specified number of rows and columns
column) that displays specified text.
Methods Description
void insert(String s, int position) It is used to insert the specified text on the specified position.
void append(String s) It is used to append the given text to the end of the document.
1. import javax.swing.*;
2. public class TextAreaExample
3. {
4. TextAreaExample(){
5. JFrame f= new JFrame();
6. JTextArea area=new JTextArea("Welcome to javatpoint");
7. area.setBounds(10,30, 200,200);
8. f.add(area);
9. f.setSize(300,300);
10. f.setLayout(null);
11. f.setVisible(true);
12. }
13. public static void main(String args[])
14. {
15. new TextAreaExample();
16. }}
Output:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JTabbedPane tabs;
Home h1;
Log1 log;
Reg re;
TabbedPane() {
h1=new Home();
h1.setBackground(Color.cyan);
Prepard by K.Swathi Lakshmi Durga
Assistant Professor
Department of IT
log = new Log1();
log.setBackground(Color.darkGray);
re = new Reg();
re.setBackground(Color.yellow);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tabs.addTab("HOME", h1);
tabs.addTab("LOGIN", log);
tabs.addTab("Registration", re);
getContentPane().add(tabs);
tabs.setBackground(Color.green);
JLabel l1;
Home() {
setLayout(new FlowLayout());
frame.setSize(400, 400);
frame.setVisible(true);
//frame.setBackground(Color.CYAN);
Log1.java:
package application;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
JLabel l1;
JLabel l2;
JTextField t1;
JLabel t2;
JPasswordField psw;
JButton b1;
Log1() {
l1 = new JLabel("USername");
l2 = new JLabel("password");
t2 = new JLabel();
b1 = new JButton(i1);
l2.setBounds(30,180,100,50);
t1.setBounds(140,100,100,50);
psw.setBounds(140,180,100,50);
b1.addActionListener(this);
add(l2);
add(t1);
add(psw);
add(b1);
setVisible(true);
setSize(400,400);
setLayout(null);
@Override
new Home();
Reg.java:
package application;
import javax.swing.*;
O/P:
The LayoutManagers are used to arrange components in a particular manner. The Java
LayoutManagers facilitates us to control the positioning and size of the components in GUI
forms. LayoutManager is an interface that is implemented by all the classes of layout managers.
There are the following classes that represent the layout managers:
1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. java.awt.GridBagLayout
6. javax.swing.BoxLayout
7. javax.swing.GroupLayout
8. javax.swing.ScrollPaneLayout
Prepard by K.Swathi Lakshmi Durga
Assistant Professor
Department of IT
9. javax.swing.SpringLayout etc.
Java BorderLayout
The BorderLayout is used to arrange the components in five regions: north, south, east, west, and
center. Each region (area) may contain one component only. It is the default layout of a frame or
window. The BorderLayout provides five constants for each region:
o BorderLayout(): creates a border layout but with no gaps between the components.
o BorderLayout(int hgap, int vgap): creates a border layout with the given horizontal and
vertical gaps between the components.
FileName: Border.java
1. import java.awt.*;
2. import javax.swing.*;
3.
4. public class Border
5. {
6. JFrame f;
7. Border()
8. {
Output:
The Java GridLayout class is used to arrange the components in a rectangular grid. One
component is displayed in each rectangle.
1. GridLayout(): creates a grid layout with one column per component in a row.
2. GridLayout(int rows, int columns): creates a grid layout with the given rows and
columns but no gaps between the components.
3. GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the
given rows and columns along with given horizontal and vertical gaps.
The GridLayout() constructor creates only one row. The following example shows the usage of
the parameterless constructor.
FileName: GridLayoutExample.java
FileName: GridLayoutExample1.java
Prepard by K.Swathi Lakshmi Durga
Assistant Professor
Department of IT
1. / import statements
2. import java.awt.*;
3. import javax.swing.*;
4.
5. public class GridLayoutExample1
6. {
7.
8. JFrame frameObj;
9.
10. // constructor
11. GridLayoutExample1()
12. {
13. frameObj = new JFrame();
14.
15. // creating 9 buttons
16. JButton btn1 = new JButton("1");
17. JButton btn2 = new JButton("2");
18. JButton btn3 = new JButton("3");
19. JButton btn4 = new JButton("4");
20. JButton btn5 = new JButton("5");
21. JButton btn6 = new JButton("6");
22. JButton btn7 = new JButton("7");
23. JButton btn8 = new JButton("8");
24. JButton btn9 = new JButton("9");
25.
26. // adding buttons to the frame
27. // since, we are using the parameterless constructor, therefore;
28. // the number of columns is equal to the number of buttons we
29. // are adding to the frame. The row count remains one.
30. frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3);
31. frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6);
32. frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);
33. // setting the grid layout
Output:
Java FlowLayout
The Java FlowLayout class is used to arrange the components in a line, one after another (in a
flow). It is the default layout of the applet or panel.
1. FlowLayout(): creates a flow layout with centered alignment and a default 5 unit
horizontal and vertical gap.
2. FlowLayout(int align): creates a flow layout with the given alignment and a default 5
unit horizontal and vertical gap.
3. FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given
alignment and the given horizontal and vertical gap.
FileName: FlowLayoutExample.java
1. // import statements
2. import java.awt.*;
3. import javax.swing.*;
4.
5. public class FlowLayoutExample
6. {
7.
8. JFrame frameObj;
9.
10. // constructor
11. FlowLayoutExample()
12. {
13. // creating a frame object
14. frameObj = new JFrame();
15.
16. // creating the buttons
17. JButton b1 = new JButton("1");
Output: