浏览 2329 次
锁定老帖子 主题:impl与内部类
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-08-23
# To change this template, choose Tools | Templates # and open the template in the editor. #puts "Hello World" include Java include_class javax.swing.JFrame include_class javax.swing.JPanel include_class javax.swing.JButton include_class java.awt.event.ActionListener include_class javax.swing.JOptionPane include_class java.awt.Color #include_class java.awt.Container class ButtonFrame < JFrame def initialize super "Hello world Swing" setTitle__method "Button" set_size(300,200) panel=ButtonPanel.new getContentPane().add(panel) set_default_close_operation JFrame::EXIT_ON_CLOSE set_visible true end end class ButtonPanel <JPanel def initialize super yellowbutton=JButton.new("Yellow") redbutton=JButton.new("Red") bluebutton=JButton.new("Blue") add(yellowbutton) add(redbutton) add(bluebutton) yellowbutton.addActionListener ActionListener.impl{ setBackground(Color::YELLOW) } bluebutton.addActionListener ActionListener.impl{ setBackground(Color::BLUE) } redbutton.addActionListener ActionListener.impl{ setBackground(Color::RED) } end # class Color_T # include ActionListener # def initialize(c) # @c=c # end # def action_performed(event) # setBackground(@c) # # end # end end ButtonFrame.new 在用class Color_T的时候,出现问题了,Color_T是内部类,但是setbackground是外部类的方法,在Java中这个不存在访问的问题。但是JRuby中出现问题了,但是我还不清楚问题如何解决 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-08-23
最后修改:2009-08-23
ruby 的内部类不与实例绑定。自然不能调用外部类的实例方法。
可以改成 ... redbutton.addActionListener Color_T.new(Color::RED, self) end # initialize class Color_T include ActionListener def initialize(c, obj) @c = c @obj = obj end def action_performed(event) @obj.setBackground(@c) end end ... 或者 ... color_me_onpush Color::RED, redbutton end # initialize def color_me_onpush color, button button.addActionListener ActionListener.impl { setBackground(color)} end ... |
|
返回顶楼 | |
发表时间:2009-08-23
最后修改:2009-08-23
用 jruby 跑了下,swing 真慢 ……
something cool: require 'cici' app = Cici.app 'color me~~' app.paint [300, 200] do |v| v.marginx = 20 # 左边距 v.br # 换一行得到点空间 v.button('red') { v.bkcolor = RGB[0xFF0000] } v.button('blue') { v.bkcolor = RGB[0x0000FF] } v.button('yello') { v.bkcolor = RGB[0xFFFF00] } end app.message_loop |
|
返回顶楼 | |