-
object GUI_Panel_Layout extends SimpleSwingApplication{
-
def top = new MainFrame {
-
title = "Second GUI"
-
val button = new Button {
-
text = "Scala"
-
}
-
val label = new Label {
-
text = "Here is Spark!!!"
-
}
-
contents = new BoxPanel(Orientation.Vertical) { //垂直布局
-
contents += button
-
contents += label
-
border = Swing.EmptyBorder(50, 50, 50, 50)
-
}
-
-
listenTo(button) //监听button,解除监听为deafTO
-
var clicks =0
-
reactions += { //处理事件
-
case ButtonClicked(button) => { //偏函数匹配点击事件
-
clicks += 1
-
label.text = "Clicked " + clicks + " times"
-
-
}
-
}
-
}
-
}
object GUI_Event extends SimpleSwingApplication {
val fileChooser = new FileChooser(new File("."))
fileChooser.title = "File Chooser"
val button = new Button {
text = "Choose a File from local"
}
val label = new Label {
text = "No any file selected yet."
}
val mainPanel = new FlowPanel {
contents += button
contents += label
}
def top = new MainFrame {
title = "Scala GUI Programing advanced!!!"
contents = mainPanel //mainPanel里有buuton、label
listenTo(button) //监听button
reactions += { //事件栈
case ButtonClicked(b) => {
val result = fileChooser.showOpenDialog(mainPanel) //打开对话框时指定父容器
if (result == FileChooser.Result.Approve) { //如果选择了文件
label.text = fileChooser.selectedFile.getPath() //修改label.text
}
}
}
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28912557/viewspace-2056614/
,如需转载,请注明出处,否则将追究法律责任。