Basic Terminologies: Java AWT (Abstract Windowing Toolkit) Is An API To Develop GUI or Window
Basic Terminologies: Java AWT (Abstract Windowing Toolkit) Is An API To Develop GUI or Window
Basic Terminologies: Java AWT (Abstract Windowing Toolkit) Is An API To Develop GUI or Window
The java.awt package provides classes for AWT api such as TextField, Label,
TextArea, RadioButton, CheckBox, Choice, List etc.
Basic Terminologies
Term Description
Window The window is the container that have no borders and menu
bars. Window is a rectangular area which is displayed on the
screen. In different window we can execute different program
and display different data. Window provide us with multitasking
environment. A window must have either a frame, dialog, or
another window defined as its owner when it's constructed.
Method Description
To create simple awt example, you need a frame. There are two ways to create a
frame in AWT.
The setBounds(intxaxis, intyaxis, int width, int height) method is used in the above
example that sets the position of the awt button.
AWT UI Elements:
Every user interface considers the following three main aspects:
UI elements :Thes are the core visual elements the user eventually sees and
interacts with. GWT provides a huge list of widely used and common
elements varying from basic to complex
1 Label
2 Button
5 List
The List component presents the user with a scrolling list of text
items.
6 Text Field
7 Text Area
8 Choice
9 Canvas
10 Image
An Image control is superclass for all image classes representing
graphical images.
11 Scroll Bar
12 Dialog
13 File Dialog
BorderLayout
BoxLayout
CardLayout
FlowLayout
GridBagLayout
GridLayout
GroupLayout
SpringLayout
BorderLayout
1. import java.awt.*;
2. import javax.swing.*;
3.
4. public class Border {
5. JFrame f;
6. Border(){
7. f=new JFrame();
8.
9. JButton b1=new JButton("NORTH");;
10. JButton b2=new JButton("SOUTH");;
11. JButton b3=new JButton("EAST");;
12. JButton b4=new JButton("WEST");;
13. JButton b5=new JButton("CENTER");;
14.
15. f.add(b1,BorderLayout.NORTH);
16. f.add(b2,BorderLayout.SOUTH);
17. f.add(b3,BorderLayout.EAST);
18. f.add(b4,BorderLayout.WEST);
19. f.add(b5,BorderLayout.CENTER);
20.
21. f.setSize(300,300);
22. f.setVisible(true);
23.}
24.public static void main(String[] args) {
25. new Border();
26.}
27.}
BoxLayout
The BoxLayout class puts components in a single row or column. It respects the
components' requested maximum sizes and also lets you align components.For this
purpose, BoxLayout provides four constants. They are as follows:
Note: BoxLayout class is found in javax.swing package.
1. import java.awt.*;
2. import javax.swing.*;
3.
4. public class BoxLayoutExample2 extends Frame {
5. Button buttons[];
6.
7. public BoxLayoutExample2() {
8. buttons = new Button [5];
9.
10. for (int i = 0;i<5;i++) {
11. buttons[i] = new Button ("Button " + (i + 1));
12. add (buttons[i]);
13. }
14.
15.setLayout (new BoxLayout(this, BoxLayout.X_AXIS));
16.setSize(400,400);
17.setVisible(true);
18.}
19.
20.public static void main(String args[]){
21.BoxLayoutExample2 b=new BoxLayoutExample2();
22.}
23.}
24.
CardLayout
The CardLayout class lets you implement an area that contains different
components at different times. A CardLayout is often controlled by a combo box,
with the state of the combo box determining which panel (group of components)
the CardLayoutdisplays. An alternative to using CardLayout is using a tabbed
pane, which provides similar functionality but with a pre-defined GUI.
FlowLayout
FlowLayout is the default layout manager for every JPanel. It simply lays out
components in a single row, starting a new row if its container is not sufficiently
wide. Both panels in CardLayoutDemo, shown previously, use FlowLayout.
1. import java.awt.*;
2. import javax.swing.*;
3.
4. public class MyFlowLayout{
5. JFrame f;
6. MyFlowLayout(){
7. f=new JFrame();
8.
9. JButton b1=new JButton("1");
10. JButton b2=new JButton("2");
11. JButton b3=new JButton("3");
12. JButton b4=new JButton("4");
13. JButton b5=new JButton("5");
14.
15. f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);
16.
17. f.setLayout(new FlowLayout(FlowLayout.RIGHT));
18. //setting flow layout of right alignment
19.
20. f.setSize(300,300);
21. f.setVisible(true);
22.}
23.public static void main(String[] args) {
24. new MyFlowLayout();
25.}
26.}
GridLayout
GridLayout simply makes a bunch of components equal in size and displays them
in the requested number of rows and columns..
1. import java.awt.*;
2. import javax.swing.*;
3.
4. public class MyGridLayout{
5. JFrame f;
6. MyGridLayout(){
7. f=new JFrame();
8.
9. JButton b1=new JButton("1");
10. JButton b2=new JButton("2");
11. JButton b3=new JButton("3");
12. JButton b4=new JButton("4");
13. JButton b5=new JButton("5");
14. JButton b6=new JButton("6");
15. JButton b7=new JButton("7");
16. JButton b8=new JButton("8");
17. JButton b9=new JButton("9");
18.
19. f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);
20. f.add(b6);f.add(b7);f.add(b8);f.add(b9);
21.
22. f.setLayout(new GridLayout(3,3));
23. //setting grid layout of 3 rows and 3 columns
24.
25. f.setSize(300,300);
26. f.setVisible(true);
27.}
28.public static void main(String[] args) {
29. new MyGridLayout();
30.}
31.}
GridBagLayout
GroupLayout is a layout manager that was developed for use by GUI builder tools,
but it can also be used manually. GroupLayoutworks with the horizontal and
vertical layouts separately. The layout is defined for each dimension
independently. Consequently, however, each component needs to be defined twice
in the layout. The Find window shown above is an example of a GroupLayout.
SpringLayout
SpringLayout is a flexible layout manager designed for use by GUI builders. It lets
you specify precise relationships between the edges of components under its
control. For example, you might define that the left edge of one component is a
certain distance (which can be dynamically calculated) from the right edge of a
second component.SpringLayout lays out the children of its associated container
according to a set of constraints