Advertisement
Guest User

Untitled

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