Jade Dungeon

Scala图形界面

GUI程序简介

只有界面的GUI程序

程序入口

scala.swing.SimpleSwingApplication {
	abastract def top: Frame   // GUI程序的入口,相当于Main方法
}

GUI主窗口

scala.swing.MainFrame {  // 主窗口,关闭主窗口相当于关闭程序
	def title: String                   // 标题栏
	def title_=(s: String): Unit        // 标题栏

	def contents: Seq[Component]        // 添加GUI组件
	def contents_=(c: Component): Unit  // 添加GUI组件
}

常用组件

按钮:

scala.swing.Button {
	def text: String                // 按钮文本
	def text_=(s: String): Unit     // 按钮文本
}

文本:

scala.swing.Label {
	def text: String                // 文本
	def text_=(s: String): Unit     // 文本
}

边框

scala.swing.Swing {
	def EmptyBorder(top: Int, left: Int, bottom: Int, right: Int): Border
}

外观样式

import java.awt.Color
import javax.swing.UIManager
import javax.swing.UnsupportedLookAndFeelException

UIManager.put("control", new Color(128, 128, 128))
UIManager.put("info", new Color(128, 128, 128))
UIManager.put("nimbusBase", new Color(18, 30, 49))
UIManager.put("nimbusAlertYellow", new Color(248, 187, 0))
UIManager.put("nimbusDisabledText", new Color(128, 128, 128))
UIManager.put("nimbusFocus", new Color(115, 164, 209))
UIManager.put("nimbusGreen", new Color(176, 179, 50))
UIManager.put("nimbusInfoBlue", new Color(66, 139, 221))
UIManager.put("nimbusLightBackground", new Color(18, 30, 49))
UIManager.put("nimbusOrange", new Color(191, 98, 4))
UIManager.put("nimbusRed", new Color(169, 46, 34))
UIManager.put("nimbusSelectedText", new Color(255, 255, 255))
UIManager.put("nimbusSelectionBackground", new Color(104, 93, 156))
UIManager.put("text", new Color(230, 230, 230))
UIManager.setLookAndFeel(
		classOf[javax.swing.plaf.nimbus.NimbusLookAndFeel].getName())

画出GUI程序

import scala.swing._
object SecondSwingApp extends SimpleSwingApplication {

	def top = new MainFrame {
		title = "Second Swing App"
		val button = new Button { text = "Click me" }
		val label = new Label { text = "No button clicks registered" }
		
		contents = new BoxPanel(Orientation.Vertical) {
			contents += button
			contents += label
			border = Swing.EmptyBorder(30, 30, 10, 30)
		}
	}

}

图

监听事件

事件监听者

trait scala.swing.Reactor extends AnyRef {   // 所有事件处理类的基类
	def deafTo(ps: Publisher*): Unit      // 定义忽略哪些事件来源发出的事件消息
	def listenTo(ps: Publisher*): Unit    // 定义监听哪些事件来源发出的事件消息
	val reactions: Reactions              // 定义处理事件消息的具体逻辑
}

事件发布者

Scala Swing所有控件的基类scala.swing.UIElement都间接混入了事件发布者特质 scala.swing.Publisher

trait scala.swing.Publisher extends Reactor {
	def publish(e: Event): Unit          // 发出事件消息
}

事件的处理逻辑

object scala.swing.Reaction

abstract Reactions extends Reaction {
	abstract def +=(r: Reaction): Reactions.this.type // 添加事件处理逻辑
	abstract def -=(r: Reaction): Reactions.this.type // 减少事件处理逻辑

	abstract def apply(v1: Event): Unit               // 定义事件处理逻辑
	abstract def isDefinedAt(x: Event): Boolean       // 检查针对具体事件消息是否有定义
}

事件消息

trait Event extends AnyRef

trait UIEvent extends Event

trait ComponentEvent extends UIEvent

trait ActionEvent extends ComponentEvent

case class ButtonClicked(source: AbstractButton) extends ActionEvent 
	with Product with Serializable       // 按按钮

class ValueChanged extends ComponentEvent

case class EditDone(source: TextField) extends ValueChanged 
	with Product with Serializable       // 文本框编辑完成

能互动的GUI程序

import scala.swing._
import scala.swing.event._
object ReactiveSwingApp extends SimpleSwingApplication {
	def top = new MainFrame {
		title = "Reactive Swing App"
		val button = new Button { text = "Click me" }
		val label = new Label { text = "No button clicks registered" }
		contents = new BoxPanel(Orientation.Vertical) {
			contents += button
			contents += label
			border = Swing.EmptyBorder(30, 30, 10, 30)
		}

		listenTo(button)      // 监听哪个事件

		var nClicks = 0

		reactions += {        // 处理消息的具体逻辑
			case ButtonClicked(b) =>
			nClicks += 1
			label.text = "Number of button clicks: " + nClicks
		}
	}
}

图