0% found this document useful (0 votes)
21 views22 pages

AWTcontrols

Advance Java Concepts

Uploaded by

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

AWTcontrols

Advance Java Concepts

Uploaded by

saritha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

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
• 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

Method Description
public void add(Component c) inserts a component on this
component.
public void setSize(int width,int sets the size (width and height) of
height) the component.
public void defines the layout manager for the
setLayout(LayoutManager m) component.
public void setVisible(boolean changes the visibility of the
status) component, by default false.
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)
import java.awt.*;
class First extends Frame{
First(){
Button b=new Button("click me");
b.setBounds(30,100,80,30);// setting button position
add(b);//adding button into frame
setSize(300,300);//frame size 300 width and 300 height
setLayout(null);//no layout manager
setVisible(true);//now frame will be visible, by default not visible
}
public static void main(String args[]){
First f=new First();
}}
User Interface-AWT CONTROLS
LABEL
• A label is a object of type label.
• It is a passive control that does not support any interaction with user.
Constructors
Label l=new Label()
Creates a blank label.
Label l=new Label(String str)
Creates a label with specified string ,and it is by default left justified.
Label l=new Label(String str,int how)
Creates a label with specified string ,and str alignment is specified by
how.
how= Label.LEFT,
Label.RIGHT
Label.CENTER
Methods
• void setText(String str)-set or change text in the label.
• String getText()-returns the string of the label.
• void setAlignment (int how)-aligns the label to the position specified
by how.
• int getAlignment()-returns the current alignment of the label.
import java.awt.*;
import java.applet.*;
/*<applet code="LabelDemo" width=300 height=300>
</applet>*/
public class LabelDemo extends Applet
{
public void init()
{
Label l1=new Label("OK");
Label l2=new Label("CANCEL");
add(l1);
add(l2);
} }
BUTTONS
• The widely used control is push button.
• It contains a label and generated an event when it is pressed.
• Push buttons are the type of Buttons.
Constructors
Button b=new Button()
-creates an empty button.
Button b=new Button(String str)
-creates a button that contains the specified string as label
Methods
void setLabel(String str)-sets the label on the button
• String getLabel()-returns the current label of the button.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="ButtonDemo" width=300 height=300>
</applet>*/
public class ButtonDemo extends Applet implements ActionListener
{
String msg="";
public void init()
{
Button b1=new Button("ok");
Button b2=new Button("cancel");
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("ok"))
msg="you pressed ok";
else if (str.equals("cancel"))
msg="you pressed cancel";
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,8,100);
}
}
CHECKBOX
• It is basically a small box which can be turned off or on.
• Each box consists of a label associated withit and represents the
action.
• We can change the state of the check box by clicking on it.
• Checkbox can be used individually or part of a group.
• Checkboxes are objects of checkbox class.
Constructors
• Checkbox c=new Checkbox()
-creates checkbox whose label is blank and state is unchecked
• Checkbox c=new Checkbox(String str)
-creates checkbox whose label is specified by string and state is
unchecked.
• Checkbox c=new Checkbox(String str, Boolean on)
-creates checkbox whose label is specified by string and state is
is set by on.
Methods
• boolean getState()-returns the current state of the checkbox
• void setState(Boolean on)-sets the State of the Checkbox
• String getLabel()-returns the label of checkbox.
• void setLabel(String str)-set the label of the checkbox.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="CheckboxDemo" width=300 height=300>
</applet>*/
public class CheckboxDemo extends Applet implements ItemListener {
String msg="";
Checkbox c1,c2,c3;
public void init() {
c1=new Checkbox("cse");
c2=new Checkbox("ece");
c3=new Checkbox("eee");
add(c1);
add(c2);
add(c3);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
}
public void itemStateChanged(ItemEvent a)
{
repaint();
}
public void paint(Graphics g)
{
msg=c1.getLabel()+" "+c1.getState();
g.drawString(msg,5,50);
msg=c2.getLabel()+" "+c2.getState();
g.drawString(msg,20,100);
msg=c3.getLabel()+" "+c3.getState();
g.drawString(msg,30,150);
}
}
CHECKBOXGROUP(RADIO BUTTONS)
• It allows you to select one and only one selection at a time
• This are also called radio buttons.
• To create a set of mutually exclusive checkboxes, you must first define
the group to which they belong and specify the group when you
construct checkbox.
Constructors
• Checkbox c=new Checkbox(String str,boolen on,checkboxGroup
cbgrp)
Creates a checkbox group with specified string and default on
Methods
• Checkbox getSelectedCheckbox()-determines which checkbox in a group is selected.
• Void setSelectedCheckbox(Checkbox which) -sets the checkbox by calling getSelected.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="CheckboxGroupDemo" width=300 height=300>
</applet>*/
public class CheckboxGroupDemo extends Applet implements ItemListener {
String msg="";
Checkbox c1,c2,c3;
CheckboxGroup cbg;
public void init() {
cbg=new CheckboxGroup();
c1=new Checkbox("cse",cbg,true);
c2=new Checkbox("ece",cbg,false);
c3=new Checkbox("eee",cbg,false);
add(c1);
add(c2);
add(c3);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
}
public void itemStateChanged(ItemEvent a)
{
repaint();
}
public void paint(Graphics g)
{
msg=cbg.getSelectedCheckbox().getLabel();
g.drawString(msg,5,50);
}
}
CHOICE CONTROLS
It is used to creates pop-up list of items from which user may choose
one.
Constructors
Choice c=new Choice()
-creates only a default constructor which creates an empty list
Methods
• void add(String name)-add item to the list in the order in which add
occur.
• String getSelectedItem()-returns the currently selected item.
• Int getSelectedIndex()-returns the index of the item.
• Int getItemCount()-counts the no. of items in the list.
• String getItem(int index)-returns the name of the item associated with item.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="ChoiceDemo" width=300 height=300>
</applet>*/
public class ChoiceDemo extends Applet implements ItemListener {
String msg="";
Choice c1,c2;
public void init() {
c1=new Choice();
c2=new Choice();
c1.add("cse");
c1.add("ece");
c2.add("mech");
c2.add("civil");
add(c1);
add(c2);
c1.addItemListener(this);
c2.addItemListener(this);
}
public void itemStateChanged(ItemEvent a)
{
repaint();
}
public void paint(Graphics g)
{
msg=c1.getSelectedItem();
g.drawString(msg,5,50);
msg=c2.getSelectedItem();
g.drawString(msg,20,50);
}
}

You might also like