Home Tutorials Jruby "Hello World" program in Swing and JRuby
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

"Hello World" program in Swing and JRuby

Advertisement
In the previous section of JRuby Tutorials you have studied how to use Java classes in JRuby examples to show results on your console.

"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


Download Source Code

     

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 29, 2008

Related Tutorials

Ask Questions?    Discuss: "Hello World" program in Swing and JRuby  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com