I am very impressed with Ruby, and I am experimenting with JRuby in NetBeans. But it’s hard to get more than a smattering of information on using JRuby with Swing. At the moment, I have the following program, which works, except for the commented line.
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)
When I run this, the button automatically expands to occupy the whole window.
So why does “setSize” work on the main window, but not on the button.
Also, it there a “setBounds” method analogous to that in Java?
Thanks for any help with this. I code my own layouts in Java, and that is what I’d like to do in JRuby.
This has to do with the layout of mainWindow and is the same behaviour that you’d have if this was written in Java. The default is BorderLayout, which changes the size of the frame’s contents. You could try using something like FlowLayout instead.
There certainly is a
setBoundsmethod.