java is an platform independent language so we can show it's results in any other platforms here we are going see how to display java calculator in a applet form
/////////////////////////////////CalculatorPanal.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CalculatorPanal extends JApplet implements ActionListerner
{
public CalculatorPanal()
{
setLayout(new BorderLayout());
display =new JtextField("0");
display.setEdittable(false);
disply.setBackground(color.white);
JPanal p=new JPanal();
p.setLayout(new GridLayout(4,4);
String buttons="789/456*123-0=+";
for(int i=0;i<buttons.length();i++)
addButtons(p,buttons.substring(i,i+1);
add(p,"center");
}
private void addButton(Container c,String s)
{
JButton b=new JBotton(s);
c.add(b);
b.addActionListerner(this);
}
public void actionPerformed(ActionEvent ae)
{
String s=new ae.getActionCommand();
if('0'<=s.charAt(0)&&s.charat(0)<='9'||s.equals(".")
{
if(start) display.setText(s);
else display.setText(display.getText()+s);
start=false;
}
else
{
if(start)
{
if(s.equals("="))
{
display.setText(s);
start=false;
}
else
op=s;
}
else
{
double x=Double.parseDouble(display.getText());
calculator(x);
op=s;
start=true;
}
}
}
public void Calculator(double n)
{
if(op.equals("+") arg += n;
else if(op.equals("-")) arg -+n;
else if(op.equals("*")) arg *=n;
else if(op.equals("/")) arg /=n;
else if(op.equals("=")) arg =n;
display.setText(""+arg);
}
private JTextField display;
private double arg=0;
private String op="=";
private boolean start=true;
}
//////////////////////////////////////////////////////CalculatorApplet.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CalculatorApplet extends JApplet
{
public void init()
{
Container ContentPane = getContentPane();
ContentPane.add(new CalculatorPanel());
}
}
////////////////////////////////////////////////////////Calculator.html
<head><title>calculator</title>
</head>
<body>
<APPLET code="CalculatorApplet.class" width="300" height="300">
</applet>
</body>
No comments:
Post a Comment