0% found this document useful (0 votes)
11 views19 pages

Java Unit II

The document provides an overview of Java Applets, AWT controls, event listeners, menu bars, and layout managers in Java programming. It explains the life cycle of applets, various AWT components like buttons, labels, text fields, and their functionalities, as well as how to handle events with listeners. Additionally, it covers the creation of menu bars and different types of layout managers for arranging components in a user interface.

Uploaded by

ramanshukla2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views19 pages

Java Unit II

The document provides an overview of Java Applets, AWT controls, event listeners, menu bars, and layout managers in Java programming. It explains the life cycle of applets, various AWT components like buttons, labels, text fields, and their functionalities, as well as how to handle events with listeners. Additionally, it covers the creation of menu bars and different types of layout managers for arranging components in a user interface.

Uploaded by

ramanshukla2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)

1. Java Applets

Q: What is an Applet? Explain its life cycle.

A: Applet is a Java program that runs in web browser. It's a subclass of [Link].

Applet Life Cycle Methods:

1. init(): Called once when applet is loaded. Used for initialization.

java

public void init() {

// initialization code

2. start(): Called after init() and whenever applet is revisited. Starts applet execution.

java

public void start() {

// start applet

3. paint(Graphics g): Called to draw/redraw applet content.

java

public void paint(Graphics g) {

[Link]("Hello Applet", 50, 50);

4. stop(): Called when user leaves applet page. Suspends applet execution.

java

public void stop() {

// stop applet

5. destroy(): Called when applet is removed from memory. Final cleanup.

java

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)
public void destroy() {

// cleanup code

Basic Applet Structure:

java

import [Link].*;

import [Link].*;

public class MyApplet extends Applet {

public void init() {

// initialize

public void paint(Graphics g) {

[Link]("Hello World", 20, 20);

HTML to run applet:

html

<applet code="[Link]" width="300" height="300">

</applet>

2. AWT Controls

A. Button

Q: How to create and use Button in AWT?

A: Button is a clickable component.

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)
Main Functions:

java

import [Link].*;

Button btn = new Button("Click Me"); // create button

[Link]("New Label"); // set button text

String text = [Link](); // get button text

[Link](false); // disable button

[Link](true); // enable button

Complete Example:

java

import [Link].*;

import [Link].*;

public class ButtonExample extends Frame implements ActionListener {

Button btn;

ButtonExample() {

btn = new Button("Click Me");

[Link](50, 50, 100, 30);

[Link](this); // add listener

add(btn);

setSize(300, 300);

setLayout(null);

setVisible(true);

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)
}

public void actionPerformed(ActionEvent e) {

[Link]("Clicked!");

B. Label

Q: What is Label? Explain with functions.

A: Label displays non-editable text.

Main Functions:

java

Label lbl = new Label("Enter Name:"); // create label

[Link]("New Text"); // change text

String text = [Link](); // get text

[Link]([Link]); // set alignment (LEFT, CENTER, RIGHT)

Example:

java

Label lbl1 = new Label("Username:");

[Link](50, 50, 100, 20);

add(lbl1);

C. TextField

Q: Explain TextField with main functions.

A: TextField is single-line text input field.

Main Functions:

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)
java

TextField tf = new TextField(); // empty textfield

TextField tf = new TextField("Initial Text"); // with text

TextField tf = new TextField(20); // with columns

[Link]("New Text"); // set text

String text = [Link](); // get text

[Link]('*'); // for password (hide characters)

[Link](false); // make read-only

Example:

java

TextField tf = new TextField();

[Link](150, 50, 150, 20);

add(tf);

D. TextArea

Q: What is TextArea? Main functions.

A: TextArea is multi-line text input area.

Main Functions:

java

TextArea ta = new TextArea(); // empty

TextArea ta = new TextArea("Initial Text"); // with text

TextArea ta = new TextArea(5, 20); // rows, columns

[Link]("New Text"); // set text

String text = [Link](); // get text

[Link]("More text"); // append text

[Link]("Insert", 5); // insert at position

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)

E. Checkbox

Q: Explain Checkbox with functions.

A: Checkbox is a selection control (on/off).

Main Functions:

java

Checkbox cb = new Checkbox("Option 1"); // create checkbox

Checkbox cb = new Checkbox("Option 2", true); // checked by default

[Link](true); // check

[Link](false); // uncheck

boolean isChecked = [Link](); // get state

String label = [Link](); // get label

CheckboxGroup (Radio Buttons):

java

CheckboxGroup cbg = new CheckboxGroup();

Checkbox cb1 = new Checkbox("Male", cbg, true);

Checkbox cb2 = new Checkbox("Female", cbg, false);

F. Choice (Combo Box/Dropdown)

Q: What is Choice? Explain main functions.

A: Choice is dropdown list (combo box) for selecting one option.

Main Functions:

java

Choice choice = new Choice(); // create choice

[Link]("Item 1"); // add item

[Link]("Item 2");

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)
[Link]("Item 3");

[Link]("Item 0", 0); // insert at index

String selected = [Link](); // get selected item

int index = [Link](); // get selected index

[Link](2); // select by index

[Link]("Item 2"); // select by name

[Link](1); // remove item at index

[Link](); // remove all items

int count = [Link](); // get item count

Example:

java

Choice choice = new Choice();

[Link](100, 100, 100, 50);

[Link]("Red");

[Link]("Green");

[Link]("Blue");

add(choice);

G. List

Q: Explain List component with functions.

A: List displays multiple items and allows selection.

Main Functions:

java

List list = new List(); // single selection

List list = new List(5); // visible rows = 5

List list = new List(5, true); // multiple selection enabled

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)

[Link]("Item 1"); // add item

[Link]("Item 2", 0); // add at index

String selected = [Link](); // get selected item

int index = [Link](); // get selected index

String[] items = [Link](); // multiple selection

int[] indices = [Link](); // multiple indices

[Link](2); // select by index

[Link](1); // deselect by index

[Link](0); // remove item

[Link](); // remove all

int count = [Link](); // get count

Example:

java

List list = new List(4, true); // 4 visible rows, multiple selection

[Link](100, 100, 100, 100);

[Link]("Java");

[Link]("Python");

[Link]("C++");

add(list);

3. Event Listeners

Q: What are Event Listeners? Explain main listeners.

A: Listeners handle events (user actions) on components.

A. ActionListener

For: Button, TextField, MenuItem

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)
Main Function:

java

public void actionPerformed(ActionEvent e) {

// handle action

Example:

java

class MyClass implements ActionListener {

Button btn = new Button("Click");

MyClass() {

[Link](this);

public void actionPerformed(ActionEvent e) {

[Link]("Button clicked");

B. ItemListener

For: Checkbox, Choice, List

Main Function:

java

public void itemStateChanged(ItemEvent e) {

// handle item selection

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)
Example:

java

class MyClass implements ItemListener {

Checkbox cb = new Checkbox("Accept");

MyClass() {

[Link](this);

public void itemStateChanged(ItemEvent e) {

if([Link]() == [Link]) {

[Link]("Checked");

C. MouseListener

For: All components

Main Functions:

java

public void mouseClicked(MouseEvent e) { }

public void mousePressed(MouseEvent e) { }

public void mouseReleased(MouseEvent e) { }

public void mouseEntered(MouseEvent e) { }

public void mouseExited(MouseEvent e) { }

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)
D. KeyListener

For: TextField, TextArea

Main Functions:

java

public void keyPressed(KeyEvent e) { }

public void keyReleased(KeyEvent e) { }

public void keyTyped(KeyEvent e) { }

E. WindowListener

For: Frame, Dialog

Main Functions:

java

public void windowOpened(WindowEvent e) { }

public void windowClosing(WindowEvent e) {

[Link](0); // close window

public void windowClosed(WindowEvent e) { }

public void windowActivated(WindowEvent e) { }

public void windowDeactivated(WindowEvent e) { }

4. Menu Bar

Q: How to create MenuBar with Menu and MenuItem?

A: MenuBar contains menus, which contain menu items.

Main Functions:

MenuBar:

java

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)
MenuBar mb = new MenuBar(); // create menubar

setMenuBar(mb); // add to frame

[Link](menu); // add menu

Menu:

java

Menu menu = new Menu("File"); // create menu

[Link](menuItem); // add menu item

[Link](); // add separator line

MenuItem:

java

MenuItem mi = new MenuItem("Open"); // create menu item

[Link](this); // add listener

[Link](false); // disable

Complete Example:

java

import [Link].*;

import [Link].*;

public class MenuExample extends Frame implements ActionListener {

MenuBar mb;

Menu fileMenu, editMenu;

MenuItem openItem, saveItem, exitItem;

MenuItem cutItem, copyItem, pasteItem;

MenuExample() {

// Create MenuBar

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)
mb = new MenuBar();

// Create File Menu

fileMenu = new Menu("File");

openItem = new MenuItem("Open");

saveItem = new MenuItem("Save");

exitItem = new MenuItem("Exit");

[Link](this);

[Link](this);

[Link](this);

[Link](openItem);

[Link](saveItem);

[Link](); // separator line

[Link](exitItem);

// Create Edit Menu

editMenu = new Menu("Edit");

cutItem = new MenuItem("Cut");

copyItem = new MenuItem("Copy");

pasteItem = new MenuItem("Paste");

[Link](cutItem);

[Link](copyItem);

[Link](pasteItem);

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)

// Add menus to menubar

[Link](fileMenu);

[Link](editMenu);

// Set menubar to frame

setMenuBar(mb);

setSize(400, 400);

setVisible(true);

public void actionPerformed(ActionEvent e) {

if([Link]() == exitItem) {

[Link](0);

else if([Link]() == openItem) {

[Link]("Open clicked");

else if([Link]() == saveItem) {

[Link]("Save clicked");

CheckboxMenuItem:

java

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)
CheckboxMenuItem cbmi = new CheckboxMenuItem("Word Wrap");

[Link](true); // check

boolean state = [Link](); // get state

5. Layout Managers

Q: What are Layout Managers? Explain types.

A: Layout Managers arrange components in container automatically.

A. FlowLayout (Default for Panel)

Arranges components left to right, top to bottom.

Main Functions:

java

setLayout(new FlowLayout()); // default (CENTER)

setLayout(new FlowLayout([Link])); // left aligned

setLayout(new FlowLayout([Link])); // right aligned

setLayout(new FlowLayout([Link], 10, 20)); // hgap=10, vgap=20

Example:

java

setLayout(new FlowLayout());

add(new Button("Button 1"));

add(new Button("Button 2"));

add(new Button("Button 3"));

B. BorderLayout (Default for Frame)

Divides container into 5 regions: NORTH, SOUTH, EAST, WEST, CENTER.

Main Functions:

java

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)
setLayout(new BorderLayout());

add(component, [Link]);

add(component, [Link]);

add(component, [Link]);

add(component, [Link]);

add(component, [Link]);

setLayout(new BorderLayout(10, 20)); // hgap=10, vgap=20

Example:

java

setLayout(new BorderLayout());

add(new Button("North"), [Link]);

add(new Button("South"), [Link]);

add(new Button("East"), [Link]);

add(new Button("West"), [Link]);

add(new Button("Center"), [Link]);

C. GridLayout

Arranges components in rows and columns (equal size).

Main Functions:

java

setLayout(new GridLayout(3, 2)); // 3 rows, 2 columns

setLayout(new GridLayout(3, 2, 10, 20)); // with gaps (hgap=10, vgap=20)

Example:

java

setLayout(new GridLayout(3, 2)); // 3x2 grid

add(new Button("1"));

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)
add(new Button("2"));

add(new Button("3"));

add(new Button("4"));

add(new Button("5"));

add(new Button("6"));

D. CardLayout

Shows one component at a time (like cards).

Main Functions:

java

CardLayout card = new CardLayout();

setLayout(card);

add(panel1, "Card1"); // add with name

add(panel2, "Card2");

[Link](container, "Card1"); // show specific card

[Link](container); // show first card

[Link](container); // show last card

[Link](container); // show next card

[Link](container); // show previous card

E. GridBagLayout

Most flexible, complex layout. Allows different sized components.

Main Functions:

java

GridBagLayout gbl = new GridBagLayout();

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)
GridBagConstraints gbc = new GridBagConstraints();

setLayout(gbl);

[Link] = 0; // column position

[Link] = 0; // row position

[Link] = 2; // spans 2 columns

[Link] = 1; // spans 1 row

[Link] = [Link]; // fill space

[Link] = 1.0; // horizontal weight

[Link] = 1.0; // vertical weight

add(component, gbc);

F. No Layout (null)

Absolute positioning using setBounds().

Main Functions:

java

setLayout(null);

Button btn = new Button("Click");

[Link](x, y, width, height); // set position and size

add(btn);

Example:

java

setLayout(null);

Button btn = new Button("Click Me");

[Link](50, 50, 100, 30); // x=50, y=50, w=100, h=30

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA
BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II)
add(btn);

Quick Reference Table

Component Main Methods

Button setLabel(), getLabel(), addActionListener()

Label setText(), getText(), setAlignment()

TextField setText(), getText(), setEchoChar()

TextArea setText(), getText(), append()

Checkbox setState(), getState(), getLabel()

Choice add(), getSelectedItem(), select()

List add(), getSelectedItem(), select()

Menu add(), addSeparator()

MenuItem addActionListener(), setEnabled()

BCA5002 – Java Programming and Dynamic Webpages Design (Unit-II) RAMAN SHUKLA

You might also like