Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.78 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class Calculator {
  6.  
  7.  
  8.     public static void main(String[] args) {
  9.         EventQueue.invokeLater(new Runnable() {
  10.             public void run() {
  11.                 CalculatorFrame frame = new CalculatorFrame();
  12.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13.                 frame.setVisible(true);
  14.             }
  15.         });
  16.     }
  17. }
  18.  
  19. class CalculatorFrame extends JFrame {
  20.     public CalculatorFrame() {
  21.         setTitle("Calculator");
  22.         CalculatorPanel panel = new CalculatorPanel();
  23.         add(panel);
  24.         pack();
  25.     }
  26. }
  27.  
  28. class CalculatorPanel extends JPanel {
  29.     private static int sub = 0;
  30.     private static int add = 0;
  31.  
  32.     public CalculatorPanel() {
  33.         setLayout(new BorderLayout());
  34.  
  35.         result = 0;
  36.         lastCommand = "=";
  37.         start = true;
  38.  
  39.         operations = new JButton("Сложения/вычитания");
  40.         operations.setEnabled(false);
  41.         add(operations, BorderLayout.SOUTH);
  42.  
  43.         display = new JButton("0");
  44.         display.setEnabled(false);
  45.         add(display, BorderLayout.NORTH);
  46.  
  47.         ActionListener insert = new InsertAction();
  48.         ActionListener command = new CommandAction();
  49.  
  50.         panel = new JPanel();
  51.         panel.setLayout(new GridLayout(4, 4));
  52.  
  53.         addButton("7", insert);
  54.         addButton("8", insert);
  55.         addButton("9", insert);
  56.         addButton("/", command);
  57.  
  58.         addButton("4", insert);
  59.         addButton("5", insert);
  60.         addButton("6", insert);
  61.         addButton("*", command);
  62.  
  63.         addButton("1", insert);
  64.         addButton("2", insert);
  65.         addButton("3", insert);
  66.         addButton("-", command);
  67.  
  68.         addButton("0", insert);
  69.         addButton(".", insert);
  70.         addButton("=", command);
  71.         addButton("+", command);
  72.  
  73.         add(panel, BorderLayout.CENTER);
  74.     }
  75.  
  76.     private void addButton(String label, ActionListener listener) {
  77.         JButton button = new JButton(label);
  78.         button.addActionListener(listener);
  79.         panel.add(button);
  80.     }
  81.  
  82.     private class InsertAction implements ActionListener {
  83.         public void actionPerformed(ActionEvent event) {
  84.             String input = event.getActionCommand();
  85.             if (start) {
  86.                 display.setText("");
  87.                 start = false;
  88.             }
  89.             display.setText(display.getText() + input);
  90.         }
  91.     }
  92.  
  93.     private class CommandAction implements ActionListener {
  94.         public void actionPerformed(ActionEvent event) {
  95.             String command = event.getActionCommand();
  96.             if (start) {
  97.                 if (command.equals("-")) {
  98.                     display.setText(command);
  99.                     start = false;
  100.                 } else lastCommand = command;
  101.             } else {
  102.                 calculate(Double.parseDouble(display.getText()));
  103.                 lastCommand = command;
  104.                 start = true;
  105.             }
  106.         }
  107.     }
  108.  
  109.     public void calculate(double x) {
  110.         if (lastCommand.equals("+")) {
  111.             result += x;
  112.             this.add +=1;
  113.         } else if (lastCommand.equals("-")) {
  114.             result -= x;
  115.             this.sub += 1;
  116.         } else if (lastCommand.equals("*")) {
  117.             result *= x;
  118.         }
  119.         else if (lastCommand.equals("/")) {
  120.             result /= x;
  121.         }
  122.         else if (lastCommand.equals("=")) {
  123.             result = x;
  124.         }
  125.         display.setText("" + result);
  126.         operations.setText(this.add + " / "  + this.sub);
  127.     }
  128.  
  129.     private JButton display;
  130.     private JButton operations;
  131.     private JPanel panel;
  132.     private double result;
  133.     private String lastCommand;
  134.     private boolean start;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement