Scala Swing Scala uses Swing n n n
- Slides: 8
Scala Swing
Scala uses Swing n n n Java Swing is a full-featured GUI system Scala is built on top of Java (well, actually, the JVM) Scala introduces some very minor simplifications in the way GUIs are constructed and used, but basically, it’s just Swing 2
Example GUI 3
Example n package simple. GUI import scala. swing. _ scala. swing. Border. Panel. Position. _ event. _ java. awt. { Color, Graphics 2 D } object S 1 mple. GUI extends Simple. Swing. Application { def top = new Main. Frame { // top is a required method title = "A Sample Scala Swing GUI" val label = new Label { text = "I'm a big label!. " font = new Font("Ariel", java. awt. Font. ITALIC, 24) } 4
Example, continued n val button = new Button { text = "Don't click!" foreground = Color. blue background = Color. red border. Painted = true enabled = true tooltip = "Don't touch me!" } val toggle = new Toggle. Button { text = "Toggle" } val check. Box = new Check. Box { text = "Check me" } val text. Field = new Text. Field { columns = 10 text = "Click on the target!" } val text. Area = new Text. Area { text = "initial textnline two" background = Color. green } 5
Example, continued n val canvas = new Canvas { preferred. Size = new Dimension(100, 100) } contents = new Border. Panel { layout(label) = North layout(button) = West layout(canvas) = Center layout(toggle) = East layout(text. Field) = South } size = new Dimension(300, 200) menu. Bar = new Menu. Bar { contents += new Menu("File") { contents += new Menu. Item(Action("Exit") { sys. exit(0) }) } } 6
Example, continued listen. To(button) listen. To(toggle) listen. To(canvas. mouse. clicks) n reactions += { case Button. Clicked(component) if component == button => label. text = "Leave that button alone!" case Button. Clicked(component) if component == toggle => label. text = "Toggle is " + (if (toggle. selected) "on" else "off") case Mouse. Clicked(_, point, _, _, _) => text. Field. text = (s"You clicked in the Canvas at x=${point. x}, y=${point. y}. ") } } } 7
The End 8