码迷,mamicode.com
首页 > 其他好文 > 详细

Scala界面事件处理

时间:2015-07-30 00:44:31      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:scala   编程   


示例代码:


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  访问密码45e2





版权声明:本文为博主原创文章,未经博主允许不得转载。

Scala界面事件处理

标签:scala   编程   

原文地址:http://blog.csdn.net/u013361361/article/details/47137987

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!