• No results found

13_Awt controls in java.pptx

N/A
N/A
Protected

Academic year: 2020

Share "13_Awt controls in java.pptx"

Copied!
40
0
0

Loading.... (view fulltext now)

Full text

(1)

AWT controls

(2)

AWT Controls and event listeners

(3)

AWT Controls and event listeners

(4)

Label

A label is an object of type Label, and it contains a string, which it displays. Labels are passive controls that do not support any interaction with the user.

Label defines the following constructors:

Label( )

Label(String str)

Label(String str, int how)

The value of how

Label.LEFT,

Label.RIGHT, or

Label.CENTER.

(5)

Label Methods

setText()

getText()

void setAlignment(int how)

int getAlignment( )

(6)

Buttons

A

push button is a component that

contains a label

and that generates an event when it is pressed.

Push buttons are objects of type

Button.

Button defines these two constructors:

void setLabel(String str)

String getLabel( )

(7)

Problem

Design a window/applet in which there are

three buttons and whenever anyone of

them is clicked a message is generated

informing which button is clicked

(8)

public class ButtonDemo extends Applet implements ActionListener {

String msg = "";

Button yes, no, maybe;

public void init() {

yes = new Button("Yes"); no = new Button("No");

maybe = new Button("Undecided"); add(yes); add(no); add(maybe); yes.addActionListener(this); no.addActionListener(this); maybe.addActionListener(this); } public void

actionPerformed(ActionEvent ae) {

String str = ae.getActionCommand(); if(str.equals("Yes")) {

msg = "You pressed Yes."; }

else if(str.equals("No")) { msg = "You pressed No.";

} else {

msg = "You pressed Undecided."; }

repaint(); }

public void paint(Graphics g) {

g.drawString(msg, 6, 100);

} }

(9)

TextField

• TextField class implements a single-line text-entry area, usually called an edit control.

Text fields allow the user to enter strings and to edit the text using the arrow keys, cut and paste keys, and mouse

selections.

constructors:

• TextField( )

• TextField(int numChars)

• TextField(String str)

• TextField(String str, int numChars)

(10)

TextField Methods

String getText( )

void setText(String str)

String getSelectedText( )

void select(int startIndex, int endIndex)

void setEditable(boolean canEdit)

boolean isEditable( )

void setEchoChar(char ch)

boolean echoCharIsSet( )//is it in echo mode

char getEchoChar( )

(11)

problem

Design a login form where username and pwd

must be same for a successful login.

(12)

Check Boxes

A

check box is a control that is used to turn an option

on or off

Checkbox supports these constructors:

Checkbox( )

Checkbox(String str)

Checkbox(String str, boolean on)

Checkbox(String str, boolean on, CheckboxGroup cbGroup) Checkbox(String str, CheckboxGroup cbGroup, boolean on)

(13)

Methods for checkboxes

boolean getState( )

void setState(boolean on)

String getLabel( )

void setLabel(String str)

Event handling

Ckbox.addItemListener(this)//register listener

public void itemStateChanged(ItemEvent ie)

//handler method

(14)

Problem

Design a window like this either using an

applet or a Frame and record the event

when ever a change occurs in the state of

the checkbox.

(15)

public class CheckboxDemo extends Applet implements ItemListener {

String msg = "";

Checkbox Car, Bike, Laptop, Guitar; public void init() {

Car = new Checkbox("Car", null, true); Bike = new Checkbox("Bike");

Laptop = new Checkbox("Laptop"); Guitar = new Checkbox("Guitar"); add(Car); add(Bike); add(Laptop); add(Guitar); Car.addItemListener(this); Bike.addItemListener(this); Laptop.addItemListener(this); Guitar.addItemListener(this); } 15

public void itemStateChanged(ItemEvent ie) { repaint();

}

// Display current state of the check boxes. public void paint(Graphics g) {

msg = "Current state: "; g.drawString(msg, 6, 80);

msg = " Car: " + Car.getState(); g.drawString(msg, 6, 100);

msg = " Bike: " + Bike.getState(); g.drawString(msg, 6, 120);

msg = " Laptop: " + Laptop.getState(); g.drawString(msg, 6, 140);

msg = " Guitar: " + Guitar.getState(); g.drawString(msg, 6, 160);

(16)

CheckboxGroup

It is possible to create a set of mutually exclusive

check boxes in which one and only one check box

in the group can be checked at any one time. These

check boxes are often called

radio buttons

How to create

first define the group to which they will belong and

then specify that group when you construct the

check boxes. Check box groups are objects of type

CheckboxGroup

(17)

Which radio button is selected?

Call following methods on checkbox group object

Checkbox getSelectedCheckbox( )

void setSelectedCheckbox(Checkbox which)

Event handling

Ckbox.addItemListener(this)//register listener

public void itemStateChanged(ItemEvent ie)

//handler method

(18)

Problem

Design a window like this and record

which option is selected whenever a

state change occurs in the group

(19)

public class CheckboxGroupDemo extends Applet implements ItemListener

{

String msg = "";

Checkbox Car, Bike, Laptop, Guitar;

CheckboxGroup cbg;

public void init() {

cbg=new CheckboxGroup();

Car = new Checkbox("Car", cbg, true); Bike = new Checkbox("Bike",cbg,false);

Laptop = new Checkbox("Laptop",cbg,false); Guitar = new Checkbox("Guitar",cbg,false); add(Car); add(Bike); add(Laptop); add(Guitar); Car.addItemListener(this); Bike.addItemListener(this); Laptop.addItemListener(this); Guitar.addItemListener(this); } 19

public void itemStateChanged(ItemEvent ie) {

repaint(); }

public void paint(Graphics g) {

msg = "Current Selection "; g.drawString(msg, 6, 80);

msg=cbg.getSelectedCheckbox().getLabel();

g.drawString(msg, 6, 100); }

(20)

Choice

The Choice class is used to create a

pop-up list of

items from which the user may

choose.

When inactive, a Choice component takes up only

enough space to show the currently selected item.

When the user clicks on it, the whole list of choices

pops up, and a new selection can be made

Choice only defines the default constructor, which

creates an empty list.

To add a selection to the list

void addItem(String name)

void add(String name)

(21)

Choice methods

String getSelectedItem( )

int getSelectedIndex( )

int getItemCount( )

void select(int index)

void select(String name)

String getItem(int index)

(22)

Problem

Design a window/applet in which u ask

the user to select his/her branch and

Sem.

(23)

public class ChoiceDemo extends Applet implements ItemListener { Choice branch, sem;

String msg = ""; public void init() {

branch = new Choice(); sem = new Choice();

// add items to branch list branch.add("CSE");

branch.add("IT"); branch.add("ECE"); branch.add("ME");

// add items to sem list sem.add("1");

sem.add("3"); sem.add("5"); sem.add("7");

// add choice lists to window add(branch);

add(sem);

// register to receive item events branch.addItemListener(this); sem.addItemListener(this); }

23 public void

itemStateChanged(ItemEvent ie) {

repaint(); }

public void paint(Graphics g) { msg = "Current Branch ";

msg

+=

branch.getSelectedItem(); g.drawString(msg, 6, 80);

msg = "Current Sem ";

msg += sem.getSelectedItem(); g.drawString(msg, 6, 100);

(24)

Lists

• The List class provides a compact, multiple-choice, scrolling selection list.

• It can be used to show any number of choices in the visible window.

• It can also used for multiple selections.

List provides these constructors:

List( )

List(int numRows)// no of visible entries List(int numRows, boolean multipleSelect)

Adding items to list

• void add(String name)

• void add(String name, int index)

(25)

List methods

For single selection

• String getSelectedItem( ) • int getSelectedIndex( )

for multiple selection

• String[ ] getSelectedItems( ) • int[ ] getSelectedIndexes( )

• int getItemCount( ) • void select(int index)

25

Sachin Tendulkar Yuvi

(26)

Scroll bars

Scroll bars are used to select continuous values between a specified minimum and maximum.

• Scroll bars may be oriented horizontally or vertically.

Constructors

Scrollbar( ) //Scrollbar.VERTICAL

Scrollbar(int style) //Scrollbar.VERTICAL | HORIZONTALScrollbar(int style, int initialValue, int thumbSize, int

min, int max)

(27)

Scrollbar methods

• void setValues(int initialValue, int thumbSize, int min, int max)

• int getValue( )

• void setValue(int newValue)

• int getMinimum( )

• int getMaximum( )

• void setUnitIncrement(int newIncr)//1

• void setBlockIncrement(int newIncr)//10 page up

(28)

ScrollBar Event handling

Listener

AdjustmentListener

Method

Public void adjustmentValueChanged(AdjustmentEvent ae)

{

}

(29)

Problem

Create an applet in which we have three scollbars

each one for the R G and B component of color. On

the basis of the values adjusted by these scrollbar

change the Background Color of applet

(30)

public class SBColor extends Applet implements AdjustmentListener {

int r,g,b;

Scrollbar redSB, greenSB, blueSB;

public void init() {

/orientation,initVal,ThumbSize,min,max

redSB = new Scrollbar(Scrollbar.HORIZONTAL,0, 5, 0, 255); greenSB = new Scrollbar(Scrollbar.HORIZONTAL,0, 5, 0, 255); blueSB = new Scrollbar(Scrollbar.HORIZONTAL,0, 5, 0, 255); add(redSB); add(greenSB); add(blueSB); redSB.addAdjustmentListener(this); greenSB.addAdjustmentListener(this); blueSB.addAdjustmentListener(this); }

public void adjustmentValueChanged(AdjustmentEvent ae) { r=redSB.getValue(); g=greenSB.getValue(); b=blueSB.getValue(); repaint(); } 30

public void paint(Graphics gg)

{ setBackground(new Color(r,g,b));

(31)

TextArea

• Multiline text Field

Following are the constructors for TextArea:

• TextArea( )

• TextArea(int numLines, int numChars)

• TextArea(String str)

• TextArea(String str, int numLines, int numChars)

• TextArea(String str, int numLines, int numChars, int sBars)

numLines specifies the height, in lines, of the text area, and

numChars specifies its width, in characters.

sBars must be one of these values:

SCROLLBARS_BOTHSCROLLBARS_NONE

SCROLLBARS_HORIZONTAL_ONLYSCROLLBARS_VERTICAL_ONLY

(32)

Methods

getText( ),

setText( ),

getSelectedText( ),

select( ),

isEditable( )

setEditable( )

void append(String str)

void insert(String str, int index)

void replaceRange(String str, int startIndex, int

endIndex)

(33)

Menu

33

(Swings)

JMenuBar

JMenu menu, submenu;

JMenuItem

(34)

Parts of menu System

34

MenuBar

Menu

Menu Items

Sub Menu

(35)

Add a MenuBar to the Frame

MenuBar mbar=new MenuBar()

setMenuBar(mbar)

Add menu to menu bar

Add(menu)

(36)

Menu

A Menu object is a pull-down menu component that is deployed from a

menu bar.

• A menu can optionally be a tear-off menu. A tear-off menu can be

opened and dragged away from its parent menu bar or menu. It remains on the screen after the mouse button has been released.

Menu constructors

Menu( )

• Menu(String optionName)

Menu(String optionName, boolean removable)

Imp methods

add(MenuItem mi)  Adds the specified menu item to this menu.

add(String label) Adds an item with the specified label to this menu.  

addSeparator() Adds a separator line, or a hypen, to the menu at the current position.

(37)

Menu Item

Constructor Summary

MenuItem ()

 Constructs a new MenuItem with an empty label and no keyboard shortcut. • MenuItem(String label)

 Constructs a new MenuItem with the specified label and no keyboard shortcut. • MenuItem (String label, Menu Shortcut s)

Create a menu item with an associated keyboard shortcut.

Checkbox MenuItem Constructor Summary

CheckboxMenuItem()

 Create a check box menu item with an empty label. • CheckboxMenuItem (String label)

 Create a check box menu item with the specified label. • CheckboxMenuItem (String label, boolean state)

      Create a check box menu item with the specified label and state.

(38)

Menu and menu items

• Individual menu items are of type MenuItem.

It defines these constructors:

• MenuItem( )

• MenuItem(String itemName)

• MenuItem(String itemName, MenuShortcut keyAccel)

Methods

• setEnable(boolean)

• isEnable()

• void setLabel(String newName)

• String getLabel( )

(39)

Problem

Design a frame like this in which,

which ever color the user selects from

the Background menu it becomes the

background color of the frame

(40)

class MenuTest extends Frame implements ActionListener {MenuBar mbar; MenuTest() { setTitle("MenuTest"); mbar=new MenuBar(); setMenuBar(mbar); Background=new Menu("Background"); MenuItem r,g,b; r=new MenuItem("Red"); g=new MenuItem("Green"); b=new MenuItem("Blue"); Background.add(r); Background.add(g); Background.add(b); mbar.add(Background); r.addActionListener(this); About=new Menu("About"); mbar.add(About); setSize(200,200); setVisible(true); } 40 public void actionPerformed(ActionEvent ae) { if (ae.getActionCommand().equals("Red")) { setBackground(Color.red); repaint();} }

public static void main(String args[]) {

new MenuTest(); }

References

Related documents

When you’re recording an electric bass guitar, blending a direct injection (DI) line recording with a mic’d cabinet is the safest way to make sure you’re going to get the tone

Users have the option to select a line item by checking the box to the right of the item and choosing an option from this

As in the device discovery phase nodes acquire a symmetric knowledge of their one and two-hop neighbors (i.e., if a node u knows its one (two)-hop neighbor v, v also knows that u is

For this code to question, evaluate formula cells and durable custom formatting to generated excel file using Apache POI library with examples.. Here so throw a basic Java example

In this tutorial we will create a simple chat program using two users One user will be chatting from the server side whilst the other user would.. Thanks for contributing an answer

• Select the Connect To menu and RIGHT click on the Globalstar Packet Modem menu item.. • Left click on the Create

However, we suggest that policy claims regarding the links between microfinance, women’s empowerment, poverty reduction, and sustainable development warrant systematic

Unpublishing a menu item: click on the Menu Manager icon from the control panel.. In the published column of the appropriate menu item click on the green check mark to change it to