Java AWT guidance

Java AWT 

Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-based applications in java.
Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight i.e. its components are using the resources of OS.
The java.awt package provides classes for AWT api such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.

ava AWT Hierarchy

The hierarchy of Java AWT classes are given below.

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.

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.

Panel

The Panel is the container that doesn't contain title bar and menu bars. It can have other components like button, textfield etc.

Frame

The Frame is the container that contain title bar and can have menu bars. It can have other components like button, textfield etc.

Useful Methods of Component class

MethodDescription
public void add(Component c)inserts a component on this component.
public void setSize(int width,int height)sets the size (width and height) of the component.
public void setLayout(LayoutManager m)defines the layout manager for the component.
public void setVisible(boolean status)changes the visibility of the component, by default false.

1)setBounds()

The setBounds() method needs four arguments. The first two arguments are x and y coordinates of the top-left corner of the component, the third argument is the width of the component and the fourth argument is the height of the component.


Syntax:-


setBounds(int x-coordinate, int y-coordinate, int width, int height)


2)setSize () 


syntax:-


public void setSize(int width,

           int height)

   

Sets the size of this Dimension object to the specified width and height. This method is included for completeness, to parallel the setSize method defined by Component.

Parameters:

width - the new width for this Dimension object

height - the new height for this Dimension object


3)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


4)The setVisible(true) method makes the frame appear on the screen. If you forget to do this, the frame object will exist as an object in memory, but no picture will appear on the screen. Later on in a program, if you call setVisible(false) the frame will become invisible, but the software object will still exist and can be made visible again with setVisible(true).

5)The add() method of Set in Java is used to add a specific element into a Set collection. The function adds the element only if the specified element is not already present in the set else the function return False if the element is already present in the Set.


Java AWT Example

To create simple awt example, you need a frame. There are two ways to create a frame in AWT.
  • By extending Frame class (inheritance)
  • By creating the object of Frame class (association)

AWT Example by Inheritance

Let's see a simple example of AWT where we are inheriting Frame class. Here, we are showing Button component on the Frame.
  1. import java.awt.*;  
  2. class First extends Frame{  
  3. First(){  
  4. Button b=new Button("click me");  
  5. b.setBounds(30,100,80,30);// setting button position  
  6. add(b);//adding button into frame  
  7. setSize(300,300);//frame size 300 width and 300 height  
  8. setLayout(null);//no layout manager  
  9. setVisible(true);//now frame will be visible, by default not visible  
  10. }  
  11. public static void main(String args[]){  
  12. First f=new First();  
  13. }}  

The setBounds(int xaxis, int yaxis, int width, int height) method is used in the above example that sets the position of the awt button.

AWT Example by Association

Let's see a simple example of AWT where we are creating instance of Frame class. Here, we are showing Button component on the Frame.
  1. import java.awt.*;  
  2. class First2{  
  3. First2(){  
  4. Frame f=new Frame();  
  5. Button b=new Button("click me");  
  6. b.setBounds(30,50,80,30);  
  7. f.add(b);  
  8. f.setSize(300,300);  
  9. f.setLayout(null);  
  10. f.setVisible(true);  
  11. }  
  12. public static void main(String args[]){  
  13. First2 f=new First2();  
  14. }}


  15. https://javadevelopmenttricks.blogspot.com/2022/06/java-awt-example-extending-frame-class.html  
Previous
Next Post »