Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. public class Window extends JFrame {
  6.     JTextField input ;
  7.     JTextArea calculations;
  8.     JButton bEvaluate;
  9.  
  10.     public Window() {
  11.         setSize(800, 600);
  12.         setTitle("Calc");
  13.         setLayout(null);
  14.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.         bEvaluate = new JButton("Make Math Happen!");
  16.         bEvaluate.setBounds(600, 550, 200, 85);
  17.         input = new JTextField();
  18.         input.setBounds(0, 550, 600, 85);
  19.         calculations = new JTextArea();
  20.         calculations.setBounds(0, 0, 600, 450);
  21.         //adding menuBar and stuff
  22.         addMenu();
  23.  
  24.  
  25.         add(calculations);
  26.  
  27.         add(input);
  28.         add(bEvaluate);
  29.  
  30.         bEvaluate.addActionListener(new ActionListener() {
  31.             public void actionPerformed(ActionEvent actionEvent) {
  32.                 double result = calculate(input.getText());
  33.                 calculations.append(input.getText() + "\n");
  34.                 calculations.append("\t \t" + result + "\n");
  35.                 input.setText("");
  36.             }
  37.         });
  38.         setVisible(true);
  39.  
  40.     }
  41.  
  42.     public void addMenu() {
  43.         JMenu menu = new JMenu("options");
  44.         JMenuBar mb = new JMenuBar();
  45.         JMenuItem i1 = new JMenuItem("reset");
  46.         JMenuItem i2 = new JMenuItem("exit");
  47.         menu.add(i1);
  48.         menu.add(i2);
  49.         i1.addActionListener(new ActionListener() {
  50.             public void actionPerformed(ActionEvent actionEvent) {
  51.                 input.setText("");
  52.             }
  53.         });
  54.         i2.addActionListener(new ActionListener() {
  55.             public void actionPerformed(ActionEvent actionEvent) {
  56.                 System.exit(0);
  57.             }
  58.         });
  59.         mb.add(menu);
  60.         this.setJMenuBar(mb);
  61.  
  62.     }
  63.  
  64.     public double calculate(String text) {
  65.         double a = 0;
  66.         double b = 0;
  67.         int lenght = text.length();
  68.         if (text.contains("*")) {
  69.             int i = text.indexOf('*');
  70.             a = Double.parseDouble(text.substring(0, i));
  71.             b = Double.parseDouble(text.substring(i + 1, lenght));
  72.             return a * b;
  73.         } else if (text.contains("/")) {
  74.             int i = text.indexOf('/');
  75.             a = Double.parseDouble(text.substring(0, i));
  76.             b = Double.parseDouble(text.substring(i + 1, lenght));
  77.             return a / b;
  78.         } else if (text.contains("-")) {
  79.             int i = text.indexOf('-');
  80.             a = Double.parseDouble(text.substring(0, i));
  81.             b = Double.parseDouble(text.substring(i + 1, lenght));
  82.             return a - b;
  83.         }
  84.         if (text.contains("+")) {
  85.             int i = text.indexOf('+');
  86.             a = Double.parseDouble(text.substring(0, i));
  87.             b = Double.parseDouble(text.substring(i + 1, lenght));
  88.             return a + b;
  89.         } else {
  90.             return a;
  91.         }
  92.  
  93.  
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement