ITPub博客

首页 > 应用开发 > Java > Scala界面Pannel、Layout

Scala界面Pannel、Layout

原创 Java 作者:541732025 时间:2016-03-13 19:55:57 0 删除 编辑

点击(此处)折叠或打开

  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/,如需转载,请注明出处,否则将追究法律责任。

上一篇: Scala并发编程
下一篇: Type与Class
请登录后发表评论 登录
全部评论

注册时间:2013-05-23

  • 博文量
    127
  • 访问量
    479167