Scala介面Pannel、Layout

541732025發表於2016-03-13

點選(此處)摺疊或開啟

  1. object GUI_Panel_Layout extends SimpleSwingApplication{
  2.     def top = new MainFrame {
  3.       title = "Second GUI"
  4.       val button = new Button {
  5.         text = "Scala"
  6.       }
  7.       val label = new Label {
  8.         text = "Here is Spark!!!"
  9.       }
  10.       contents = new BoxPanel(Orientation.Vertical) { //垂直佈局
  11.         contents += button
  12.         contents += label
  13.         border = Swing.EmptyBorder(50, 50, 50, 50)
  14.       }
  15.       
  16.       listenTo(button) //監聽button,解除監聽為deafTO
  17.       var clicks =0
  18.       reactions += { //處理事件
  19.         case ButtonClicked(button) => { //偏函式匹配點選事件
  20.           clicks += 1
  21.           label.text = "Clicked " + clicks + " times"
  22.           
  23.         }
  24.       }
  25.     }
  26. }

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部落格 ” ,連結:http://blog.itpub.net/28912557/viewspace-2056614/,如需轉載,請註明出處,否則將追究法律責任。

相關文章