Scala Swing 之使用 FileChooser

时间:2021-07-08 23:09:28
import scala.swing.Button
import scala.swing.FileChooser
import scala.swing.FlowPanel
import scala.swing.MainFrame
import scala.swing.SimpleGUIApplication
import java.io.File
import scala.swing.event.ButtonClicked
import scala.swing.Label

object SimpleGUI extends SimpleGUIApplication {
val chooser = new FileChooser(new File("."))
chooser.title = "chooser"
val button = new Button {
text = "Choose a File"
}
val label = new Label {
text = "No file selected yet."
}
val mainPanel = new FlowPanel {
contents += button
contents += label
}
def top = new MainFrame {
title = "Simple GUI"
contents = mainPanel

listenTo(button)

reactions += {
case ButtonClicked(b) => {
val result = chooser.showOpenDialog(mainPanel)
if(result == FileChooser.Result.Approve) {
label.text = chooser.selectedFile.getPath()
}
}
}
}
}