Developed by Y. Daniel Liang using Java Servlets (Current time is Sat Nov 21 19:43:06 EST 2009)

The questions are to accompany Introduction to Java Programming, 4E by Y. Daniel Liang. Please report errors to y.daniel.liang@gmail.com.

Introduction to Java Programming, 4E has been superseded by Introduction to Java Programming, 5E.

Chapter 11: Creating User Interfaces

Section 11.3 Buttons
1   The method __________ gets the text (or caption) of the button jbt.

A. jbt.text()
B. jbt.getText()
C. jbt.findText()
D. None of the above.

2   Analyze the following code:

import javax.swing.*;
import java.awt.*;

public class Test extends JFrame {
   public Test() {
     ImageIcon usIcon = new ImageIcon("image/usIcon.gif");
     JButton jbt1 = new JButton(usIcon);
     JButton jbt2 = new JButton(usIcon);

     JPanel p1 = new JPanel();
     p1.add(jbt1);

     JPanel p2 = new JPanel();
     p2.add(jbt2);

     JPanel p3 = new JPanel();
     p2.add(jbt1);

     getContentPane().add(p1, BorderLayout.NORTH);
     getContentPane().add(p2, BorderLayout.SOUTH);
     getContentPane().add(p3, BorderLayout.CENTER);
   }

   public static void main(String[] args) {
     // Create a frame and set its properties
     JFrame frame = new Test();
     frame.setSize(200, 100);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
   }
}

A. Two buttons displayed with the same icon.
B. Three buttons displayed with the same icon.
C. Only jbt1 displays the icon and jbt2 does not display the icon.
D. (B) and (C)

3   Analyze the following code:

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;

public class Test extends JFrame {
   public Test() {
     Border border = new TitledBorder("My button");
     JButton jbt1 = new JButton("OK");
     JButton jbt2 = new JButton("Cancel");
     jbt1.setBorder(border);
     jbt2.setBorder(border);
     getContentPane().add(jbt1, BorderLayout.NORTH);
     getContentPane().add(jbt2, BorderLayout.SOUTH);
   }

   public static void main(String[] args) {
     JFrame frame = new Test();
     frame.setSize(200, 100);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
   }
}

A. Two buttons displayed with the same border.
B. Two buttons displayed, but only one button has the border.
C. The program has a syntax error because you assign new TitledBorder("My button") to a variable of the Border type.
D. The program has a runtime error because you cannot set a border on a button.

Section 11.4 Labels
4   The method __________ assigns the name Result to the Text of variable jlbl.

A. jlbl.setText("Result")
B. jlbl.newText("Result")
C. jlbl.text("Result")
D. None of the above.

Section 11.5 Text Fields
5   The method __________ gets the contents of the text field jtf.

A. jtf.getText(s)
B. jtf.getText()
C. jtf.getString()
D. None of the above.

Section 11.6 Text Areas
6   The method __________ appends a string s into the text area jta.

A. jta.setText(s)
B. jta.appendText(s)
C. jta.append(s)
D. None of the above.

7   The method __________ adds a text area jta to a scrollpane jScrollPane.

A. jScrollPane.add(jta)
B. jScrollPane.insert(jta)
C. jScrollPane.addItem(jta)
D. None of the above.

Section 11.7 Combo Boxes
8   How many items can be added into a JComboBox object?

A. 0
B. 1
C. 2
D. Unlimited

9   How many items can be selected from a JComboBox object at a time?

A. 0
B. 1
C. 2
D. Unlimited

10   The method __________ adds an item s into a JComboBox jcbo.

A. jcbo.add(s)
B. jcbo.addChoice(s)
C. jcbo.addItem(s)
D. None of the above.

11   Clicking a JComboBox object generates __________ events.

A. ActionEvent
B. ItemEvent
C. MouseEvent
D. WindowEvent

Section 11.8 Lists
12   Clicking a JList object generates __________ events.

A. ActionEvent and ItemEvent
B. ItemEvent and ComponentEvent
C. ComponentEvent and ContainerEvent
D. ActionEvent and ContainerEvent

Section 11.9 Check Boxes
13   Clicking a JCheckBox object generates __________ events.

A. ActionEvent
B. ItemEvent
C. ComponentEvent
D. ContainerEvent

Section 11.10 Radio Buttons
14   Clicking a JRadioButton generates _____________ events.

A. ActionEvent
B. ItemEvent
C. ComponentEvent
D. ContainerEvent
E. a and b

Section 11.13 Menus
15   The method __________ places a menu bar mb into the frame f.

A. f.setJMenu(mb)
B. f.add(mb)
C. f.setMenuBar(mb)
D. f.addBar(mb)

16   The method __________ places a menu mu into a menu bar mb.

A. mb.add(mu)
B. mb.addMenuItem(mu)
C. mb.addItem(mu)
D. None of the above.

17   The method __________ places a menu item mi into a menu mu.

A. mu.add(mi)
B. mu.addMenuItem(mi)
C. mu.addItem(mi)
D. None of the above.

18   A MenuItem object can generate __________ events.

A. ActionEvent
B. ItemEvent
C. ComponentEvent
D. ContainerEvent

19   The method __________ separates menu items in a menu mu.
A. mu.add('-')
B. mu.addSeparator()
C. Either a or b
D. None of the above.