Scala GUI 之 FileChooser选择文件

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()
        }
      }
    }
  }
}
 

你可能感兴趣的:(scala,GUI,swing,FileChoser)