First, copy the lab materials to your account.
cp -r /home/Classes/Cs1/Labs/Lab07 .Everything you need for the lab exercises today is contained in this new directory.
Change directory to Lab07.
Predict what will happen when this
class is compiled and run. Compile and run to see if your prediction
is correct.
1
Call us over when you are ready with the answer to the first two questions.
Be ready to explain the behavior of the MyJFrame in terms
of the code for the MyJFrame constructor.
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.
2 Call us over to see your modification.
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. What should you initialize this variable to 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 a parameter to the button's setText() method to set the label on the button to the new value of your counter variable.
3 Call us over to see your code and running program.
4 Call us 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.
5 Call us 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.
6 Call us over to see your modification and running program.
For the last checkpoint, add a menu to your version of MyJFrame that has two menu items, one labeled Clear and the other labeled Exit. Follow the example in section 7.5 of your text. You will need to add instance variables for the menu and the two menu items. Use
JMenu menu; JMenuItem clearItem; JMenuItem exitItem;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.
7
Show us your modifications and demonstrate
that your program works correctly making appropriate menu selections.