Monday, 26 September 2011

Java program for addition and multiplication of two number in AWT view


The java.awt Package

The java.awt package is the main package of the AWT, or Abstract Windowing Toolkit. It contains classes for graphics, including the Java 2D graphics capabilities introduced in the Java 2 platform, and also defines the basic graphical user interface (GUI) framework for Java. java.awt also includes a number of heavyweight GUI objects, many of which have been superseded by the javax.swing package. java.awt also has a number of important subpackages.
The most important graphics classes in java.awt are Graphics and its Java 2D extension, Graphics2D. These classes represent a drawing surface, maintain a set of drawing attributes, and define methods for drawing and filling lines, shapes, and text. Classes that represent graphics attributes include ColorFontPaintStroke, and Composite. The Image class and Shape interface represent things that you can draw using a Graphics object and the various graphics attributes. Figure shows the graphics classes of this package.


figure


import java.awt.*;
import java.awt.event.*;
class GuiApp extends MouseAdapter
{
static Frame f; //because the frame is used in outside of method.
public void mouseEntered(MouseEvent m) //we can enter in the frame then Color is change
{
f.setBackground(Color.red);
}
public void mouseExited(MouseEvent m) //when we can exit then color same.
{
f.setBackground(Color.yellow);
}
public static void main(String g[])
{
Label l1=new Label("Enter first no:");
Label l2=new Label("Enter second no:");  //lable object is defind.
Label l3=new Label("result:");
TextField tf1 =new TextField(10); //textfeild object is defind.
tf1.setBackground(Color.pink); //set the color of textfeild.
MyKeyListener mkl=new MyKeyListener(); //object of mykeylistener.
tf1.addKeyListener(mkl);//and call then it is type for one.
    TextField tf2 =new TextField(10); //textfeild object is defind.
tf2.setBackground(Color.pink); //set the color of textfeild.
TextField tf3 =new TextField(10); //textfeild object is defind.
tf3.setBackground(Color.pink); //set the color of textfeild.
LogicListener ll=new LogicListener();//logic class ka object.
ll.getData(tf1,tf2,tf3);//data get and pass.
Button b1=new Button("Add"); //create an object of class of button.
b1.setForeground(Color.red); //set color of button.
b1.addActionListener(ll);//registered with button.
Button b2=new Button("multiply"); //create an object of class of button.
b2.setForeground(Color.red); //set color of button.
b2.addActionListener(ll);//registered with button.
f=new Frame("MyGuiApp"); //create an object of frame.
f.setBackground(Color.yellow); //set color of frame.
ClosingListener cl=new ClosingListener(); //object of close class.
f.addWindowListener(cl);//call and pass cl to the source.
GuiApp ga=new GuiApp(); //object of GuiApp.
f.addMouseListener(ga);// call and pass ga in mouse.
FlowLayout f1=new FlowLayout(); //Layout of frame is created.
f.setLayout(f1);//pass it method frame.
Panel p1=new Panel(); //create object of panel class.
p1.setBackground(Color.green); //set color of panel.
p1.add(l1);//add in panel lable L1.
p1.add(tf1);//add Textfeild.
Panel p2=new Panel(); //create object of panel class.
p2.setBackground(Color.green); //set color of panel.
p2.add(l2);//add in panel lable L2.
p2.add(tf2);//add Textfeild.
Panel p3=new Panel(); //create object of panel class.
p3.setBackground(Color.green); //set color of panel.
p3.add(b1);//add in panel button.
p3.add(b2);//add in panel button.
f.add(p1);// add in frame all.
f.add(p2);
f.add(p3);
f.add(l3);
f.add(tf3);
f.setSize(200,200);//set size of frame and we can change at run time.
f.setVisible(true);//visible is true or not.
}
}
class ClosingListener extends WindowAdapter
{ ////extends the class Adapter where all method declear.
public void windowClosing(WindowEvent w) //first create an object of this then call.
{
System.exit(0); //this method is clos the window of GuiAPP.
}
}
class MyKeyListener extends KeyAdapter
{
public void keyTyped(KeyEvent ke)
{
System.out.println("Key is type ...");//this work on text feild then create object and call
}
}
class LogicListener implements ActionListener
{ //logic of class is.
int res;
TextField tf1,tf2,tf3; //varabile defiend and pass it textfield.
public void getData(TextField tf1,TextField tf2,TextField tf3) //return the textfield
{
this.tf1=tf1;//current variable
this.tf2=tf2;
this.tf3=tf3;
}
public void actionPerformed(ActionEvent ae)//this is Action of the method.
{
int a=Integer.parseInt(tf1.getText());//this is the covert the String to integer.
int b=Integer.parseInt(tf2.getText());
if(ae.getActionCommand().equals("Add"))//return the Caption source and eqality with add
{
res=a+b;
}else
{
res=a*b;
}
tf3.setText(String.valueOf(res));//this is return in string result.
}
}

No comments:

Post a Comment