Hi all
can any one help me to design the layout of this calculator and also add one more button
that clears textbox strings one by one instead of whole.
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;
public class Calculator extends JFrame{
double value1;
double value2;
String operator;
double result;
JTextField txt1;
JButton btn1;
JButton btn2;
JButton btn3;
JButton btndivide;
JButton btn4;
JButton btn5;
JButton btn6;
JButton btnmult;
JButton btn7;
JButton btn8;
JButton btn9;
JButton btnminus;
JButton btn0;
JButton btndot;
JButton btnequel;
JButton btnplus;
JButton clear;
public Calculator()
{
super("Calculator");
setLayout(new FlowLayout());
txt1=new JTextField(15);
btn1=new JButton("1");
btn2=new JButton("2");
btn3=new JButton("3");
btndivide=new JButton("/");
btn4=new JButton("4");
btn5=new JButton("5");
btn6=new JButton("6");
btnmult=new JButton("*");
btn7=new JButton("7");
btn8=new JButton("8");
btn9=new JButton("9");
btnminus=new JButton("-");
btn0=new JButton("0");
btndot=new JButton(".");
btnequel=new JButton("=");
btnplus=new JButton("+");
clear=new JButton("CLEAR");
add(txt1);
add(btn1);
add(btn2);
add(btn3);
add(btndivide);
add(btn4);
add(btn5);
add(btn6);
add(btnmult);
add(btn7);
add(btn8);
add(btn9);
add(btnminus);
add(btn0);
add(btndot);
add(btnequel);
add(btnplus);
add(clear);
ButtonHolder holder=new ButtonHolder();
btn1.addActionListener(holder);
btn2.addActionListener(holder);
btn3.addActionListener(holder);
btn4.addActionListener(holder);
btn5.addActionListener(holder);
btn6.addActionListener(holder);
btn7.addActionListener(holder);
btn8.addActionListener(holder);
btn9.addActionListener(holder);
btn0.addActionListener(holder);
btndot.addActionListener(holder);
btnplus.addActionListener(holder);
btnminus.addActionListener(holder);
btnmult.addActionListener(holder);
btndivide.addActionListener(holder);
btnequel.addActionListener(holder);
clear.addActionListener(holder);
}
private class ButtonHolder implements ActionListener {
public void actionPerformed(ActionEvent event)
{
if (event.getSource()==btn1)
{
txt1.setText(txt1.getText()+"1");
}
if (event.getSource()==btn2)
{
txt1.setText(txt1.getText()+"2");
}
if (event.getSource()==btn3)
{
txt1.setText(txt1.getText()+"3");
}
if (event.getSource()==btn4)
{
txt1.setText(txt1.getText()+"4");
}
if (event.getSource()==btn5)
{
txt1.setText(txt1.getText()+"5");
}
if (event.getSource()==btn6)
{
txt1.setText(txt1.getText()+"6");
}
if (event.getSource()==btn7)
{
txt1.setText(txt1.getText()+"7");
}
if (event.getSource()==btn8)
{
txt1.setText(txt1.getText()+"8");
}
if (event.getSource()==btn9)
{
txt1.setText(txt1.getText()+"9");
}
if (event.getSource()==btn0)
{
txt1.setText(txt1.getText()+"0");
}
if (event.getSource()==btndot)
{
if (txt1.getText().contains("."))
{
return;
}
txt1.setText(txt1.getText()+".");
}
if (event.getSource()==btnplus)
{
value1=Double.parseDouble(txt1.getText());
operator="+";
txt1.setText("");
//JOptionPane.showMessageDialog(rootPane, operator);
}
if (event.getSource()==btnequel)
{
value2=Double.parseDouble(txt1.getText());
if (operator=="+")
{
result=value1+value2;
txt1.setText(String.valueOf(result));
}
if (operator=="-")
{
result=value1-value2;
txt1.setText(String.valueOf(result));
}
if (operator=="*")
{
result=value1*value2;
txt1.setText(String.valueOf(result));
}
if (operator=="/")
{
result=value1/value2;
txt1.setText(String.valueOf(result));
}
}
if (event.getSource()==btnminus)
{
value1=Double.parseDouble(txt1.getText());
operator="-";
txt1.setText("");
}
if (event.getSource()==btnmult)
{
value1=Double.parseDouble(txt1.getText());
operator="*";
txt1.setText("");
}
if (event.getSource()==btndivide)
{
value1=Double.parseDouble(txt1.getText());
operator="/";
txt1.setText("");
}
if (event.getSource()==clear)
{
txt1.setText("");
}
}
}
public static void main(String[] args)
{
Calculator cal=new Calculator();
cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
cal.setSize(200,210);
cal.setVisible(true);
}
}