fork download
  1. import scala.swing._
  2. import scala.swing.BorderPanel.Position._
  3. import event._
  4. import java.awt.{ Color, Graphics2D }
  5. import scala.util.Random
  6.  
  7. object SimpleGUI extends SimpleSwingApplication {
  8.  
  9. def top = new MainFrame { // top is a required method
  10. title = "A Sample Scala Swing GUI"
  11.  
  12. // declare Components here
  13. val label = new Label {
  14. text = "I'm a big label!."
  15. font = new Font("Ariel", java.awt.Font.ITALIC, 24)
  16. }
  17. val button = new Button {
  18. text = "Throw!"
  19. foreground = Color.blue
  20. background = Color.red
  21. borderPainted = true
  22. enabled = true
  23. tooltip = "Click to throw a dart"
  24. }
  25. val toggle = new ToggleButton { text = "Toggle" }
  26. val checkBox = new CheckBox { text = "Check me" }
  27. val textField = new TextField {
  28. columns = 10
  29. text = "Click on the target!"
  30. }
  31. val textArea = new TextArea {
  32. text = "initial text\nline two"
  33. background = Color.green
  34. }
  35. val canvas = new Canvas {
  36. preferredSize = new Dimension(100, 100)
  37. }
  38. val gridPanel = new GridPanel(1, 2) {
  39. contents += checkBox
  40. contents += label
  41. contents += textArea
  42. }
  43.  
  44. // choose a top-level Panel and put components in it
  45. // Components may include other Panels
  46. contents = new BorderPanel {
  47. layout(gridPanel) = North
  48. layout(button) = West
  49. layout(canvas) = Center
  50. layout(toggle) = East
  51. layout(textField) = South
  52. }
  53. size = new Dimension(300, 200)
  54. menuBar = new MenuBar {
  55. contents += new Menu("File") {
  56. contents += new MenuItem(Action("Exit") {
  57. sys.exit(0)
  58. })
  59. }
  60. }
  61.  
  62. // specify which Components produce events of interest
  63. listenTo(button)
  64. listenTo(toggle)
  65. listenTo(canvas.mouse.clicks)
  66.  
  67. // react to events
  68. reactions += {
  69. case ButtonClicked(component) if component == button =>
  70. val x = Random.nextInt(100)
  71. val y = Random.nextInt(100)
  72. val c = new Color(Random.nextInt(Int.MaxValue))
  73. canvas.throwDart(new Dart(x, y, c))
  74. textField.text = s"Dart thrown at $x, $y"
  75. case ButtonClicked(component) if component == toggle =>
  76. toggle.text = if (toggle.selected) "On" else "Off"
  77. case MouseClicked(_, point, _, _, _) =>
  78. canvas.throwDart(new Dart(point.x, point.y, Color.black))
  79. textField.text = (s"You clicked in the Canvas at x=${point.x}, y=${point.y}.")
  80. }
  81. }
  82. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.scala:35: error: not found: type Canvas
    val canvas = new Canvas {
                     ^
Main.scala:49: error: type mismatch;
 found   : AnyRef
 required: scala.swing.Component
      layout(canvas) = Center
             ^
Main.scala:65: error: value mouse is not a member of AnyRef
    listenTo(canvas.mouse.clicks)
                    ^
Main.scala:73: error: value throwDart is not a member of AnyRef
        canvas.throwDart(new Dart(x, y, c))
               ^
Main.scala:73: error: not found: type Dart
        canvas.throwDart(new Dart(x, y, c))
                             ^
Main.scala:78: error: value throwDart is not a member of AnyRef
        canvas.throwDart(new Dart(point.x, point.y, Color.black))
               ^
Main.scala:78: error: not found: type Dart
        canvas.throwDart(new Dart(point.x, point.y, Color.black))
                             ^
7 errors found
Usage: scalac <options> <source files>
where possible standard options include:
  -Dproperty=value                Pass -Dproperty=value directly to the runtime system.
  -J<flag>                        Pass <flag> directly to the runtime system.
  -P:<plugin>:<opt>               Pass an option to a plugin
  -X                              Print a synopsis of advanced options.
  -bootclasspath <path>           Override location of bootstrap class files.
  -classpath <path>               Specify where to find user class files.
  -d <directory|jar>              destination for generated classfiles.
  -dependencyfile <file>          Set dependency tracking file.
  -deprecation                    Emit warning and location for usages of deprecated APIs.
  -encoding <encoding>            Specify character encoding used by source files.
  -explaintypes                   Explain type errors in more detail.
  -extdirs <path>                 Override location of installed extensions.
  -feature                        Emit warning and location for usages of features that should be imported explicitly.
  -g:<level>                      Set level of generated debugging info. (none,source,line,vars,notailcalls) default:vars
  -help                           Print a synopsis of standard options
  -javabootclasspath <path>       Override java boot classpath.
  -javaextdirs <path>             Override java extdirs classpath.
  -language:<_,feature,-feature>  Enable or disable language features: `_' for all, `-language:help' to list
  -no-specialization              Ignore @specialize annotations.
  -nobootcp                       Do not use the boot classpath for the scala jars.
  -nowarn                         Generate no warnings.
  -optimise                       Generates faster bytecode by applying optimisations to the program
  -print                          Print program with Scala-specific features removed.
  -sourcepath <path>              Specify location(s) of source files.
  -target:<target>                Target platform for object files. All JVM 1.5 targets are deprecated. (jvm-1.5,jvm-1.6,jvm-1.7,jvm-1.8) default:jvm-1.6
  -toolcp <path>                  Add to the runner classpath.
  -unchecked                      Enable additional warnings where generated code depends on assumptions.
  -uniqid                         Uniquely tag all identifiers in debugging output.
  -usejavacp                      Utilize the java.class.path in classpath resolution.
  -usemanifestcp                  Utilize the manifest in classpath resolution.
  -verbose                        Output messages about what the compiler is doing.
  -version                        Print product version and exit.
  @<file>                         A text file containing compiler arguments (options and source files)

spoj: The program compiled successfully, but Main.class was not found.
      Class Main should contain method: def main(args: Array[String]).
stdout
Standard output is empty