basicapp.rkt 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #lang racket/gui
  2. ; Make a frame by instantiating the frame% class
  3. (define frame (new frame% [label "Example"]))
  4. ; Make a static text message in the frame
  5. (define msg (new message% [parent frame]
  6. [label "No events so far..."]))
  7. ; Make a button in the frame
  8. (new button% [parent frame]
  9. [label "Click Me"]
  10. ; Callback procedure for a button click:
  11. [callback (lambda (button event)
  12. (send msg set-label "Button click"))])
  13. ; Derive a new canvas (a drawing window) class to handle events
  14. (define my-canvas%
  15. (class canvas% ; The base class is canvas%
  16. ; Define overriding method to handle mouse events
  17. (define/override (on-event event)
  18. (send msg set-label "Canvas mouse"))
  19. ; Define overriding method to handle keyboard events
  20. (define/override (on-char event)
  21. (send msg set-label "Canvas keyboard"))
  22. ; Call the superclass init, passing on all init args
  23. (super-new)))
  24. ; Make a canvas that handles events in the frame
  25. (new my-canvas% [parent frame])
  26. ;; make the application sleep for 5 seconds
  27. (new button% [parent frame]
  28. [label "Pause"]
  29. [callback (lambda (button event) (sleep 5))])
  30. (define panel (new horizontal-panel% [parent frame]))
  31. (new button% [parent panel]
  32. [label "Left"]
  33. [callback (lambda (button event)
  34. (send msg set-label "Left click"))])
  35. (new button% [parent panel]
  36. [label "Right"]
  37. [callback (lambda (button event)
  38. (send msg set-label "Right click"))])
  39. ; Show the frame by calling its show method
  40. (send frame show #t)