Advertisement
KrabPlotva

Untitled

Sep 13th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class Calculator
  6. {
  7. public static void main(String[] args) {
  8. EventQueue.invokeLater(new Runnable()
  9. {
  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. class CalculatorFrame extends JFrame {
  19. public CalculatorFrame() {
  20. setTitle("Calculator");
  21. CalculatorPanel panel = new CalculatorPanel();
  22. add(panel);
  23. pack();
  24. }
  25. }
  26. class CalculatorPanel extends JPanel {
  27. public CalculatorPanel() {
  28. setLayout(new BorderLayout());
  29.  
  30. result = 0;
  31. lastCommand = "=";
  32. start=true;
  33.  
  34. display = new JButton("0");
  35. display.setEnabled(false);
  36. add(display, BorderLayout.NORTH);
  37.  
  38. ActionListener insert = new InsertAction();
  39. ActionListener command = new CommandAction();
  40.  
  41. panel = new JPanel();
  42. panel.setLayout(new GridLayout(4, 4));
  43.  
  44. addButton("7", insert);
  45. addButton("8", insert);
  46. addButton("9", insert);
  47. addButton("/", command);
  48.  
  49. addButton("4", insert);
  50. addButton("5", insert);
  51. addButton("6", insert);
  52. addButton("*", command);
  53.  
  54. addButton("1", insert);
  55. addButton("2", insert);
  56. addButton("3", insert);
  57. addButton("-", command);
  58.  
  59. addButton("0", insert);
  60. addButton(".", insert);
  61. addButton("=", command);
  62. addButton("+", command);
  63.  
  64. add(panel, BorderLayout.CENTER);
  65. }
  66. private void addButton(String label, ActionListener listener) {
  67. JButton button = new JButton(label);
  68. button.addActionListener(listener);
  69. panel.add(button);
  70. }
  71. private class InsertAction implements ActionListener
  72. {
  73. public void actionPerformed(ActionEvent event)
  74. {
  75. String input = event.getActionCommand();
  76. if(start) {
  77. display.setText("");
  78. start = false;
  79. }
  80. display.setText(display.getText() + input);
  81. }
  82. }
  83. private class CommandAction implements ActionListener
  84. {
  85. public void actionPerformed(ActionEvent event)
  86. {
  87. String command = event.getActionCommand();
  88. if(start)
  89. {
  90. if(command.equals("-"))
  91. {
  92. display.setText(command);
  93. start = false;
  94. }
  95. else lastCommand = command;
  96. }
  97. else
  98. {
  99. calculate(Double.parseDouble(display.getText()));
  100. lastCommand = command;
  101. start=true;
  102. }
  103. }
  104. }
  105. public void calculate(double x)
  106. {
  107. if(lastCommand.equals("+")) result += x;
  108. else if(lastCommand.equals("-")) result -= x;
  109. else if(lastCommand.equals("*")) result *= x;
  110. else if(lastCommand.equals("/")) result /= x;
  111. else if(lastCommand.equals("=")) result = x;
  112. display.setText("" + result);
  113. }
  114. private JButton display;
  115. private JPanel panel;
  116. private double result;
  117. private String lastCommand;
  118. private boolean start;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement