"Hello World" program in Swing and JRuby
In the previous section of JRuby Tutorials you have studied how to use Java classes in JRuby examples to show results on your console. Now we are going to describe you how to use Swing in JRuby program.
In this example we will create our first "HelloWorld" swing application in JRuby. First line in our program tells that we are making our JRuby program enabled to use java classes.
frame = javax.swing.JFrame.new() creates a new variable object "frame" which is an instance of javax.swing.JFrame .
frame.getContentPane().add(javax.swing.JLabel.new("Hello World")) will add a new JLabel to this frame via getting the content pane.
frame.set_visible(true) makes frame to be visible. Here is the full example code of JRubySwingHello.rb as follows:
JRubySwingHello.rb
require "java" frame = javax.swing.JFrame.new() frame.getContentPane().add(javax.swing.JLabel.new("Hello World")) frame.setDefaultCloseOperation(javax.swing.JFrame::EXIT_ON_CLOSE) frame.pack() frame.set_visible(true) |
Output:
Type the following command in your command prompt to
execute this example:
C:\JRuby>jruby JRubySwingHello.rb |