CS 1401, Quiz #7, Solutions

Date: Tuesday, March 7, 2007
Name: ____________________________________________________________
Class section (9:00 am or 10:30 am): _______________________________

1. For each of the following topics, write "yes" or "no" depending on whether this topic is covered in Chapter 7, the chapter that you were supposed to read before the class:

user-defined methods yes

2-dimensional arrays no

void methods yes

exceptions no

2. Draw a pane that will appear as a result of the following code. What will happen is you do not use setVisible(true)?

firstNameL = new JLabel("Enter your first name:", SwingConstants.CENTER);
lastNameL = new JLabel("Enter your last name:", SwingConstants.CENTER);
gradeInQuiz7L = new JLabel("Enter your grade:", SwingConstants.CENTER);

firstNameTF = new JTextField(10);
lastNameTF  = new JTextField(10);
gradeInQuiz7TF  = new JTextField(10);

Container pane = getContentPane();

pane.setLayout(new GridLayout(2,3));

pane.add(firstNameL);
pane.add(lastNameL);
pane.add(gradeInQuiz7L);
pane.add(firstNameTF);
pane.add(lastNameTF);
pane.add(gradeInQuiz7TF);

setSize(400, 100);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

Solution:

 ------------------------------------------------------------------------
|                                                                        |
|   Enter your first name:   Enter your last name:    Enter your grade:  |
|   ---------------------    ---------------------    ----------------   |
|  |                     |  |                     |  |                |  |
|   ---------------------    ---------------------    ----------------   |
|                                                                        |
 ------------------------------------------------------------------------

Note that the grid layout manager sets the size of all the managed GUI
components to be the same. All labels and text fields would have the
same height (100/2) and they would all have the same width (400/3).

If we do not use setVisible(true), the pane will not be visible on the
screen.