JRBuilder: Hello World Demo 1
Home    Contents    Next  » Download
JRBuilder: Hello World Demo 1
The traditional Hello World application, with a File menu.
JRBuilder code runs in an instance of JRBuilder::Context (or you can write your own Context class). Code in the ctx.enter{} block runs as though it were inside that class.

Component names are resolved by looking in the java.awt and javax.swing packages (and a couple of others), preferring the Swing 'J' versions over corresponding AWT components. If you really want a java.awt.Frame, use Frame, instead of frame.

Any event handler defined in a xxxListener interface can be specified by prepending on_ to the name of handler method. on_action_performed is aliased as on_click for convenience.

Many common constants can be specified using :CONSTANT_NAME.

The use of Box for layout has been simplified by introducing pseudo-component names, like x_glue, for its static methods (like Box.createHorizontalGlue). Box offers a fairly low frustration/functionality ratio for those of us who aren't UI pros.

Layouts and Borders use a special syntax: layout :type and border :type. This tells the Builder to call setLayout or setBorder, instead of add. (This syntax can be used for other components that follow the same pattern.)

require 'jrbuilder'

ctx = JRBuilder.new_swing_context

ctx.enter {
  attr_reader :my_frame

  @my_frame = frame('Hello World Demo 1') { size 320, 240
    content_pane { layout :box,_parent,:Y_AXIS }
    on_window_closing { my_frame.dispose }
    menu_bar { 
      menu('File') {  mnemonic :VK_F; background :WHITE
        menu_item('Exit') { mnemonic :VK_X; background :WHITE
          on_click { my_frame.dispose }
        }
      }
    }
    y_glue
    panel {layout :box,_parent,:X_AXIS; background :WHITE 
      label('Hello, World!') {  foreground :BLUE }
    }
    y_glue
  }
}

ctx.my_frame.show
Home    Contents    Next  » Download