Calculator in JAVA |
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class calculator extends JFrame implements ActionListener
{
public static final int WIDTH=300;
public static final int HEIGTH=200;
public JTextField name;
public static void main(String[] args)
{
calculator gui= new calculator();
gui.setVisible(true);
}
public calculator()
{
super("Calculator");
setSize(WIDTH,HEIGTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel panel1=new JPanel();
panel1.setLayout(new GridLayout(1,1));
JTextField name= new JTextField();
panel1.add(name);
add(panel1, BorderLayout.NORTH);
JPanel buttonpanel= new JPanel();
buttonpanel.setBackground(Color.LIGHT_GRAY);
buttonpanel.setLayout(new GridLayout(4,4));
JButton onebutton= new JButton("1");
onebutton.addActionListener(this);
buttonpanel.add(onebutton);
JButton twobutton= new JButton("2");
twobutton.addActionListener(this);
buttonpanel.add(twobutton);
JButton threebutton= new JButton("3");
threebutton.addActionListener(this);
buttonpanel.add(threebutton);
JButton plusbutton= new JButton("+");
plusbutton.addActionListener(this);
buttonpanel.add(plusbutton);
JButton fourbutton= new JButton("4");
fourbutton.addActionListener(this);
buttonpanel.add(fourbutton);
JButton fivebutton= new JButton("5");
fivebutton.addActionListener(this);
buttonpanel.add(fivebutton);
JButton sixbutton= new JButton("6");
sixbutton.addActionListener(this);
buttonpanel.add(sixbutton);
JButton minusbutton= new JButton("-");
minusbutton.addActionListener(this);
buttonpanel.add(minusbutton);
JButton sevenbutton= new JButton("7");
sevenbutton.addActionListener(this);
buttonpanel.add(sevenbutton);
JButton eightbutton= new JButton("8");
eightbutton.addActionListener(this);
buttonpanel.add(eightbutton);
JButton ninebutton= new JButton("9");
ninebutton.addActionListener(this);
buttonpanel.add(ninebutton);
JButton multibutton= new JButton("*");
multibutton.addActionListener(this);
buttonpanel.add(multibutton);
JButton zerobutton= new JButton("0");
zerobutton.addActionListener(this);
buttonpanel.add(zerobutton);
JButton pointbutton= new JButton(".");
pointbutton.addActionListener(this);
buttonpanel.add(pointbutton);
JButton equalbutton= new JButton("=");
equalbutton.addActionListener(this);
buttonpanel.add(equalbutton);
JButton dividebutton= new JButton("/");
dividebutton.addActionListener(this);
buttonpanel.add(dividebutton);
JButton cbutton= new JButton("C");
cbutton.addActionListener(this);
panel1.add(cbutton);
add(buttonpanel, BorderLayout.CENTER);
}
public void actionPerformed (ActionEvent e)
{
String text= e.getActionCommand();
if(text.equals("1"))
{
name.setText("1");
}
else if(text.equals("2"))
{
name.setText("2");
}
else if(text.equals("3"))
{
name.setText("3");
}
else if(text.equals("4"))
{
name.setText("4");
}
else if(text.equals("5"))
{
name.setText("5");
}
else if(text.equals("6"))
{
name.setText("6");
}
else if(text.equals("7"))
{
name.setText("7");
}
else if(text.equals("8"))
{
name.setText("8");
}
else if(text.equals("9"))
{
name.setText("9");
}
else if(text.equals("0"))
{
name.setText("0");
}
else if(text.equals("."))
{
name.setText(".");
}
else if(text.equals("+"))
{
result=result + stringToDouble(name.getText());
name.setText(Double.toString(result));
}
else if(text.equals("-"))
{
result=result - stringToDouble(name.getText());
name.setText(Double.toString(result));
}
else if(text.equals("*"))
{
result=result * stringToDouble(name.getText());
name.setText(Double.toString(result));
}
else if(text.equals("/"))
{
result=result / stringToDouble(name.getText());
name.setText(Double.toString(result));
}
}
private static double stringToDouble(String stringObject)
{
return Double.parseDouble(stringObject.trim());
}
}
No comments:
Post a Comment