Following Code will make a Calculator:
Code::
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Calci extends Applet implements ActionListener {
Button [] b=new Button[14];
TextField t=new TextField();
String Op="";
int n=0,d=0;
Button eq=new Button("=");
Button cl=new Button("CE");
public void init()
{
setLayout(null);
t.setBounds(50,20,110,20);
int x=0;
int y=0;
for(int i=0;i<10;i++)
{ if(i%4==0 && i!=0)
{
y+=50;
x=0;
}
b[i]=new Button(String.valueOf(i));
b[i].setBounds(x+50,y+50,20,20);
x +=30;
add(b[i]);
b[i].addActionListener(this);
}
String[] s={"+","-","*","/"};
int j=0;
int u=0;
int v=150;
for(int i=10;i<14;i++)
{
b[i]=new Button(s[j++]);
b[i].setBounds(u+50,v+50,20,20);
add(b[i]);
u+=30;
b[i].addActionListener(this);
}
eq.addActionListener(this);
eq.setBounds(110,150,20,20);
add(eq);
cl.addActionListener(this);
cl.setBounds(140,150,20,20);
add(cl);
add(t);
setSize(300,300);
}
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<10;i++)
{
if(e.getSource()==b[i])
{
t.setText(t.getText()+b[i].getLabel());
}
}
for(int i=10;i<14;i++)
{
if(e.getSource()==b[i])
{
Op=b[i].getLabel();
d=Integer.parseInt(t.getText());
t.setText("");
}
}
if(e.getSource()==eq)
{ int r=0;
n=Integer.parseInt(t.getText());
switch(Op)
{
case "+": r=d+n;
break;
case "-": r=d-n;
break;
case "*": r=d*n;
break;
case "/": r=d/n;
break;
}
t.setText(String.valueOf(r));
}
if(e.getSource()==cl)
{
t.setText("");
}
}
public void paint(Graphics g)
{ Image p=getImage(getDocumentBase(),"23.jpg");
g.drawImage(p, 0, 0, this);
g.setFont(new Font("",Font.TYPE1_FONT,25));
g.drawString("By Dishant",50, 280);
}
}
Code::
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Calci extends Applet implements ActionListener {
Button [] b=new Button[14];
TextField t=new TextField();
String Op="";
int n=0,d=0;
Button eq=new Button("=");
Button cl=new Button("CE");
public void init()
{
setLayout(null);
t.setBounds(50,20,110,20);
int x=0;
int y=0;
for(int i=0;i<10;i++)
{ if(i%4==0 && i!=0)
{
y+=50;
x=0;
}
b[i]=new Button(String.valueOf(i));
b[i].setBounds(x+50,y+50,20,20);
x +=30;
add(b[i]);
b[i].addActionListener(this);
}
String[] s={"+","-","*","/"};
int j=0;
int u=0;
int v=150;
for(int i=10;i<14;i++)
{
b[i]=new Button(s[j++]);
b[i].setBounds(u+50,v+50,20,20);
add(b[i]);
u+=30;
b[i].addActionListener(this);
}
eq.addActionListener(this);
eq.setBounds(110,150,20,20);
add(eq);
cl.addActionListener(this);
cl.setBounds(140,150,20,20);
add(cl);
add(t);
setSize(300,300);
}
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<10;i++)
{
if(e.getSource()==b[i])
{
t.setText(t.getText()+b[i].getLabel());
}
}
for(int i=10;i<14;i++)
{
if(e.getSource()==b[i])
{
Op=b[i].getLabel();
d=Integer.parseInt(t.getText());
t.setText("");
}
}
if(e.getSource()==eq)
{ int r=0;
n=Integer.parseInt(t.getText());
switch(Op)
{
case "+": r=d+n;
break;
case "-": r=d-n;
break;
case "*": r=d*n;
break;
case "/": r=d/n;
break;
}
t.setText(String.valueOf(r));
}
if(e.getSource()==cl)
{
t.setText("");
}
}
public void paint(Graphics g)
{ Image p=getImage(getDocumentBase(),"23.jpg");
g.drawImage(p, 0, 0, this);
g.setFont(new Font("",Font.TYPE1_FONT,25));
g.drawString("By Dishant",50, 280);
}
}
Output::