GUI Components-Java Network Programming-Lecture Slides, Slides of Java Programming

This lecture is delivered by Prem Vikas at Jaypee Institute of Information Technology University for discussing following points of Java Network Programming: Components, Hierarchy, GUI, Classes, Properties, JBuilder7, AWT, Components, Event, Handling

Typology: Slides

2011/2012

Uploaded on 07/03/2012

aapti
aapti 🇮🇳

4.6

(28)

80 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Programming
Day-04
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download GUI Components-Java Network Programming-Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Java Programming

Day-

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

Packages

  • Packages

 java.awt  jvax.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); Complete Program

import java.awt.*;

import java.applet.*;

public class ButtonApplet extends Applet{ public Button firstButton = new Button(“Click It public void init( ){ 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); }

}

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

 So how did the previous applet worked

 The default layout manager

Using Layout managers

  • Few Simple Steps

 Prepare your container

 Create the container class e.g. applet  Set layout

 Create instance of a Components

 Set Initial conditions e.g. titles, x y position, size etc..

 Add component to the container

  • Setting layout

 The setLayout method

setLayout(Object of a Layout Class)

 Inherited from Applet class

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); } }

Flow layout cont..

  • Example-

 Using a different constructor

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

public class flowLayoutTest extends Applet{

public FlowLayout myFlowLayout=newFlowLayout(FlowLayout.Right);

Button B=new Button(“Click It”);

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

Using Labels

  • String for descriptions or decoration
  • Constructors

 public Label()

 public Label( String text)

 public Label( String text, int alignment)

  • Alignments

 Label.LEFT

 Label.RIGHT

  • Important methods

 coid setText(String text) //to set the label text

 String getText() // to get the label text

 void setAlignment(int Alignment) //set alignment of the label

 public int getAlignment() // get alignment of the label

Labels..

  • Example-

 A label for a button import java.awt.; import java.applet.;

public class flowLayoutTest extends Applet{

public FlowLayout myFlowLayout= newFlowLayout((FlowLayout.Right );

Label label_save=new Label( “Save”); Button B=new Button(“Save”);

public void init(){

setLayout(myFlowLayout); add(label_save); add(B); }}

Using Text Areas

  • Multi-line input component
  • Constructors

 public TextArea()

 Creates an empty text area with default size (9 rows,47 columns)

 public TextArea(Strig initialText)

 Creates a text area and displays the given text

 public TextArea(int numrows,int numcolumns)

 Creates a text area according to the given number of rows and coumns

 public TextArea(String initialText,int numRows,int numcolumns)

  • Important methods

 pubic void appendText(String text)

 Adds text at the end of the current text in the text area

 public void insert(String text,int position)

 Adds text at specified character position

 public void replaceText(String text,int start,int end)

Event Handling

  • Events are raised when

 The state of a component is changed

 A button click  Clicking a check box

 User performs some action on a component

 Writing text to a field  Clicking on the applet surface

  • Handling events

 Interfaces

 action() method

 Depricated method previously used for handling events  Cannot deal all sorts of events  Syntax public boolean action(Event evnt, Object obj){ -------code to perform some actions----------- }