Advertisement
Guest User

javaupdatecalc

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