Developed by Susan Haller and Timothy Fossum, University of Wisconsin-Parkside, to accompany Chapter 14 of An Introduction to Object-Oriented Programming with Java by C. Thomas Wu. Adapted by Pete Sanderson
I have placed the lab materials, folder Lab6, into your individual folder in the course repository (not in the public folder). You probably called this folder myFiles. Update it and you should be ready to go.
Launch jGRASP.
Write your answers to these questions on your answer sheet:
Predict what will happen when this class is compiled and run. Compile and run to see if your prediction is correct.
Predict what will happen when you run this program. Compile and run it. Without changing the size of the window or the button, position the button so that it is completely within the window.
Write your answer to this question on your answer sheet:
It would be more interesting if something happened when you click on the button
Go into subdirectory ThirdFrame and edit the MyJFrame class here. Examine the code. Notice that MyJFrame implements the ActionListener interface. Next look at the MyJFrame constructor. When a MyJFrame object is constructed, it is added as a listener to the button displayed in its content pane. Notice that the button is labeled "0".
Because this version of MyJFrame implements the ActionListener interface, it must have an actionPerformed() method that is passed an ActionEvent to handle. Right now, the actionPerformed() method does nothing with the button event. Compile and run this program and notice that nothing happens when you click on the button.
Add code to the actionPerformed() method so that each time you click on the button, the number displayed increments by one. For example, when you click on it the first time a "1" should appear, the second time you click on it a "2" should appear and so on.
Hint: Add a new private integer instance variable buttonCount that keeps track of how many times the button has been clicked on. You should initialize this variable in the MyJFrame constructor. Every time the button is clicked, increment this variable and display the new value on the button. In your Java documentation, look up the setText() method that the JButton class inherits from its parent class AbstractButton(). Also, look at how the JButton was constructed for some ideas about how to convert the value of buttonCount to a String. You can pass this string as an argument to the button's setText() method to set the label on the button to the new value of your counter variable.
1 Call me over to see your code and running program.
2 Call me over to see your code and running program.
Modify your actionListener() code so that when you enter text in the text field and hit ENTER, the handler extracts the String from the text and turns it into an int (use Integer.parseInt()). This value should then be used to update the buttonCount value and also the label on the button. For example, suppose the button currently displays "2" and you enter "77" into the text field and hit ENTER. The button should display "77". Furthermore, if you follow this with clicking on the button, it should display "78" and so on. As with the previous checkpoint, your actionListener()code can determine the source of the event (either the text box or the button) and act accordingly.
3 Call me over to see your modification and running program.
For the next checkpoint, add a label to this version of MyJFrame, positioned above the text field, that says "Enter a number to update the button". This is to let the user know what should be entered into the text field. The label should be centered (as best you can) just above the text field. You may need to experiment with a few position values to get the label to look right. If you want to change the label text color, use its (inherited) setForeground() method.
4 Call me over to see your modification and running program.
JMenu menu; JMenuItem clearItem; JMenuItem exitItem;Don't forget to create a JMenuBar object and add it to the frame using setJMenuBar(). As before, use the MyJFrame object as the action listener for each of the two menu items, but do not change your actionListener() code yet.
Compile your program and check to see that the menu appears and that you can "select" either of the two menu items labeled Clear and Exit, but that nothing happens when you do.
Finally, change the actionListener() code so that if the event source is clearItem, the value of buttonCount will be reset to zero (and the button text will change accordingly) and if the event source is exitItem, the program will terminate immediately using System.exit(0).
Make these changes, and compile and test your program.
5 Show me your modifications and demonstrate that your program works correctly making appropriate menu selections.