CSC 216 F09/ActionEvents

From Expertiza_Wiki
Revision as of 18:39, 17 November 2009 by Sdelliot (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Listen to this!

By, Steven Elliott, Eric Lumpkin, Ryan Orning

Purpose:

This game is intended to give beginning and intermediate java classes an opportunity to understand the concept behind action events in relation to GUI programming. This game is intended to be informative, interactive, and at the same time entertaining.

Supplies:

  • 6 willing participants
  • 3 pieces of paper with the button names on them. Try to make them ambiguous so that the action performed does not know what the action will be.
    • JButton button1 = new JButton;
    • JButton button2 = new JButton;
    • JButton button3 = new JButton;
  • 3 pieces of paper that are folded in half. Write the if/else statements that correspond to the button names, on the outside of the paper. The inside should have some fun but not demeaning task. Use Pseudo-code to keep the example realistic.
    • if(e.getSource() == button1) {/* insert action here */}
    • else if(e.getSource() == button2) {/* insert action here */}
    • else if(e.getSource() == button3) {/* insert action here */}

Set up:

Have 3 of the volunteers be the buttons. Give each button a piece of paper with the button name on it. The buttons should NOT look at the paper. Then have your other 3 volunteers be the actionPerformed method. They should stand in order. With the else’s being after the first if statement. They CANNOT look at their action. This will be more entertaining for the class and also shows that the actionPerformed method is unaware of the task until it is activated. Use the whiteboard and initialize an int. (int i=10;) This will set up an action that is performed by one of the actionListener statements.

Playing the Game:

Have the class “push” (pick) a button. The button then takes the paper with the name on it and hands it to the first if statement. The if/else’s should run as a normal program would and pass the paper to the next one until the action is carried out. Once the paper gets to the correct if statement, the action within the statement should be carried out. Then repeat until all the buttons/actions have been preformed.

Example actions:

  • Changing a value: i=i*9;
  • Saying a phrase: System.out.speak(“Java is my favorite language”);
  • Writing a phrase on the whiteboard: System.out.println("I want a big cup of Java");
  • Etc.

Example Program Code


package mainpackage;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
 * Creates a sample Action Listener Class
 * 
 * @author Steven Elliott sdelliot@ncsu.edu
 * @version 1.0
 */

public class ActionListenerClass extends JFrame implements ActionListener {

	JButton button1;
	JButton button2;
	JButton button3;
	int i = 10;

	public ActionListenerClass() {
		super("Example");

		setSize(350, 250);
		setLocation(100, 100);
		setLayout(new GridLayout());
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		button1 = new JButton("Button1");
		button1.addActionListener(this);
		add(button1);

		button2 = new JButton("Button2");
		button2.addActionListener(this);
		add(button2);

		button3 = new JButton("Button3");
		button3.addActionListener(this);
		add(button3);

		setVisible(true);
	}

	/**
	 * Listens for buttons being clicked
	 * 
	 * @param e
	 *            the event that occured
	 */
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == button1) {
			i = i * 9;
			System.out.println(i);
		} else if (e.getSource() == button2) {
			System.out.println("I want a big cup of Java.");
		}

		else if (e.getSource() == button3) {
			// This part is pseudo-code and will aquire an error when ran.
			// Change to println to fix this.
			System.out.speak("Java is my favorite language.");
		}
	}

	/**
	 * Creates an object of the GUI which runs the program.
	 * 
	 */
	public static void main(String[] args) {
		ActionListenerClass listener = new ActionListenerClass();
	}
}

Video Example

This is a video example that shows the entire game. This perticular example is played with 2 buttons and 2 if statements. Here is the video