惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

博客园 - evilyzou

scala 的List foldLeft用法 Circumflex Scala中的Either的两个子类(Left,Right)的简单用法 Scala中的一些特殊字符用法 Scala中的PartialFunction在Lift当中的NamedPF应用 Lift当中的AnyVarTrati源码解析 SBT和IDEA 开发scala web程序 使用Sbt(一) scalatra web框架安装指南 ASP.NET MVC: Do You Know Where Your TempData Is? 使用IntelliJ IDEA, Maven and the jetty plugin调试web应用程序 Scala GraphViz DOT Parser Scala Standard library Scala 一些基本的语法 Scala 相关的资源和Blog Lift2.0发布 Linux 汇编语言开发指南 Linux2.6内核启动流程学习 [导入]C#强化系列文章五:动态代码的使用(反射和动态生成类) [导入]C#强化系列文章七:序列化和反序列化
scala的DynamicVariable用法
evilyzou · 2010-08-20 · via 博客园 - evilyzou
 package org.scalatra
import scala.util.DynamicVariable
import javax.swing._

/**
 * Created by IntelliJ IDEA.
 * User: evilyzou
 * Date: 2010-8-20
 * Time: 14:29:05
 * To change this template use File | Settings | File Templates.
 */

object SwingBuilder{
  case class Context(frame: Option[JFrame],parent: Option[JComponent])
}

class SwingBuilder{
  import SwingBuilder._
  val context=new DynamicVariable[Context](Context(None,None))

  def frame(title: String)(f: =>Unit)={
    val res=new JFrame(title)
    res.add(new JPanel())
    context.withValue(Context(Some(res),context.value.parent)){f;res}
  }

  def menuBar(f: =>Unit)={
    val mb=new JMenuBar()
    context.value.frame.foreach(_.setJMenuBar(mb))
    context.withValue(Context(context.value.frame,Some(mb))){f;mb}
  }

  def menu(title:String)(f: =>Unit)={
    val m=new JMenu(title)
    var m2=new JMenu(title+"Test")
    context.value.parent.foreach(_.asInstanceOf[JMenuBar].add(m))
    context.value.parent.foreach(_.asInstanceOf[JMenuBar].add(m2))
    context.withValue(Context(context.value.frame,Some(m))){f;m}
    context.withValue(Context(context.value.frame,Some(m2))){f;m2}
  }

  def menuItem(title:String)={
    val mi=new JMenuItem(title)
    context.value.parent.foreach(_.asInstanceOf[JMenu].add(mi))
  }
}

object Test{

  def main(args:Array[String]){
    val builder=new SwingBuilder()
    import builder._

    val f=frame("Demo"){
      val mb=menuBar{
        menu("File"){
          menuItem("New")
          menuItem("Open")
          menuItem("SSS")
      }
    }
    mb
  }
  f.setVisible(true)
}
}