Docsity

Understanding AWT Components and Layout Managers in Java GUI Development, Slides of Java Programming

Partial preview of the text

Download Understanding AWT Components and Layout Managers in Java GUI Development and more Slides Java Programming in PDF only on Docsity!

ava - programming

An Introduction

Java Short Course

Day-

J

Outline

  • Applets Once again
  • GUI-Components
  • Components hierarchy
  • Packages
  • The Java AWT Package
  • Exploring Component Classes
  • Creating and Using Components
  • Exploring Components Properties
  • Using JBuilder7 design tool

Container-Non-Container Components

JPanel

JPanel

JButton JButton

JTextArea

JFrame

Combination

JPanel

JButton

JLabel

JButton JButton

JTextArea

AWT Components

Swing

Components

Packages

  • Packages  java.awt  javax.swings
  • java.awt pacakge

 Abstract Windowing Kit  GUI-Components are drawn according to the underlying windowing system  Initial set of Components developed  AWT Components  Buttons  Text Fields  Labels  Text Areas  Check Boxes  Menus  Radio Buttons  Canvases  Lists  Scroll Panes  Choices

First Button

  • Two simple steps

 Create a Button

 Add button to the applet

 Don’t Forget to import the awt package.

  • Step-

public Button firstButton = new Button(“ Click It ”);

  • Step-

add(firstButton);

Button applet output

  • Button Applet

Adding few more buttons

import java.awt.*;

import java.applet.*;

public class ButtonApplet extends Applet{

//creating the button component

public Button firstButton = new Button(“One”);

public Button secondButton = new Button(“Two”);

public Button thirdButton = new Button(“Threet”);

public void init( ){

//adding to the applet

add(firstButton); add(secondButton); add(thirdButton); }

}

Why Buttons are in one row?

How to control them?

Layout Managers

  • How your components should be organized

 Layout Managers draw place your components at relative positions

 Absolute position can cause problems

 Necessary for application intended for different platforms

 Layout Managers are classes

 AWT Layout Managers

 Flow Layout  GridLayout  Border Layout  CardLayout  GridBagLayout  Null layout

 So how did the previous applet worked

 The default layout manager

Flow Layout

  • Adds components in a row
  • Starts from left
  • Treats container as set of rows
  • Height of row is defined by the height of components
  • Default alignment is left
  • Can also start from right or center
  • The constructor

 pulic FlowLayout()

 Public FlowLayout(int alignment) //FlowLayout.LEFT

 Public FlowLayout(int alignement,int hgap,vgap)

  • Setting layout
  • Step-

Public FlowLayout myFlowLayout=new FlowLayout();

  • Step-

setLayout(myFlowLayout)

Flow Layout…cont..

  • Example-

 Using Flow Layout constructor 1

import java.awt.; import java.applet.;

public class flowLayoutTest extends Applet{

public FlowLayout myFlowLayout=new FlowLayout(); Button B=new Button(“Click It”);

public void init(){ setLayout(myFlowLayout); add(B); } }