本文主要是介绍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.ButtonClickedobject 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 += buttoncontents += labelborder = Swing.EmptyBorder(50,50,50,50)}listenTo(button) //监听该buttonvar clicks = 0reactions += {case ButtonClicked(button) => {clicks += 1label.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.ButtonClickedobject 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 += buttoncontents += label}def top = new MainFrame{title = "Scala GUI Programing advanced By LEAF !!!"contents = mainPanellistenTo(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界面事件处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!