Sie sind auf Seite 1von 2

4CCS1PRA

Programming Applications

Lab Assignment

Lab 2: Basic GUI Programming


Exercise 1
Create an application which when run displays a window of size 640x480! Closing the window of your
application should also close the application.
Hint: Use a method named setDefaultCloseOperation for this purpose.

Exercise 2
Add to your application a menu File, which should contain a single menu item Exit. When the user
selects Exit the program should quit.

Exercise 3
Extend your answer to the previous question by adding a JTextArea to your main window, and an
Insert menu with an entry Date menu item! When the user selects Date your program should insert
the current date into the text area.
Ensure that when adding the Date entry you do not stop the Exit entry from functioning.
Hint: You may wish to examine the append() method of JTextArea to answer this question. You
can use
DateFormat.getDateInstance().format(new Date())
to obtain the current date as a string. Note that you may need to import additional packages to compile this.

Exercise 4
Extend your answer to the previous question by adding a new menu Edit in between File and Insert.
The Edit menu must contain three items: Cut, Copy and Paste. Ensure that cut, copy and paste functionality is implemented for the text area.
Hint: The last part of this exercise is a lot easier than it sounds. Do you not know where to start? If so,
there is only one place to start your search: the Java API Documentation. You will need to look into
one of the parent classes of JTextArea.

Exercise 5 (advanced)
First, check out the class JOptionPane in the Java API and read through the information provided
carefully. If this is a little difficult to understand, go to the tutorial on making dialogue boxes at
http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html.
Now, extend your answer to the previous question such that when the user presses the Exit menu
item, (s)he is asked Are you sure you wish to quit? There should be two options: Yes and No. Only if
the user presses Yes the application should finish.
Recommendation: This is a useful exercise, but do not implement this feature in your applications.
Generally speaking, if you ask the user for confirmation about exiting the application and there are no
unsaved changes in the application, you are annoying him/her with redundant questions. Do not do

Last changed: 07 January 2014

this. Only ask the user for exit confirmation if there are unsaved changes and inform the user that this
is the reason they are being asked.
Follow-up Question: You should be able to answer the following question if you read the API/tutorial
documentation carefully. What is the difference between a modal and a non-modal dialogue box?
Answer:

Exercise 6 (advanced)
It is good practice to design a user interface that prevents the user from making errors. One way of
doing this is by ensuring that there are no options available that are not applicable to the current situation. Components can be disabled (greyed out) by using their setEnabled() method.
Extend your application such that the Cut and Copy options are disabled whenever no text is selected!
Hint: You may wish to study the API documentation for CaretListener and how this is used by
JTextArea.

I hope you find the lecture material, handouts, exercises, SGTs, and labs useful. However, if you have
any comments (good or bad), please do not hesitate to contact me at steffen.zschaler@kcl.ac.uk or
come see me during my office hours.

Last changed: 07 January 2014

Das könnte Ihnen auch gefallen