I try to build a very simple GUI, in fact a JFrame which only contains a
button which is clickable, but it doesnt work properly. The GUI is
visible, but when I try to click the button, an error occurs.
My code:
#Just a helping method
def include_classes(*classes)
classes.each do |element|
include_class(element)
end
end
require “java”
include_classes “javax.swing.JFrame”, “java.awt.event.ActionListener”
class MyFrame < JFrame
include ActionListener #in order to implement the interface
def initialize
#Initializing the frame
super(“JRuby!”)
setSize(600, 200)
setLayout(java.awt.FlowLayout.new)
setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE)
#Configuring and adding the button
button = javax.swing.JButton.new("Click!")
getContentPane.add(button)
button.addActionListener(self)
setVisible(true)
end
#think of an @Override here
def actionPerformed(event)
print “apfelnerd”
end
end
#Running
MyFrame.new
The beforementioned error is attached to this post.