Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- public class Tanvir implements WindowListener, ActionListener {
- String str = "";
- char sign;
- double v1, v2, ans;
- private Frame f;
- private MenuBar mb;
- private Menu edit, help;
- private MenuItem mi1, mi2, mi3, mi4, mi5;
- private Button num0, num1, num2, num3, num4, num5, num6, num7, num8, num9;
- private Button bAdd, bSub, bMul, bDiv, bPer, bSqrt, bFrac, bDot, bCE,
- bEqual, bBackspace, bClear;
- private TextField tf;
- Panel p1, p2, p3, p4, p5;
- public Tanvir() {
- f = new Frame("xE Calculator");
- tf = new TextField(30);
- p1 = new Panel();
- p2 = new Panel();
- p3 = new Panel();
- p4 = new Panel();
- p5 = new Panel();
- mb = new MenuBar();
- edit = new Menu("Edit");
- help = new Menu("Help");
- mi1 = new MenuItem("Copy");
- mi1.setActionCommand("mi1");
- mi2 = new MenuItem("Paste");
- mi2.setActionCommand("mi2");
- mi3 = new MenuItem("Quit");
- mi3.setActionCommand("mi3");
- mi4 = new MenuItem("Help");
- mi4.setActionCommand("mi4");
- mi5 = new MenuItem("About");
- mi5.setActionCommand("mi5");
- edit.add(mi1);
- edit.add(mi2);
- edit.addSeparator();
- edit.add(mi3);
- help.add(mi4);
- help.addSeparator();
- help.add(mi5);
- bCE = new Button(" CE ");
- bCE.setActionCommand("bCE");
- bBackspace = new Button(" Backspace ");
- bBackspace.setActionCommand("bBackspace");
- bClear = new Button(" C ");
- bClear.setActionCommand("bClear");
- num7 = new Button(" 7 ");
- num7.setActionCommand("num7");
- num8 = new Button(" 8 ");
- num8.setActionCommand("num8");
- num9 = new Button(" 9 ");
- num9.setActionCommand("num9");
- bDiv = new Button(" / ");
- bDiv.setActionCommand("bDiv");
- bSqrt = new Button(" sqrt ");
- bSqrt.setActionCommand("bSqrt");
- num4 = new Button(" 4 ");
- num4.setActionCommand("num4");
- num5 = new Button(" 5 ");
- num5.setActionCommand("num5");
- num6 = new Button(" 6 ");
- num6.setActionCommand("num6");
- bMul = new Button(" x ");
- bMul.setActionCommand("bMul");
- bPer = new Button(" % ");
- bPer.setActionCommand("bPer");
- num1 = new Button(" 1 ");
- num1.setActionCommand("num1");
- num2 = new Button(" 2 ");
- num2.setActionCommand("num2");
- num3 = new Button(" 3 ");
- num3.setActionCommand("num3");
- bSub = new Button(" - ");
- bSub.setActionCommand("bSub");
- bFrac = new Button(" 1/x ");
- bFrac.setActionCommand("bFrac");
- num0 = new Button(" 0 ");
- num0.setActionCommand("num0");
- bAdd = new Button(" + ");
- bAdd.setActionCommand("bAdd");
- bDot = new Button(" . ");
- bDot.setActionCommand("bDot");
- bEqual = new Button(" = ");
- bEqual.setActionCommand("bEqual");
- }
- public void launchFrame() {
- f.setMenuBar(mb);
- mb.add(edit);
- mb.add(help);
- f.add(num0);
- f.add(num1);
- f.add(num2);
- f.add(num3);
- f.add(num4);
- f.add(num5);
- f.add(num6);
- f.add(num7);
- f.add(num8);
- f.add(num9);
- f.add(bAdd);
- f.add(bSub);
- f.add(bMul);
- f.add(bDiv);
- f.add(bPer);
- f.add(bCE);
- f.add(bSqrt);
- f.add(bDot);
- f.add(bFrac);
- p1.add(bBackspace);
- p1.add(bCE);
- p1.add(bClear);
- p2.add(num7);
- p2.add(num8);
- p2.add(num9);
- p2.add(bDiv);
- p2.add(bSqrt);
- p3.add(num4);
- p3.add(num5);
- p3.add(num6);
- p3.add(bMul);
- p3.add(bPer);
- p4.add(num1);
- p4.add(num2);
- p4.add(num3);
- p4.add(bSub);
- p4.add(bFrac);
- p5.add(num0);
- p5.add(bDot);
- p5.add(bAdd);
- p5.add(bEqual);
- f.add(tf);
- tf.setText("0");
- tf.setEnabled(false);
- bClear.addActionListener(this);
- bCE.addActionListener(this);
- num0.addActionListener(this);
- num1.addActionListener(this);
- num2.addActionListener(this);
- num3.addActionListener(this);
- num4.addActionListener(this);
- num5.addActionListener(this);
- num6.addActionListener(this);
- num7.addActionListener(this);
- num8.addActionListener(this);
- num9.addActionListener(this);
- bAdd.addActionListener(this);
- bSub.addActionListener(this);
- bMul.addActionListener(this);
- bDiv.addActionListener(this);
- bPer.addActionListener(this);
- bSqrt.addActionListener(this);
- bFrac.addActionListener(this);
- bDot.addActionListener(this);
- bEqual.addActionListener(this);
- bBackspace.addActionListener(this);
- mi1.addActionListener(this);
- mi2.addActionListener(this);
- mi3.addActionListener(this);
- mi3.addActionListener(this);
- mi4.addActionListener(this);
- f.setLayout((new GridLayout(6, 0)));
- f.setSize(300, 250);
- f.add(p1);
- f.add(p2);
- f.add(p3);
- f.add(p4);
- f.add(p5);
- f.setVisible(true);
- }
- public void actionPerformed(ActionEvent e) {
- if (e.getActionCommand() == "num0") {
- str = str + 0;
- tf.setText(str);
- }
- if (e.getActionCommand() == "num1") {
- str = str + 1;
- tf.setText(str);
- }
- if (e.getActionCommand() == "num2") {
- str = str + 2;
- tf.setText(str);
- }
- if (e.getActionCommand() == "num3") {
- str = str + 3;
- tf.setText(str);
- }
- if (e.getActionCommand() == "num4") {
- str = str + 4;
- tf.setText(str);
- }
- if (e.getActionCommand() == "num5") {
- str = str + 5;
- tf.setText(str);
- }
- if (e.getActionCommand() == "num6") {
- str = str + 6;
- tf.setText(str);
- }
- if (e.getActionCommand() == "num7") {
- str = str + 7;
- tf.setText(str);
- }
- if (e.getActionCommand() == "num8") {
- str = str + 8;
- tf.setText(str);
- }
- if (e.getActionCommand() == "num9") {
- str = str + 9;
- tf.setText(str);
- }
- if (e.getActionCommand() == "bDot") {
- str = str + ".";
- tf.setText(str);
- }
- if (e.getActionCommand() == "bAdd") {
- str = "";
- v1 = Double.parseDouble(tf.getText());
- sign = '+';
- tf.setText("");
- }
- if (e.getActionCommand() == "bSub") {
- str = "";
- v1 = Double.parseDouble(tf.getText());
- sign = '-';
- tf.setText("");
- }
- if (e.getActionCommand() == "bMul") {
- str = "";
- v1 = Double.parseDouble(tf.getText());
- sign = '*';
- tf.setText("");
- }
- if (e.getActionCommand() == "bDiv") {
- str = "";
- v1 = Double.parseDouble(tf.getText());
- sign = '/';
- tf.setText("");
- }
- if (e.getActionCommand() == "bSqrt") {
- str = "";
- v1 = Double.parseDouble(tf.getText());
- v1 = Math.sqrt(v1);
- tf.setText(""+v1);
- }
- if (e.getActionCommand() == "bFrac") {
- str = "";
- v1 = Double.parseDouble(tf.getText());
- v1 = 1/v1;
- tf.setText(""+v1);
- }
- if (e.getActionCommand() == "bEqual") {
- v2=Double.parseDouble(tf.getText());
- if(sign == '+'){
- ans=v1+v2;
- tf.setText(" "+ans);
- sign= '=';
- }
- if(sign == '-'){
- ans=v1-v2;
- tf.setText(" "+ans);
- sign= '=';
- }
- if(sign == '*'){
- ans=v1*v2;
- tf.setText(" "+ans);
- sign= '=';
- }
- if(sign == '/'){
- ans=v1/v2;
- tf.setText(" "+ans);
- sign= '=';
- }
- if(sign == '%'){
- ans=(v1 % v2);
- tf.setText(" "+ans);
- sign= '=';
- }
- }
- if (e.getActionCommand() == "mi3") {
- System.exit(0);
- }
- }
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- public void windowOpened(WindowEvent e) {
- }
- public void windowIconified(WindowEvent e) {
- }
- public void windowDeiconified(WindowEvent e) {
- }
- public void windowClosed(WindowEvent e) {
- }
- public void windowActivated(WindowEvent e) {
- }
- public void windowDeactivated(WindowEvent e) {
- }
- public static void main(String[] args) {
- Tanvir t = new Tanvir();
- t.launchFrame();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement