L04

Due by the end of Tuesday, 2 February 2021


SUBMIT   /   CHECK

When this week's lab is done, you should have a GUI that looks and behaves like this:

Programming Activity 1
Download the file AdderDialog.java into your project folder. Compile and run the file. Use it to add some numbers.

Change the name of the Calculate button to Add. Add a Multiply button beside the Add button. When pressed, it multiplies the two numbers together and saves the result in the result box.

The multiplier works just like the adder, except that it does multiplication rather than addition.
You should also change the instruction label so it asks for two numbers "to add or multiply" and the title of the window to "Add/Multiply Dialog".
Programming Activity 2
Add a Clear button that clears both the number fields as well as the result. After pressing it, the text fields should all be blank/empty.
Programming Activity 3
This time we are going to add a menu to our GUI. The File menu will have Add and Multiply commands (that do exactly what the corresponding buttons do) and an Exit command (that does exactly what the Done button does).

There are three kinds of objects required:

When you create an item or a menu, you give the name as an argument to the constructor -- new JMenuItem("Add"), for example. The JMenuBar constructor needs no argument.

When you have created all the objects you need, you should add the JMenuItems to the JMenu, and the JMenu to the JMenuBar.

Then you need to use the JFrame's setJMenuBar method to add the menubar to the adder dialog window. (Note: it is not added like the other controls.)

JMenuItems are very much like JButtons: you need to add an action listener to them to get them to do anything.

Make sure all your buttons and menu commands work.

Submit this/these files:

You will be graded on the following:

  1. Calculate button has been changed to Add (still adds)
  2. Multiply button has been added...
  3. ... and it multiplies the two user-entered numbers into the result
  4. instruction label changed appropriately
  5. window title modified properly
  6. Clear button has been added...
  7. ... and works properly
  8. Done button still ends the program (i.e. wasn't modified)
  9. the GUI has a menubar
  10. ... the menubar has a File menu
  11. ... ... the File menu has Add, Multiply and Exit commands
  12. ... the commands in the menu bar work correctly

SUBMIT   /   CHECK