我对Ruby印象深刻,我正在NetBeans中尝试JRuby。但是很难获得更多关于在Swing中使用JRuby的信息。目前,我有以下程序,这是工作,除了评论行。
require 'java'
include_class 'java.awt.event.ActionListener'
include_class 'javax.swing.JButton'
include_class 'javax.swing.JFrame'
class ClickAction
include ActionListener
def actionPerformed(event)
puts "Button clicked"
end
end #ClickAction
class MainWindow < JFrame
def initialize
super "JRuby Swing Demo"
setDefaultCloseOperation JFrame::EXIT_ON_CLOSE
button = JButton.new "Click me"
button.setSize 30, 100 #this line does nothing
button.addActionListener ClickAction.new
add button
pack
end
end
mainWindow = MainWindow.new
mainWindow.setSize 300, 300
mainWindow.setVisible(true)
当我运行此命令时,按钮会自动展开以占据整个窗口。
为什么“setSize”在主窗口上工作,而不是在按钮上。
还有,它有一个类似于Java的“setBounds”方法吗?
谢谢你的帮助。我用Java编写自己的布局,这就是我想在JRuby中做的。