1. 程式人生 > >Scala界面事件處理

Scala界面事件處理

點擊 order 大數據夢工廠 次數 program n) margin win cal


演示樣例代碼:


import scala.swing.SimpleSwingApplication
import scala.swing.MainFrame
import scala.swing.Button
import scala.swing.Label
import scala.swing.Orientation
import scala.swing.BoxPanel
import scala.swing.Swing
import scala.swing.event.ButtonClicked

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
    var clicks = 0
    reactions += {
      case ButtonClicked(button) => {
        clicks += 1
        label.text = "Clicked " + clicks + "times"
      }
    }
  }

執行效果(監聽button點擊次數):

技術分享技術分享



==========================================================

演示樣例代碼二:

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 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 By LEAF !!!"
    contents = mainPanel
    
    listenTo(button)
    
    reactions += {
      case ButtonClicked(b) => {
        val result = fileChooser.showOpenDialog(mainPanel)
        if(result == FileChooser.Result.Approve){
          label.text = fileChooser.selectedFile.getPath()
        }
      }
    }
  }
  
}


執行效果:

技術分享


當點擊choose a file from local時。我們能夠得到當前文件系統,如圖:

技術分享


當我們選擇某一個文件時。label中監聽該文件的絕對路徑(比方我們選擇.class文件):

技術分享





相關來源:DT大數據夢工廠,微信公眾號是DT_Spark,每天都會有大數據實戰視頻公布,請您持續學習。

相關資料:

scala深入淺出實戰經典完整視頻、PPT、代碼下載:

百度雲盤:http://pan.baidu.com/s/1c0noOt6

騰訊微雲:http://url.cn/TnGbdC

360雲盤:http://yunpan.cn/cQ4c2UALDjSKy 訪問password45e2





Scala界面事件處理