include Java
include_class javax.sound.midi.MidiSystem
include_class javax.swing.JFrame
include_class java.awt.event.KeyListener
# 准备合成
hecheng = MidiSystem.synthesizer
hecheng.open
channel = hecheng.channels[0]
# 接收按键的frame
midi_frame = JFrame.new("Music Frame")
midi_frame.set_size 200, 200
midi_frame.default_close_operation = JFrame::EXIT_ON_CLOSE
# 监听键盘
midi_frame.add_key_listener KeyListener.impl { |name, event|
case name
when :keyPressed
channel.note_on event.key_char, 64
when :keyReleased
channel.note_off event.key_char
end
}
# 显示 midi_frame
midi_frame.visible = true
这样写一个MIDI是不是比Java写的简单很多,也简洁很多?
值得注意的一个小地方是,关于接口的实现。在较新版本的JRuby中,接口一般是当做模块混含进JRuby的。但是同时JRuby提供了一个接口的实现方法 impl。
这样我觉得是更加简洁了,而且有匿名类的效果。
分享到:
相关推荐
JRuby - 一个Ruby语言的Java实现,Ruby在JVM上的实现
本文介绍了如何用JRuby开发Web Service。用JRuby开发Web Service,需要安装ActionWebService。安装完毕之后便可以开始调用及部署了。
jruby是一个ruby的解析器,可将ruby解析为java文件,jruby作为ruby解析器时刻调用java代码
本文介绍了JRuby的安装与配置。JRuby的安装需要JRuby以及JDK,本文使用的版本分别是JRuby 1.1.2以及Java 1.6。至于JRuby配置,文章主要介绍了测试JRuby安装的方法。
jruby安装包 和 jruby的lib库 JRuby是面向Ruby、基于Java虚拟机(JVM)的一种解释程序,它结合了Ruby语言的简易性和功能强大的JVM的执行机制,包括与Java库 全面集成。Rails彻底加快及简化了Web应用的开发,不过它让...
jruby-src-1.0.zip
jruby-1.3.1.jar包 解决org.jruby.exceptions.JumpException 等报错
jruby-complete-1.7.19.jar,java版本的ruby解析器
org.jruby.rar
对于Jruby、Jython和Groovy三种动态语言,长期以来很多人犹豫于如何选择它们。本文提出一个非常有力的例证证明:如果你是一个基于Java虚拟机的开发者,那么请放弃Jruby和Jython。
jruby-bin-1.3.1.zip 用于jruby语言研发的必要工具
JRuby window X64 开发软件
jruby与java使用开发,听说jruby能是swing的调用变为更简单
JRuby在Java测试中的应用,使用脚本语言,高效快进行Java程序测试。
This book introduces you to three JRuby deployment strategies that give you the performance and scalability you need, while you use a language designed for rapid development. Explore common ...
jRuby On Rails WEB 2.0
JRuby语言实战技术,java下运用ruby
jruby 1.9.6 win64
jruby-1.5.5+OperaWatir+RSpec
Now you can bring the best of Ruby into the world of Java, with Using JRuby. Come to the source for the JRuby core team’s insights and insider tips. You’ll learn how to call Java objects seamlessly ...