2
UFCE3J-20-3 Practical 4 Page 1 Agent-Based Computing (UFCE3J-20-3) Practical Session 4 “Hooking up” Event-Handlers for Noughts and Crosses GUI Session Goals 1. Hook up the event-handler mechanisms to build the correct event-driven behaviour for our Noughts and Crosses GUI. A quick recap on the noughts and crosses GUIWe need to make the “nought” or “cross” grid change when a player clicks on the grid element. We’ll do this first, and then move on to making the GUI functional from a two player viewpoint secondly. Before (or even after!) attempting this tutorial, please read Java Software Solutions by Lewis and Loftus or your favourite Java text book for a full explanation on how to hand craft the panels, their layout, and the necessary event-handlers to make it all work. Studio Exercise Here’s where we were at the end of the last practical session with the GUI builder: Let’s begin by setting up an event-handler for the first element of the grid, which is the JLabel variable “grid11”. In NetBeans, load up the “AgentGame” project you were working with in practical session 3. Click on the Frame.java file in Design mode. In the “Inspector” window, click on “grid11” JLabel. Look at the “Properties” window, and click on the “Events” button underneath. A scrollable list of events should appear below. Scroll down to “mouseClicked”.

ABC 13 Tutorial 04 Hooking Up Event Handlers

  • Upload
    nesh

  • View
    215

  • Download
    4

Embed Size (px)

DESCRIPTION

event handler

Citation preview

  • UFCE3J-20-3 Practical 4 Page 1

    Agent-Based Computing (UFCE3J-20-3)

    Practical Session 4

    Hooking up Event-Handlers for Noughts and Crosses GUI

    Session Goals 1. Hook up the event-handler mechanisms to build the correct event-driven

    behaviour for our Noughts and Crosses GUI.

    A quick recap on the noughts and crosses GUI We need to make the nought or cross grid change when a player clicks on the grid element. Well do this first, and then move on to making the GUI functional from a two player viewpoint secondly.

    Before (or even after!) attempting this tutorial, please read Java Software Solutions by

    Lewis and Loftus or your favourite Java text book for a full explanation on how to

    hand craft the panels, their layout, and the necessary event-handlers to make it all

    work.

    Studio Exercise Heres where we were at the end of the last practical session with the GUI builder:

    Lets begin by setting up an event-handler for the first element of the grid, which is the JLabel variable grid11.

    In NetBeans, load up the AgentGame project you were working with in practical session 3. Click on the Frame.java file in Design mode. In the Inspector window, click on grid11 JLabel. Look at the Properties window, and click on the Events button underneath. A scrollable list of events should appear below. Scroll down to

    mouseClicked.

  • UFCE3J-20-3 Practical 4 Page 2

    Left mouse click ONCE on the mouseClicked down arrow and notice that NetBeans suggests a candidate handler method for you called something like

    grid11MouseClicked. (You can change this to anything you want, but for the moment go with the generated method name.) Now heres the clever bit NetBeans will generate you the method, take you to the Source view of the Frame, and place the cursor in the handler method. In the handler method, you might like to add a line

    of code such as:

    grid11.setText( X );

    Now compile and build, and see what happens when you click on element 1, 1 of the

    grid

    Now we know how to hook up and event handler in NetBeans, lets go on the make the GUI more like a game but well develop the game logic and rules next week! For this weeks practical, youll need to:

    1. Set all the gamePanel grid JLabels to (whitespace) at the start of the game,

    2. Introduce, for example, a boolean variable to handle the switching from player 1 to player 2,

    3. Ensure that each grid element can only be changed once i.e. once a grid element is changed to a nought or a cross, it cant be changed again.

    4. When the new button is clicked, all the gamePanel JLabels are set to (whitespace). Hint use the action performed event for the button

    Good luck!