Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class Window extends JFrame {
- JTextField input ;
- JTextArea calculations;
- JButton bEvaluate;
- public Window() {
- setSize(800, 600);
- setTitle("Calc");
- setLayout(null);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- bEvaluate = new JButton("Make Math Happen!");
- bEvaluate.setBounds(600, 550, 200, 85);
- input = new JTextField();
- input.setBounds(0, 550, 600, 85);
- calculations = new JTextArea();
- calculations.setBounds(0, 0, 600, 450);
- //adding menuBar and stuff
- addMenu();
- add(calculations);
- add(input);
- add(bEvaluate);
- bEvaluate.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent actionEvent) {
- double result = calculate(input.getText());
- calculations.append(input.getText() + "\n");
- calculations.append("\t \t" + result + "\n");
- input.setText("");
- }
- });
- setVisible(true);
- }
- public void addMenu() {
- JMenu menu = new JMenu("options");
- JMenuBar mb = new JMenuBar();
- JMenuItem i1 = new JMenuItem("reset");
- JMenuItem i2 = new JMenuItem("exit");
- menu.add(i1);
- menu.add(i2);
- i1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent actionEvent) {
- input.setText("");
- }
- });
- i2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent actionEvent) {
- System.exit(0);
- }
- });
- mb.add(menu);
- this.setJMenuBar(mb);
- }
- public double calculate(String text) {
- double a = 0;
- double b = 0;
- int lenght = text.length();
- if (text.contains("*")) {
- int i = text.indexOf('*');
- a = Double.parseDouble(text.substring(0, i));
- b = Double.parseDouble(text.substring(i + 1, lenght));
- return a * b;
- } else if (text.contains("/")) {
- int i = text.indexOf('/');
- a = Double.parseDouble(text.substring(0, i));
- b = Double.parseDouble(text.substring(i + 1, lenght));
- return a / b;
- } else if (text.contains("-")) {
- int i = text.indexOf('-');
- a = Double.parseDouble(text.substring(0, i));
- b = Double.parseDouble(text.substring(i + 1, lenght));
- return a - b;
- }
- if (text.contains("+")) {
- int i = text.indexOf('+');
- a = Double.parseDouble(text.substring(0, i));
- b = Double.parseDouble(text.substring(i + 1, lenght));
- return a + b;
- } else {
- return a;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement