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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值