0% found this document useful (0 votes)
5 views

AWT COMPONENTS

Uploaded by

Lakshmi G
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

AWT COMPONENTS

Uploaded by

Lakshmi G
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

AWT COMPONENTS

• Java AWT Button


• A button is basically a control component with a label that generates an event when
pushed. The Button class is used to create a labeled button that has platform
independent implementation. The application result in some action when the button is
pushed.

• When we press a button and release it, AWT sends an instance of ActionEvent to that
button by calling processEvent on the button.

• The processEvent method of the button receives the all the events, then it passes an
action event by calling its own method processActionEvent. This method passes the
action event on to action listeners that are interested in the action events generated by
the button.

• To perform an action on a button being pressed and released,


the ActionListener interface needs to be implemented. The registered new listener can
receive events from the button by calling addActionListener method of the button. The
Java application can use the button's action command as a messaging protocol.
AWT Button Class Declaration
public class Button extends Component implements Accessible

Button Class Constructors

Sr. no. Constructor Description

1. Button( ) It constructs a new


button with an empty
string i.e. it has no
label.
2. Button (String text) It constructs a new
button with given
string as its label.
ButtonExample.java
import java.awt.*;
public class ButtonExample {
public static void main (String[] args) {
// create instance of frame with the label
Frame f = new Frame("Button Example");

// create instance of button with label


Button b = new Button("Click Here");

// set the position for the button in frame


b.setBounds(50,100,80,30);

// add button to the frame


f.add(b);
// set size, layout and visibility of frame
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Java AWT Label
The object of the Label 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 a
programmer but a user cannot edit it directly.
It is called a passive control as it does not create any event when it is
accessed. To create a label, we need to create the object of Label class.
AWT Label Class Declaration
public class Label extends Component implements Accessible
Label class Constructors
Sr. no. Constructor Description

1. Label() It constructs an empty


label.
2. Label(String text) It constructs a label
with the given string
(left justified by
default).
3. Label(String text, int It constructs a label
alignement) with the specified
string and the
specified alignment.
LabelExample.java
import java.awt.*;
public class LabelExample {
public static void main(String args[]){
// creating the object of Frame class and Label class
Frame f = new Frame ("Label example");
Label l1, l2;
// initializing the labels
l1 = new Label ("First Label.");
l2 = new Label ("Second Label.");
// set the location of label
l1.setBounds(50, 100, 100, 30);
l2.setBounds(50, 150, 100, 30);
// adding labels to the frame
f.add(l1);
f.add(l2);
// setting size, layout and visibility of frame
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Java AWT TextField
The object of a TextField class is a text component that allows a user to enter
a single line text and edit it. It inherits TextComponent class, which further
inherits Component class.

When we enter a key in the text field (like key pressed, key released or key
typed), the event is sent to TextField. Then the KeyEvent is passed to the
registered KeyListener. It can also be done using ActionEvent;

if the ActionEvent is enabled on the text field, then the ActionEvent may be
fired by pressing return key. The event is handled by
the ActionListener interface.

AWT TextField Class Declaration


public class TextField extends TextComponent
TextField Class constructors

Sr. no. Constructor Description

1. TextField() It constructs a new text


field component.
2. TextField(String text) It constructs a new text
field initialized with the
given string text to be
displayed.

3. TextField(int columns) It constructs a new


textfield (empty) with
given number of
columns.
4. TextField(String text, It constructs a new text
int columns) field with the given
text and given number
of columns (width).
1.// importing AWT class
2.import java.awt.*;
3.public class TextFieldExample1 {
4. // main method
5. public static void main(String args[]) {
6. // creating a frame
7. Frame f = new Frame("TextField Example");
8.
9. // creating objects of textfield
10. TextField t1, t2;
11. // instantiating the textfield objects
12. // setting the location of those objects in the frame
13. t1 = new TextField("Welcome to Javatpoint.");
14. t1.setBounds(50, 100, 200, 30);
15. t2 = new TextField("AWT Tutorial");
16. t2.setBounds(50, 150, 200, 30);
17. // adding the components to frame
18. f.add(t1);
19. f.add(t2);
20. // setting size, layout and visibility of frame
21. f.setSize(400,400);
22. f.setLayout(null);
23. f.setVisible(true);
24.}
Java AWT TextArea
The object of a TextArea class is a multiline region that displays text. It allows
the editing of multiple line text. It inherits TextComponent class.
The text area allows us to type as much text as we want. When the text in the
text area becomes larger than the viewable area, the scroll bar appears
automatically which helps us to scroll the text up and down, or right and left.
AWT TextArea Class Declaration
public class TextArea extends TextComponent
Fields of TextArea Class
The fields of java.awt.TextArea class are as follows:
static int SCROLLBARS_BOTH - It creates and displays both horizontal and
vertical scrollbars.
static int SCROLLBARS_HORIZONTAL_ONLY - It creates and displays only the
horizontal scrollbar.
static int SCROLLBARS_VERTICAL_ONLY - It creates and displays only the
vertical scrollbar.
static int SCROLLBARS_NONE - It doesn't create or display any scrollbar in the
text area Class constructors:

Sr. no. Constructor Description

1. TextArea() It constructs a new and


empty text area with no text
in it.
2. TextArea (int row, int column) It constructs a new text area
with specified number of rows
and columns and empty
string as text.

3. TextArea (String text) It constructs a new text area


and displays the specified
text in it.
4. TextArea (String text, int row, It constructs a new text area
int column) with the specified text in the
text area and specified
number of rows and columns.

5. TextArea (String text, int row, It construcst a new text area


int column, int scrollbars) with specified text in text
area and specified number of
rows and columns and
visibility.
//importing AWT class
import java.awt.*;
public class TextAreaExample
{
// constructor to initialize
TextAreaExample() {
// creating a frame
Frame f = new Frame();
// creating a text area
TextArea area = new TextArea("Welcome to javatpoint");
// setting location of text area in frame
area.setBounds(10, 30, 300, 300);
// adding text area to frame
f.add(area);
// setting size, layout and visibility of frame
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true); }
// main method
public static void main(String args[])
{ new TextAreaExample();
}
}
Java AWT Checkbox
The Checkbox class is used to create a checkbox. It is used to turn an option on (true)
or off (false). Clicking on a Checkbox changes its state from "on" to "off" or from "off"
to "on".
AWT Checkbox Class Declaration
public class Checkbox extends Component implements ItemSelectable, Accessible
Checkbox Class Constructors
Sr. no. Constructor Description

1. Checkbox() It constructs a
checkbox with no string
as the label.
2. Checkbox(String label) It constructs a
checkbox with the
given label.
3. Checkbox(String label, It constructs a
boolean state) checkbox with the
given label and sets
the given state.
4. Checkbox(String label, It constructs a
boolean state, checkbox with the
CheckboxGroup group) given label, set the
given state in the
specified checkbox
group.
// importing AWT class
import java.awt.*;
public class CheckboxExample1
{
// constructor to initialize
CheckboxExample1() {
// creating the frame with the title
Frame f = new Frame("Checkbox Example");
// creating the checkboxes
Checkbox checkbox1 = new Checkbox("C++");
checkbox1.setBounds(100, 100, 50, 50);
Checkbox checkbox2 = new Checkbox("Java", true);
// setting location of checkbox in frame
checkbox2.setBounds(100, 150, 50, 50);
// adding checkboxes to frame
f.add(checkbox1);
f.add(checkbox2);
// setting size, layout and visibility of frame
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
} // main method
public static void main (String args[])
{
new CheckboxExample1();
}
}
Java AWT CheckboxGroup
The object of CheckboxGroup class is used to group together a set of Checkbox.
At a time only one check box button is allowed to be in "on" state and remaining
check box button in "off" state. It inherits the object class.

AWT CheckboxGroup Class Declaration


public class CheckboxGroup extends Object implements Serializable
Java AWT CheckboxGroup Example
import java.awt.*;
public class CheckboxGroupExample
{
CheckboxGroupExample(){
Frame f= new Frame("CheckboxGroup Example");
CheckboxGroup cbg = new CheckboxGroup();
Checkbox checkBox1 = new Checkbox("C++", cbg, false);
checkBox1.setBounds(100,100, 50,50);
Checkbox checkBox2 = new Checkbox("Java", cbg, true);
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new CheckboxGroupExample();
}
}
https://www.javatpoint.com/java-awt-
checkboxgroup
AWT List Class Constructors

Java AWT List


The object of List class represents a list of text items. With the help of the List
class, user can choose either one item or multiple items. It inherits the
Component class.
AWT List class Declaration
public class List extends Component implements ItemSelectable, Accessible

Sr. no. Constructor Description

1. List() It constructs a new


scrolling list.
2. List(int row_num) It constructs a new
scrolling list initialized
with the given number
of rows visible.
3. List(int row_num, It constructs a new
Boolean multipleMode) scrolling list initialized
which displays the
given number of rows.
ListExample1.java
// importing awt class
import java.awt.*;

public class ListExample1


{
// class constructor
ListExample1() {
// creating the frame
Frame f = new Frame();
// creating the list of 5 rows
List l1 = new List(5);

// setting the position of list component


l1.setBounds(100, 100, 75, 75);

// adding list items into the list


l1.add("Item 1");
l1.add("Item 2");
l1.add("Item 3");
l1.add("Item 4");
l1.add("Item 5");
// adding the list to frame
f.add(l1);

// setting size, layout and visibility of frame


f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}

// main method
public static void main(String args[])
{
new ListExample1();
}
}
Choice Class constructor

Java AWT Choice


The object of Choice class is used to show popup menu of choices. Choice selected
by user is shown on the top of a menu. It inherits Component class.
AWT Choice Class Declaration
public class Choice extends Component implements ItemSelectable, Accessible

Sr. no. Constructor Description

1. Choice() It constructs a new


choice menu.
/ importing awt class
import java.awt.*;
public class ChoiceExample1 {

// class constructor
ChoiceExample1() {

// creating a frame
Frame f = new Frame();

// creating a choice component


Choice c = new Choice();

// setting the bounds of choice menu


c.setBounds(100, 100, 75, 75);

// adding items to the choice menu


c.add("Item 1");
c.add("Item 2");
c.add("Item 3");
c.add("Item 4");
c.add("Item 5");
// adding choice menu to frame
f.add(c);

// setting size, layout and visibility of frame


f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}

// main method
public static void main(String args[])
{
new ChoiceExample1();
}
}
Java AWT Scrollbar
The object of Scrollbar class is used to add horizontal and vertical
scrollbar. Scrollbar is a GUI component allows us to see invisible
number of rows and columns.
It can be added to top-level container like Frame or a component like
Panel. The Scrollbar class extends the Component class.
AWT Scrollbar Class Declaration
public class Scrollbar extends Component implements Adjustable, A
ccessible

Scrollbar Class Fields


The fields of java.awt.Image class are as follows:
static int HORIZONTAL - It is a constant to indicate a horizontal scroll bar.
static int VERTICAL - It is a constant to indicate a vertical scroll bar.
Scrollbar Class Constructors

Sr. no. Constructor Description

1 Scrollbar() Constructs a new


vertical scroll bar.
2 Scrollbar(int Constructs a new
orientation) scroll bar with the
specified
orientation.
3 Scrollbar(int Constructs a new
orientation, int scroll bar with the
value, int visible, specified
int minimum, int orientation, initial
maximum) value, visible
amount, and
minimum and
maximum values.
// importing awt package
import java.awt.*;
public class ScrollbarExample1 {
// class constructor
ScrollbarExample1() {
// creating a frame
Frame f = new Frame("Scrollbar Example");
// creating a scroll bar
Scrollbar s = new Scrollbar();
// setting the position of scroll bar
s.setBounds (100, 100, 50, 100);
// adding scroll bar to the frame
f.add(s);
// setting size, layout and visibility of frame
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}
// main method
public static void main(String args[]) {
new ScrollbarExample1();
}
}
BorderLayout (LayoutManagers)
Java LayoutManagers
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:
java.awt.BorderLayout
java.awt.FlowLayout
java.awt.GridLayout
java.awt.CardLayout
java.awt.GridBagLayout
javax.swing.BoxLayout
javax.swing.GroupLayout
javax.swing.ScrollPaneLayout
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:
public static final int NORTH
public static final int SOUTH
public static final int EAST
public static final int WEST
public static final int CENTER

Constructors of BorderLayout class:


BorderLayout(): creates a border layout but with no gaps between the
components.
BorderLayout(int hgap, int vgap): creates a border layout with the given
horizontal and vertical gaps between the components.
import java.awt.*;
import javax.swing.*;

public class Border


{
JFrame f;
Border()
{
f = new JFrame();

// creating buttons
JButton b1 = new JButton("NORTH");; // the button will be labeled as NORTH
JButton b2 = new JButton("SOUTH");; // the button will be labeled as SOUTH
JButton b3 = new JButton("EAST");; // the button will be labeled as EAST
JButton b4 = new JButton("WEST");; // the button will be labeled as WEST
JButton b5 = new JButton("CENTER");; // the button will be labeled as CENTER

f.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction


f.add(b2, BorderLayout.SOUTH); // b2 will be placed in the South Direction
f.add(b3, BorderLayout.EAST); // b2 will be placed in the East Direction
f.add(b4, BorderLayout.WEST); // b2 will be placed in the West Direction
f.add(b5, BorderLayout.CENTER); // b2 will be placed in the Center
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[] args) {
new Border();
}
}
Java GridLayout
The Java GridLayout class is used to arrange the components in a rectangular grid. One
component is displayed in each rectangle.
Constructors of GridLayout class
GridLayout(): creates a grid layout with one column per component in a row.
GridLayout(int rows, int columns): creates a grid layout with the given rows and
columns but no gaps between the components.
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.
import java.awt.*;
import javax.swing.*;
public class MyGridLayout{
JFrame f;
MyGridLayout(){
f=new JFrame();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
// adding buttons to the frame
f.add(b1); f.add(b2); f.add(b3);
f.add(b4); f.add(b5); f.add(b6);
f.add(b7); f.add(b8); f.add(b9);
// setting grid layout of 3 rows and 3 columns
f.setLayout(new GridLayout(3,3));
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new MyGridLayout();
}
}
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.
Fields of FlowLayout class
public static final int LEFT
public static final int RIGHT
public static final int CENTER
public static final int LEADING
public static final int TRAILING
Constructors of FlowLayout class
FlowLayout(): creates a flow layout with centered alignment and a
default 5 unit horizontal and vertical gap.
FlowLayout(int align): creates a flow layout with the given alignment and
a default 5 unit horizontal and vertical gap.
FlowLayout(int align, int hgap, int vgap): creates a flow layout with the
given alignment and the given horizontal and vertical gap.
import java.awt.*;
import javax.swing.*;

public class MyFlowLayout{


JFrame f;
MyFlowLayout(){
f=new JFrame();

JButton b1=new JButton("1");


JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");

// adding buttons to the frame


f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5);

// setting flow layout of right alignment


f.setLayout(new FlowLayout(FlowLayout.RIGHT));

f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new MyFlowLayout();
}
}
Java BoxLayout
The Java BoxLayout class is used to arrange the components either vertically or
horizontally. For this purpose, the BoxLayout class provides four constants. They are
as follows:

Constructor of BoxLayout class


BoxLayout(Container c, int axis): creates a box layout that arranges the
components with the given axis.
Example of BoxLayout class with Y-AXIS:
FileName: BoxLayoutExample1.java
import java.awt.*;
import javax.swing.*;

public class BoxLayoutExample1 extends Frame {


Button buttons[];

public BoxLayoutExample1 () {
buttons = new Button [5];

for (int i = 0;i<5;i++) {


buttons[i] = new Button ("Button " + (i + 1));
// adding the buttons so that it can be displayed
add (buttons[i]);
}
// the buttons will be placed horizontally
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
setSize(400,400);
setVisible(true);
}
// main method
public static void main(String args[]){
BoxLayoutExample1 b=new BoxLayoutExample1();
}
}
Java CardLayout
The Java CardLayout class manages the components in such a manner that only
one component is visible at a time. It treats each component as a card that is why
it is known as CardLayout.
Constructors of CardLayout Class
CardLayout(): creates a card layout with zero horizontal and vertical gap.
CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and
vertical gap.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class CardLayoutExample2 extends JFrame implements Action


Listener{
CardLayout card;
JButton b1,b2,b3;
Container c;
CardLayoutExample2(){

c=getContentPane();
card=new CardLayout(40,30);
//create CardLayout object with 40 hor space and 30 ver space
c.setLayout(card);

b1=new JButton("Apple");
b2=new JButton("Boy");
b3=new JButton("Cat");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

c.add("a",b1);c.add("b",b2);c.add("c",b3);

}
public void actionPerformed(ActionEvent e) {
card.next(c);
}

public static void main(String[] args) {


CardLayoutExample2 cl=new CardLayoutExample2();
cl.setSize(400,400);
cl.setVisible(true);
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

You might also like