Advertisement
Guest User

Untitled

a guest
May 26th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. package example;
  2.  
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5. import java.awt.BorderLayout;
  6. import java.awt.Color;
  7. import java.awt.GridLayout;
  8.  
  9. public class Calculator extends JFrame
  10. {
  11. private JPanel panel;
  12. private JTextField display;
  13. private JButton[] buttons;
  14. private String[] labels = {
  15. "Backspace", "", "", "CE", "C",
  16. "7", "8", "9", "/", "sqrt",
  17. "4", "5", "6", "x", "%",
  18. "1", "2", "3", "-", "1/x",
  19. "0", "-/+", ".", "+", "=", };
  20. private double result = 0;
  21. private String operator = "=";
  22. private boolean startOfNumber = true;
  23.  
  24. public Calculator() {
  25. display = new JTextField(35);
  26. panel = new JPanel();
  27. display.setText("0.0");
  28. //display.setEnabled(true);
  29.  
  30.  
  31. panel.setLayout(new GridLayout(0, 5, 3, 3));
  32. buttons = new JButton[25];
  33. int index = 0;
  34. for (int rows = 0; rows < 5; rows++)
  35. {
  36. for (int cols = 0; cols < 5; cols++)
  37. {
  38. buttons[index] = new JButton(labels[index]);
  39. if (cols >= 3)
  40. buttons[index].setForeground(Color.red);
  41. else
  42. buttons[index].setForeground(Color.blue);
  43. buttons[index].setBackground(Color.yellow);
  44. panel.add(buttons[index]);
  45.  
  46. buttons[index].addActionListener((e)->
  47. {
  48. String command = e.getActionCommand();
  49. if (command.charAt(0) == 'C')
  50. {
  51. startOfNumber = true;
  52. result = 0;
  53. operator = "=";
  54. display.setText("0.0");
  55. }
  56. else if (command.charAt(0) >= '0' && command.charAt(0) <= '9'|| command.equals("."))
  57. {
  58. if (startOfNumber == true)
  59. display.setText(command);
  60. else
  61. display.setText(display.getText() + command);
  62. startOfNumber = false;
  63. }
  64. else
  65. {
  66. if (startOfNumber)
  67. {
  68. if (command.equals("-"))
  69. {
  70. display.setText(command);
  71. startOfNumber = false;
  72. }
  73. else
  74. operator = command;
  75. }
  76. else
  77. {
  78. double x = Double.parseDouble(display.getText());
  79. calculate(x);
  80. operator = command;
  81. startOfNumber = true;
  82. }
  83. }
  84. });
  85. index++;
  86. }
  87. }
  88. add(display, BorderLayout.NORTH);
  89. add(panel, BorderLayout.CENTER);
  90. setVisible(true);
  91. pack();
  92. }
  93.  
  94. private void calculate(double n)
  95. {
  96. if (operator.equals("+"))
  97. result += n;
  98. else if (operator.equals("-"))
  99. result -= n;
  100. else if (operator.equals("*"))
  101. result *= n;
  102. else if (operator.equals("/"))
  103. result /= n;
  104. else if (operator.equals("="))
  105. result = n;
  106. display.setText("" + result);
  107. }
  108.  
  109. public static void main(String args[])
  110. {
  111. Calculator s = new Calculator();
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement