You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Calc.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.demo;
  5. import com.itmill.toolkit.ui.Button;
  6. import com.itmill.toolkit.ui.GridLayout;
  7. import com.itmill.toolkit.ui.Label;
  8. import com.itmill.toolkit.ui.Window;
  9. /**
  10. * <p>
  11. * An example application implementing a simple web-based calculator using IT
  12. * Mill Toolkit. The application opens up a window and places the needed UI
  13. * components (display label, buttons etc.) on it, and registers a button click
  14. * listener for them.
  15. * </p>
  16. *
  17. * <p>
  18. * When any of the buttons are pressed the application finds out which button
  19. * was pressed, figures what that button does, and updates the user interface
  20. * accordingly.
  21. * </p>
  22. *
  23. * @see com.itmill.toolkit.Application
  24. * @see com.itmill.toolkit.ui.Button.ClickListener
  25. */
  26. public class Calc extends com.itmill.toolkit.Application implements
  27. Button.ClickListener {
  28. /** The label used as the display */
  29. private Label display = null;
  30. /** Last completed result */
  31. private double stored = 0.0;
  32. /** The number being currently edited. */
  33. private double current = 0.0;
  34. /** Last activated operation. */
  35. private String operation = "C";
  36. /** Button captions. */
  37. private static String[] captions = // Captions for the buttons
  38. { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", "=",
  39. "C", "+" };
  40. /**
  41. * <p>
  42. * Initializes the application. This is the only method an application is
  43. * required to implement. It's called by the framework and it should perform
  44. * whatever initialization tasks the application needs to perform.
  45. * </p>
  46. *
  47. * <p>
  48. * In this case we create the main window, the display, the grid to hold the
  49. * buttons, and the buttons themselves.
  50. * </p>
  51. */
  52. public void init() {
  53. // Create a new layout for the components used by the calculator
  54. final GridLayout layout = new GridLayout(4, 5);
  55. // Create a new label component for displaying the result
  56. display = new Label(Double.toString(current));
  57. display.setCaption("Result");
  58. // Place the label to the top of the previously created grid.
  59. layout.addComponent(display, 0, 0, 3, 0);
  60. // Create the buttons and place them in the grid
  61. for (int i = 0; i < captions.length; i++) {
  62. final Button button = new Button(captions[i], this);
  63. layout.addComponent(button);
  64. }
  65. // Create the main window with a caption and add it to the application.
  66. addWindow(new Window("Calculator", layout));
  67. }
  68. /**
  69. * <p>
  70. * The button listener method called any time a button is pressed. This
  71. * method catches all button presses, figures out what the user wanted the
  72. * application to do, and updates the UI accordingly.
  73. * </p>
  74. *
  75. * <p>
  76. * The button click event passed to this method contains information about
  77. * which button was pressed. If it was a number, the currently edited number
  78. * is updated. If it was something else, the requested operation is
  79. * performed. In either case the display label is updated to include the
  80. * outcome of the button click.
  81. * </p>
  82. *
  83. * @param event
  84. * the button click event specifying which button was pressed
  85. */
  86. public void buttonClick(Button.ClickEvent event) {
  87. try {
  88. // Number button pressed
  89. current = current * 10
  90. + Double.parseDouble(event.getButton().getCaption());
  91. display.setValue(Double.toString(current));
  92. } catch (final java.lang.NumberFormatException e) {
  93. // Operation button pressed
  94. if (operation.equals("+")) {
  95. stored += current;
  96. }
  97. if (operation.equals("-")) {
  98. stored -= current;
  99. }
  100. if (operation.equals("*")) {
  101. stored *= current;
  102. }
  103. if (operation.equals("/")) {
  104. stored /= current;
  105. }
  106. if (operation.equals("C")) {
  107. stored = current;
  108. }
  109. if (event.getButton().getCaption().equals("C")) {
  110. stored = 0.0;
  111. }
  112. operation = event.getButton().getCaption();
  113. current = 0.0;
  114. display.setValue(Double.toString(stored));
  115. }
  116. }
  117. }