Introduction to Java Programming, Sixth Edition, Y. Daniel Liang

Chapter 15 Creating User Interfaces


Section 15.2 Buttons
1  Which of the following is true?

A. All the methods in the JButton class are inherited from the javax.swing.AbstractButton class.
B. You can create a JButton by specifying an icon and text.
C. You can create a JButton by specifying an icon.
D. You can create a JButton by a text.
E. You can create a JButton using its default constructor.

2  The method __________ gets the text (or caption) of the button jbt.

A. jbt.text()
B. jbt.getText()
C. jbt.findText()
D. jbt.retrieveText().

3  The method __________ creates a IconImage for file c:\image\us.gif.

A. new ImageIcon("c:\image\us.gif");
B. new Icon("c:\image\us.gif");
C. new ImageIcon("c:\\image\\us.gif");
D. new Icon("c:\\image\\us.gif");

4  Which of the following are valid methods on the button jbt.

A. jbt.setMnemonic("A");
B. jbt.setMnemonic('A');
C. jbt.setIconTextGap(50);
D. jbt.setTextGap(50);

5  The method __________ specifies that the text and icon are horizontally aligned to the right in the button jbt.

A. jbt.setVerticalTextPosition(JButton.LEFT)
B. jbt.setHorizontalTextPosition(JButton.LEFT)
C. jbt.setHorizontalTextPosition(JButton.RIGHT)
D. jbt.setHorizontalAlignment(JButton.RIGHT)
E. jbt.setHorizontalAlignment(JButton.LEFT)

6  The method __________ specifies that the text is placed on the right of the icon in the button jbt.

A. jbt.setVerticalTextPosition(JButton.LEFT)
B. jbt.setHorizontalTextPosition(JButton.LEFT)
C. jbt.setHorizontalTextPosition(JButton.RIGHT)
D. jbt.setHorizontalAlignment(JButton.RIGHT)
E. jbt.setHorizontalAlignment(JButton.LEFT)

7  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);

     add(p1, BorderLayout.NORTH);
     add(p2, BorderLayout.SOUTH);
     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)

8  Analyze the following code:

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

public class Test extends JFrame {
   public Test() {
     setLayout(new FlowLayout());
     add(new JButton("Java"));
     add(new JButton("Java"));
     add(new JButton("Java"));
     add(new JButton("Java"));
   }

   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. One button is displayed with the text "Java".
B. Two buttons are displayed with the same text "Java".
C. Three buttons are displayed with the same text "Java".
D. Four buttons are displayed with the same text "Java".

9  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);
     add(jbt1, BorderLayout.NORTH);
     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 15.3 Check Boxes
10  _________ checks whether the JCheckBox jchk is selected.

A. jchk.getSelected()
B. jchk.selected()
C. jchk.isSelected().
D. jchk.select()

11  Which of the following statements are true?

A. All the methods in JCheckBox are inherited from javax.swing.AbstractButton.
B. All the methods in JCheckBox are also in JButton.
C. JButton and JCheckBox fire same types of events.
D. You can use an icon on JCheckBox.

12  Clicking a JCheckBox object generates __________ events.

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

Section 15.4 Radio Buttons
13  Which of the following statements are true?

A. All the methods in JRadioButton are inherited from javax.swing.AbstractButton.
B. All the methods in JRadioButton are also in JButton.
C. JButton and JRadioButton fire same types of events.
D. You can use an icon on JRadioButton.

14  Which of the following statements are true?

A. You can create an instance of ButtonGroup and add radio button to the instance to group the buttons.
B. ButtonGroup can be added to a container.
C. To check whether a radio button jrb is selected, use jrb.isSelected().
D. By default, when a radio button is created, the radio button is not selected.

15  Clicking a JRadioButton generates _____________ events.

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

Section 15.5 Labels
16  Which of the following statements are true?

A. All the methods in JLabel are inherited from javax.swing.AbstractButton.
B. You can set a mnemonic key in a JLabel.
C. You can create a label with both text and icon.
D. You can create a label with an icon.

17  The method __________ gets the text (or caption) of the label jlbl.

A. jlbl.text()
B. jlbl.getText()
C. jlbl.findText()
D. jlbl.retrieveText().

18  Which of the following are valid methods on the label jlbl.

A. jlbl.setMnemonic("A");
B. jlbl.setMnemonic('A');
C. jlbl.setIconTextGap(50);
D. jlbl.setTextGap(50);

19  The method __________ specifies that the text and icon are horizontally aligned to the right in the label jlbl.

A. jlbl.setVerticalTextPosition(JButton.LEFT)
B. jlbl.setHorizontalTextPosition(JButton.LEFT)
C. jlbl.setHorizontalTextPosition(JButton.RIGHT)
D. jlbl.setHorizontalAlignment(JButton.RIGHT)
E. jlbl.setHorizontalAlignment(JButton.LEFT)

20  The method __________ specifies that the text is placed on the right of the icon in the label jlbl.

A. jlbl.setVerticalTextPosition(JButton.LEFT)
B. jlbl.setHorizontalTextPosition(JButton.LEFT)
C. jlbl.setHorizontalTextPosition(JButton.RIGHT)
D. jlbl.setHorizontalAlignment(JButton.RIGHT)
E. jlbl.setHorizontalAlignment(JButton.LEFT)

21  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. jlbl.findText()

Section 15.6 Text Fields
22  ___________ can be used to enter or display a string.

A. A label
B. A button
C. A check box
D. A radio button
E. A text field

23  Which of the following statements are true?

A. You can specify a horizontal text alignment in a text field.
B. You can specify the number of columns in a text field.
C. You can disable editing on a text field.
D. YOu can create a text field with a specified text.

24  The method __________ gets the contents of the text field jtf.

A. jtf.getText(s)
B. jtf.getText()
C. jtf.getString()
D. jtf.findString()

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

A. jta.setText(s)
B. jta.appendText(s)
C. jta.append(s)
D. jta.insertText(s)

26  Which of the following statements are true?

A. You can specify a horizontal text alignment in a text area.
B. You can specify the number of columns in a text area.
C. You can disable editing on a text area.
D. YOu can create a text field with a specified text area.

27  To wrap a line in a text area jta, invoke ____________.

A. jta.setLineWrap(false)
B. jta.setLineWrap(true)
C. jta.WrapLine()
D. jta.wrapText()

28  To wrap a line in a text area jta on words, invoke ____________.

A. jta.setWrapStyleWord(false)
B. jta.setWrapStyleWord(true)
C. jta.wrapStyleWord()
D. jta.wrapWord()

29  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 15.8 Combo Boxes
30  How many items can be added into a JComboBox object?

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

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

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

32  _______________ returns the selected item on a JComboBox jcbo.

A. jcbo.getSelectedIndex()
B. jcbo.getSelectedItem()
C. jcbo.getSelectedIndices()
D. jcbo.getSelectedItems()

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

A. jcbo.add(s)
B. jcbo.addChoice(s)
C. jcbo.addItem(s)
D. jcbo.addObject(s).

34  Clicking a JComboBox object always generates __________ events.

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

35  Clicking a JComboBox object generates an ItemEvent event,

A. if an item is selected.
B. if a new item is selected.

Section 15.9 Lists
36  ____________ is a component that enables the user to choose a single value or multiple values.

A. A text field
B. A combo box
C. A list
D. A label

37  ______________ allows selections of multiple contiguous items without restrictions,

A. Single selection
B. Single-interval selection
C. Multiple-interval selection
D. default selection

38  ______________ sets the background of the selected item in list jlst to yellow.

A. jlst.setBackground(Color.YELLOW)
B. jlst.setSelectedBackground(Color.YELLOW)
C. jlst.setSelectionBackground(Color.YELLOW)
D. jlst.setSelectionbackground(Color.YELLOW)

39  Clicking a JList object generates __________ events.

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

Section 15.10 Scroll Bars
40  Which of the following statements are true?

A. Every Swing GUI component has a default constructor.
B. You can create a scroll bar by specifying its orientation.
C. JScrollBar fires an ActionEvent.
D. JScrollBar fires an AdjustmentEvent.
E. A listener of a JScrollBar must implement AdjustmentEvent and the adjustmentValueChanged method.

41  The following are properties of a JScrollBar.

A. minimum
B. maximum
C. orientation
D. visibleAmount

Section 15.11 Sliders
42  Which of the following statements are true?

A. You can create a JSlider bar by specifying its orientation.
B. JSlider fires an ActionEvent.
C. JSlider fires an AdjustmentEvent.
D. JSlider fires a javax.swing.event.ChangeEvent.
E. A listener of a JSlider must implement ChangeEvent and the stateChanged method.

43  The following are properties of a JSlider.
A. minimum
B. maximum
C. orientation
D. visibleAmount