Using JButtons in Frame-based Applications

Dharmesh Tailor's Random Numbers Guessing Game is a well-commented useful demonstration of the use of JButtons, JLabels and JTextFields in GUI applications. Note how he adds components to a FlowLayout instead of to a Box. We needed to remove the instruction gui.setResizable(False) in order to make the application work on our Raspberry Pi.

Application in action

Application in action

You can compare this Oxygene code with the RemObjects C# version below it.

The code in jbutton_app.pas

namespace jbutton_app;
{
    Author: Dharmesh Tailor
    Application: Random Numbers Guessing Game
}
interface

uses
  java.awt.*, java.awt.event.*, javax.swing.*;

type
  JButtonApp = public class(JFrame, ActionListener)
  private      
    button : JButton;
    textField : JTextField;
    promptLabel, resultLabel, randomLabel : JLabel;

  public
    constructor;
    class method Main(args : array of String);
    method actionPerformed(e : ActionEvent);
  end;

implementation

constructor JButtonApp;
begin
  {Sets the layout - FlowLayout is the most simple where components
  are arranged one after the other. Will write a program to demonstrate
  GridLayout later.}
  setLayout(new FlowLayout);

  promptLabel := new JLabel('Enter a random number 1-10');
  add(promptLabel);

  textField := new JTextField(5);
  add(textField);

  button := new JButton('Guess!');
  button.addActionListener(self);
  add(button);

  resultLabel := new JLabel('');
  add(resultLabel);

  randomLabel := new JLabel('');
  add(randomLabel);
end;

method JButtonApp.actionPerformed(e : ActionEvent);
var
  randomNum, guess : Integer;
begin
  if e.getSource = button then
    begin
      //Generates a random number between 1 and 10
      randomNum := Integer(Math.random * 10 + 1);
      try
        //Takes the input from the textfield and tries to parse it
        guess := Integer(Double.parseDouble(textField.getText));

        if guess = randomNum then
          begin
            resultLabel.setText('You won the game!');
          end
        else
          begin
            resultLabel.setText('You lost the game. ');
          end;
        randomLabel.setText('The random number generated was: ' + randomNum);
      //If the textfield input is invalid, i.e. not a number
      except
        on Exception do
          begin
            randomLabel.setText('Please enter numbers only!');
          end;
      end;
    end;
end;

class method JButtonApp.Main(args : array of String);
begin
  var gui : JButtonApp := new JButtonApp;
  gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  gui.setVisible(True);
  gui.setSize(350, 100);
  gui.setResizable(False);
  gui.setTitle('Random Number Guessing Game');
end;

end.

The code of jbutton_app.oxygene

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <PropertyGroup>
    <OutputType>Executable</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="rt.jar" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="jbutton_app.pas" />
  </ItemGroup>
  <Import Project="$(MSBuildExtensionsPath)\RemObjects Software\Oxygene\RemObjects.Oxygene.Cooper.targets" />
</Project>

RemObjects C# Version of Random Numbers Guessing Game

/*
   Author of original Oxygene version: Dharmesh Tailor
   Application: Random Numbers Guessing Game
   Converted to RemObjects C# by Norman Morrison 
*/

using java.util;
using java.awt;
using java.awt.@event;
using javax.swing;

namespace jbutton_app_cs
{
  public class JButtonApp  : JFrame, ActionListener
  { 
    private JButton button;
    private JTextField textField;
    private JLabel promptLabel, resultLabel, randomLabel;
    public JButtonApp()
    {
      /* 
      Sets the layout - FlowLayout is the most simple where components
      are arranged one after the other. Will write a program to demonstrate
      GridLayout later.
      */
      setLayout(new FlowLayout());

      promptLabel = new JLabel("Enter a random number 1-10");
      add(promptLabel);

      textField = new JTextField(5);
      add(textField);

      button = new JButton("Guess!");
      button.addActionListener(this);
      add(button);

      resultLabel = new JLabel("");
      add(resultLabel);

      randomLabel = new JLabel("");
      add(randomLabel);
    }

    public void actionPerformed(ActionEvent e)
    {
      Integer randomNum, guess;

      if (e.getSource() == button) 
      {
        //Generates a random number between 1 and 10
        randomNum = (Integer)(Math.random() * 10 + 1);
        try
        {
          //Takes the input from the textfield and tries to parse it
          guess = (Integer)(Double.parseDouble(textField.getText()));

          if (guess == randomNum)
            resultLabel.setText("You won the game!");
          else
            resultLabel.setText("You lost the game. ");
          randomLabel.setText("The random number generated was: " + randomNum);
        }
        
        catch (Exception) //If the textfield input is invalid, i.e. not a number
        {
          randomLabel.setText("Please enter numbers only!");
        }
      }
    }

    public static void Main(string[] args)
    {
      JButtonApp gui = new JButtonApp();
      gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      gui.setVisible(true);
      gui.setSize(350, 100);
      gui.setResizable(false);
      gui.setTitle("Random Number Guessing Game");
    }
  }
}


Programming - a skill for life!

How to use widgets such as JButtons, JRadioButtons, JLists, JTables and JDesktopPanes in Oxygene for Java Applications