Jump to content

Obtain Static Method And Button With Listeners


Recommended Posts

HiI am totally new to java and have been running through a few excersises and am stuck with the wording behind this. I have the basic idea down but can't seem to get from instructions to actually implementing the code. Here's what I'm trying to do,1. Invoke the Factory static method createCRCList() to obtain the collection of CRC objects.I have 3 java files. Only two are necessary here. Both codes are listed below. I'm supposed to invoke the method from Factory.java into the GUI.java file and I'm not sure what to do. It seems like a single line of code but I'm not sure what it is or where to put it?2. Create a button with an action listener for all the objects. I've created buttons before but never linked up up with object so I'm hoping someone can give me a kinda 'blank' code for it so I can learn the method behind it. Here's the specs a. get the corresponding CRC object, b. get its name and create a new JButton labelled with that name, c. make the CRC object be the listener for the button, d. add the button to the Box, e. add a 10-pixel strut to the Box, in order to separate each button from the next.Factory.java

import java.util.*;/** * Provides a static method to construct a list of CRC cards. * The class cannot be instantiated */public class Factory {	/* This class cannot be instantiated */	private Factory() {}	/**	 * Creates a list of CRC cards.	 * @return the list of CRC cards.	 */	public static List createCRCList() {		List list = new ArrayList();		CRC course = new CRC("Course",							 "Defines the name and structure of a course.\n\n"							 + "Comprises one or more stages.",							 new ArrayList());		CRC stage = new CRC("Stage",							 "One stage of a course.\n\n"							 + "Comprises a number of modules\n\n"							 + "Maintains a record of students registered\n"							 + "on the stage of the course.",							 new ArrayList());		CRC module = new CRC("Module",							 "Defines the name of a module,\n"							 + "its purpose and learning outcomes.",							 new ArrayList());		CRC student = new CRC("Student",							 "Defines the name and id of a student.",							 new ArrayList());		CRC academic = new CRC("Academic",							 "Defines the name of an academic.\n\n"							 + "Maintains a record of personal tutees\n"							 + "(students).\n\n"							 + "Maintains a record of the modules\n"							 + "for which the academic is responsible.",							 new ArrayList());		course.getCollaborators().add(stage);		stage.getCollaborators().add(module);		stage.getCollaborators().add(student);		academic.getCollaborators().add(student);		academic.getCollaborators().add(module);		list.add(course);		list.add(stage);		list.add(module);		list.add(student);		list.add(academic);		return list;	}}


package crc;import javax.swing.*;/** * Provides a window containing a list of buttons, one for each CRC card. * * @author Tahir Malik * @version 15/05/2009 */public class GUI {	/**	 * Creates and displays a window containing a list of buttons,	 * one for each CRC card	 */	public GUI() {		Box buttonList = Box.createVerticalBox();		buttonList.add(Box.createVerticalStrut(10));		// add your code here and remove this comment line	}	/**	 * main method	 */	public static void main(String[] args) {		GUI gui = new GUI();	}}

Thanks for your help, it may seem really easy but as a total beginner this is highly appreciated.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...