1.新建Scala Project,这里我们命名为test
2.导入库
3.新建包以及新建Scala Object,这里我们命名为test.scala
4.敲代码
package com.hunau
import scala.swing.SimpleSwingApplication
import scala.swing.FileChooser
import java.io.File
import scala.swing.MainFrame
import scala.swing.FlowPanel
import scala.swing.Button
import scala.swing.Label
import scala.swing.event.ButtonClicked
object test extends SimpleSwingApplication {
val fileChooser = new FileChooser(new File(".")) //文件对话框
fileChooser.title = "选择文件"
val button = new Button{
text = "从本地选择文件"
}
val label = new Label {
text = "还未选择文件."
}
val mainPanel = new FlowPanel{
contents += button
contents += label
}
def top = new MainFrame{
title = "Scala GUI编程 !!!"
contents = mainPanel
listenTo(button)
reactions += {
case ButtonClicked(b) => {
val result = fileChooser.showOpenDialog(mainPanel)
if(result == FileChooser.Result.Approve){
label.text = fileChooser.selectedFile.getPath()
}
}
}
}
}
5.导出jar
6.打开终端,运行