Java AWT Controls in Applet
Java
AWT Controls
AWT stands for
abstract windowing toolkit. It is an API used to create GUI (Graphical User Interface)
or windows based applications in Java. AWT defines windows according to the
class hierarchy that adds functionality and specificity with each level. The
two most common windows are derived from:
1. Panel that is used by the applets
2. Frame that creates a standard window
Panel: It is a container that doesn't have menu
bar or title bars. It can have buttons, checkboxes, textfields like components.
Frame: Frame is a container that has title bar
and menu bars. It also can have other components like texfields, buttons,
checkboxes etc.
Container: It is a subclass of component which contain
other components withing it. For example, textbox, list, buttn, label and many
more.
AWT package
(java.awt.*) provides number of classes to work with. For example: button,
label, textfield, list, textarea, checkbox, menu and many more.
Hierarchy
of AWT classes is shown below:
AWT supports number of
controls which are subclasses of component class. The AWT provides nine basic
non-container component classes from which a user interface may be constructed.
(Of course, new component classes may be derived from any of these or from
class Component itself.) These nine classes are class Button, Canvas, Checkbox,
Choice, Label, List, Scrollbar, TextArea, and TextField. Some of the most
commonly used AWT controls are as follows:
1. Label
2. Button
3. TextField
4. TextArea
5. Checkbox
6. CheckboxGroup
Label: Label is one of the AWT control which is used
for showing text to the user. It contains a string, which it displays. Label
does not have any interaction with the user. The two main methods to get and
set the value of a label are as follows:
1. setText()
2. getText()
Button: Button is the most common AWT control that
generates an event when it is pressed. In order to handle the event we have to
implement ActionListener interface which defines actionPerformed() method
when a button is clicked. These are the commonly used methods with button
class:
1. setLabel()
2. getLabel()
3. actionPerformed()
TextField: It
is a control that allows a user to enter texts, numbers etc in a single line
text area and edit it in the same scope of single line. TextField allow us to
edit the text by moving the arrow keys and performing cut, copy, paste
operations on the text.
TextArea: TextArea is a control that allows editing of
multiple lines of text.
Checkbox: Checkbox consists of a small box that can
either contain checked mark or not. It is used to turn an option on or off by
returning true or false state. We can check multiple checkboxes at a same time.
In order to handle the events on checkbox we have to implement ItemListener interface
which defines itemStateChanged() method when a checkbox
is checked.
There are number of
methods which are used to perform operations on checkboxes. These methods
include:
1. setLabel()
2. getLabel()
3. setText()
4. getText()
5. itemStateChanged()
CheckboxGroup: CheckboxGroup is a group of checkboxes in
which only one checkbox can be checked at a time. It is also referred to as a
radiobutton. In order to do this, we have to define a group to which these
checkboxes will belong to. These groups are object of type CheckBoxGroup.
We can determine that
which checkbox in a group is checked by calling getSelectedCheckbox() method. setSelectedCheckbox() method
is used to set the state of a checkbox.
Types of
containers
The AWT provides four container classes. They are class Window and
its two subtypes -- class Frame and class Dialog -- as well as the Panel class.
In addition to the containers provided by the AWT, the Applet class is a
container -- it is a subtype of the Panel class and can therefore hold
components. Brief descriptions of each container class provided by the AWT are
provided below.
Window
|
A top-level display
surface (a window). An instance of the Window class is not attached to nor
embedded within another container. An instance of the Window class has no
border and no title.
|
Frame
|
A top-level display
surface (a window) with a border and title. An instance of the Frame class
may have a menu bar. It is otherwise very much like an instance of the Window
class.
|
Dialog
|
A top-level display
surface (a window) with a border and title. An instance of the Dialog class
cannot exist without an associated instance of the Frame class.
|
Panel
|
A generic container
for holding components. An instance of the Panel class provides a container
to which to add components.
|
Applet program to add
two numbers enter in text box.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/* <applet
code="sumoftwonumbers.class" width="400"
height="400"></applet> */
public class sumoftwonumbers
extends Applet implements ActionListener{
Label
lbl1, lbl2, lbl3, lbl4;
TextField
txt1, txt2;
Button
btn;
public void init()
{
lbl1=new Label("Enter
first Number:");
lbl2=new Label("Enter
second Number:");
lbl3=new Label("Sum is:");
lbl4=new Label();
txt1=new TextField(20);
txt2=new TextField(20);
btn=new Button("Result");
add(lbl1);
add(txt1);
add(lbl2);
add(txt2);
add(lbl3);
add(lbl4);
add(btn);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent
e)
{
int s1,s2,s3;
if(e.getSource()==btn);
{
s1=Integer.parseInt(txt1.getText());
s2=Integer.parseInt(txt2.getText());
s3=s1+s2;
lbl4.setText(Integer.toString(s3));
}
}
}
Simple
Calculator using Applet
import java.applet.Applet;
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.TextField;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
public class Calculator extends Applet implements ActionListener {
String
s, s1, s2, s3, s4;
Button
b1, b2, b3, b4, b5, b6, b7, b8, b9, b0;
Button
add, sub, eq, cl, mul, div;
TextField
t1;
int a, b, c;
public void init() {
t1 = new TextField(10);
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
b0 = new Button("0");
add = new Button("+");
sub = new Button("-");
mul = new Button("*");
div = new Button("/");
eq = new Button("=");
cl = new Button("Clear");
GridLayout
gb = new GridLayout(5, 5);
setLayout(gb);
add(t1);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b0);
add(add);
add(sub);
add(mul);
add(div);
add(eq);
add(cl);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
eq.addActionListener(this);
cl.addActionListener(this);
}
@Override
public void
actionPerformed(ActionEvent e) {
// TODO Auto-generated
method stub
s = e.getActionCommand();
if (s.equals("0") || s.equals("1") || s.equals("2") || s.equals("3")
||
s.equals("4") || s.equals("5") || s.equals("6")
||
s.equals("7") || s.equals("8") || s.equals("9")
||
s.equals("0")) {
s1 = t1.getText() + s;
t1.setText(s1);
}
if (s.equals("+")) {
s2 = t1.getText();
t1.setText("");
s3 = "+";
}
if (s.equals("-")) {
s2 = t1.getText();
t1.setText("");
s3 = "-";
}
if (s.equals("*")) {
s2 = t1.getText();
t1.setText("");
s3 = "*";
}
if (s.equals("/")) {
s2 = t1.getText();
t1.setText("");
s3 = "/";
}
if (s.equals("=")) {
s4 = t1.getText();
a = Integer.parseInt(s2);
b = Integer.parseInt(s4);
if (s3.equals("+"))
c = a + b;
else if (s3.equals("-"))
c = a - b;
else if (s3.equals("*"))
c = a * b;
else if (s3.equals("/"))
c = a / b;
t1.setText(String.valueOf(c));
}
if (s.equals("Clear")) {
t1.setText("");
}
}
}
Output:
Comments
Post a Comment