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 3.6KB

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