Introduction to Java Programming, Sixth Edition, Y. Daniel Liang
Chapter 12 GUI Basics
Section 12.2 GUI Components
1
Swing components that don?t rely on native GUI are referred to as ___________.
A.
lightweight components
B.
heavyweight components
C.
GUI components
D.
non-GUI components
2
__________ are referred to as heavyweight components.
A.
AWT components
B.
Swing components
C.
GUI components
D.
Non-GUI components
Section 12.3 The Java GUI API
3
Which of the following statements are true?
A.
To distinguish new Swing component classes from their AWT counterparts, Swing GUI component classes are named with a prefix J.
B.
All Swing GUI components are lightweight.
C.
A user interface object such as (button, list) can appear in one container.
D.
A container such as JFrame is also a component.
E.
Every instance of Component can be added to a container.
4
Which of the following classes is a heavy weight component?
A.
JButton
B.
JTextField
C.
JPanel
D.
JFrame
5
Which component cannot be added to a container?
A.
JPanel
B.
JButton
C.
JFrame
D.
JComponent
6
Which of the following are subclasses of java.awt.Component?
A.
Container classes
B.
Swing user interface classes
C.
Helper classes such as Color and Font
D.
Layout managers
7
What is best to describe the relationship between a container and a SWing GUI object in the container?
A.
Association
B.
Aggregation
C.
Composition
D.
Inheritance
8
What is best to describe the relationship between a container and a layout manager?
A.
Association
B.
Aggregation
C.
Composition
D.
Inheritance
9
What is best to describe the relationship between JComponent and JButton?
A.
Association
B.
Aggregation
C.
Composition
D.
Inheritance
10
What is best to describe the relationship between Component and Color?
 
A.
Association
B.
Aggregation
C.
Composition
D.
Inheritance
11
What is best to describe the relationship between Component and Font?
A.
Association
B.
Aggregation
C.
Composition
D.
Inheritance
12
Which of the following classes are in the java.awt package?
A.
Color
B.
Font
C.
Component
D.
JFrame
E.
JComponent
Section 12.4 Frames
13
Analyze the following code.
import java.awt.*;
import javax.swing.*;
public class Test {
   public static void main(String[] args) {
     Component c = new JButton("OK");
     JFrame frame = new JFrame("My Frame");
     frame.add(c);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
   }
}
A.
You cannot assign a JButton to a variable of java.awt.Component.
B.
You can only add c to a container because c's type is Component.
C.
You cannot add a Swing component directly to a JFrame using add(c) prior to JDK 1.4, but it is OK in JDK 1.5.
D.
You cannot create a JFrame using new JFrame("My Frame").
14
Analyze the following code.
import java.awt.*;
import javax.swing.*;
public class Test {
   public static void main(String[] args) {
     JFrame frame = new JFrame("My Frame");
     frame.add(new JButton("OK"));
     frame.add(new JButton("Cancel"));
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setSize(200, 200);
     frame.setVisible(true);
   }
}
A.
Only button OK is displayed.
B.
Only button Cancel is displayed.
C.
Both button OK and button Cancel are displayed and button OK is displayed on the left side of button OK.
D.
Both button OK and button Cancel are displayed and button OK is displayed on the right side of button OK.
15
How many frames are displayed?
import javax.swing.*;
public class Test {
   public static void main(String[] args) {
     JFrame f1 = new JFrame("My Frame");
     JFrame f2 = f1;
     JFrame f3 = f2;
     f1.setVisible(true);
     f2.setVisible(true);
     f3.setVisible(true);
   }
}
A.
1.
B.
2.
C.
3.
D.
0.
16
How many frames are displayed?
import javax.swing.*;
public class Test extends JFrame {
   public static void main(String[] args) {
     JFrame f1 = new Test();
     JFrame f2 = new Test();
     JFrame f3 = new Test();
     f1.setVisible(true);
     f2.setVisible(true);
     f3.setVisible(true);
   }
}
A.
1.
B.
2.
C.
3.
D.
0.
17
Which of the following statement is for terminating the program when closing the frame?
A.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
B.
frame.setDefaultCloseOperation(null)
C.
frame.setDefaultCloseOperation(JFrame.STOP_ON_CLOSE)
D.
frame.setDefaultCloseOperation(JFrame.TERMINATE_ON_CLOSE)
18
Which of the following statement is for placing the frame's upper left corner to (200, 100)?
A.
frame.setLocation(100, 100)
B.
frame.setLocation(100, 200)
C.
frame.setLocation(200, 100)
D.
frame.setLocation(200, 200)
Section 12.5 Layout Managers
19
What layout manager should you use so that every component occupies the same size in the container?
A.
a FlowLayout
B.
a GridLayout
C.
a BorderLayout
D.
None
20
What should you use to position a Button within an application Frame so that the size of the Button is NOT affected by the Frame size?
A.
a FlowLayout
B.
a GridLayout
C.
the center area of a BorderLayout
D.
the East or West area of a BorderLayout
E.
the North or South area of a BorderLayout
21
Suppose a JFrame uses the GridLayout(2, 2). If you add six buttons to the frame, how many columns are displayed?
A.
1
B.
2
C.
3
D.
4
22
Suppose a JFrame uses the GridLayout(0, 2). If you add six buttons to the frame, how many columns are displayed?
A.
1
B.
2
C.
3
D.
4
23
Suppose a JFrame uses the GridLayout(2, 0). If you add six buttons to the frame, how many columns are displayed?
A.
1
B.
2
C.
3
D.
4
24
To set a FlowLayout in panel jp, you can use the method __________.
A.
jp.setLayout(new FlowLayout());
B.
jp.setLayout(new FlowLayout(FlowLayout.CENTER));
C.
jp.setLayout(new FlowLayout(FlowLayout.center));
D.
jp.setLayout(FlowLayout());
E.
a or b
25
The default layout out of a contentPane in a JFrame is __________.
A.
FlowLayout
B.
GridLayout
C.
BorderLayout
D.
None
Section 12.6 The Color Class
26
_____________ creates a color object.
A.
new Color(0, 0, 0)
B.
new Color(0, 266, 0)
C.
new Color(255, 255, 255)
D.
new Color(1, 2, 3)
27
The method __________ sets the background color to yellow in JFrame f.
A.
setBackground(Color.yellow)
B.
f.setBackground(Color.YELLOW)
C.
f.setBackground(Color.yellow)
D.
setBackground(Color.YELLOW)
E.
f.setBackGround(Color.yellow)
28
The method __________ sets the foreground color to yellow in JFrame f.
A.
setForeground(Color.yellow)
B.
f.setForeground(Color.YELLOW)
C.
f.setForeground(Color.yellow)
D.
setForeground(Color.YELLOW)
E.
f.setForeGround(Color.yellow)
29
Which color is the darkest?
A.
new Color(0, 0, 0)
B.
new Color(10, 0, 0)
C.
new Color(20, 0, 0)
D.
new Color(30, 0, 0)
E.
f.setForeGround(Color.yellow)
Section 12.7 The Font Classe
30
The method __________ sets the font (Helvetica, 20-point bold) in component C.
A.
c.setFont(new Font("Helvetica", Font.bold, 20))
B.
c.setFont(new Font("helvetica", BOLD, 20))
C.
c.setFont(Font("Helvetica", Font.BOLD, 20))
D.
c.setFont(new Font("Helvetica", Font.BOLD, 20))
31
To specify a font to be bold and italic, use the font style value _________
A.
Font.PLAIN
B.
Font.BOLD
C.
Font.ITALIC
D.
Font.BOLD + Font.ITALIC
Section 12.8 Using Panels as Sub-Containers
32
The default layout out of a JPanel is __________.
A.
FlowLayout
B.
GridLayout
C.
BorderLayout
D.
None
33
To create a JPanel of the BorderLayout, use ______________.
A.
JPanel p = new JPanel()
B.
JPanel p = new JPanel(BorderLayout());
C.
JPanel p = new JPanel(new BorderLayout());
D.
JPanel p = new JPanel().setLayout(new BorderLayout());
34
To add a component c to a JPanel p, use _________.
A.
p.add(c)
B.
p.getContentPane(c)
C.
p.insert(c)
D.
p.append(c)
Section 12.9 Common Features of Swing GUI Components
35
Can you use the setBackground method to set a back ground color for _____?
A.
Component
B.
Container
C.
JComponent
D.
JButton
E.
JLabel
36
Can you use the getWidth method to get a width for _____?
A.
Component
B.
Container
C.
JComponent
D.
JButton
E.
JLabel
37
Can you use the setBorder method to set a border for _____?
A.
Component
B.
Container
C.
JComponent
D.
JButton
E.
JLabel
38
Can you use the setToolTip method to set a tool tip for _____?
A.
Component
B.
Container
C.
JComponent
D.
JButton
E.
JLabel
39
You can use methods ___________ on any instance of java.awt.Component.
A.
setBackground
B.
getBackground
C.
getWidth
D.
getHeight
E.
setBorder
40
Analyze the following code:
import javax.swing.*;
public class Test extends JFrame {
   private JButton jbtOK = new JButton("OK");
  
   public static void main(String[] args) {
     // Create a frame and set its properties
     JFrame frame = new Test();
     frame.setTitle("Logic Error");
     frame.setSize(200, 100);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
   }
   public Test() {
     jbtOK.setToolTipText("This is a button");
     add(new JButton("OK"));
   }
}
A.
The tool tip text is displayed when you move the mouse on the button.
B.
The tool tip text will be displayed if you replace add(new JButton("OK")) with add(jbtOK).
C.
The tool tip text will be displayed if you swap the two lines in the Test constructor.
D.
The tool tip text will be displayed if you replace add(new JButton("OK")) with add(jbtOK = new JButton("OK")).
41
You can use methods ___________ on any instance of java.awt.Component.
A.
setForeground
B.
setBackground
C.
setFont
D.
getFont
E.
setLayout
42
Show the output of the following code?
import javax.swing.*;
public class Test {
   public static void main(String[] args) {
     JButton jbtOK = new JButton("OK");
     System.out.print(jbtOK.isVisible() + ", ");
     JFrame frame = new JFrame();
     System.out.println(frame.isVisible());
   }
}
A.
true, true
B.
true, false
C.
false, true
D.
false, false
Section 12.10 Image Icons
43
To create an image icon for a file in c:\book\image\icon, use ____________.
A.
new ImageIcon("c:\book\image\icon");
B.
new ImageIcon('c:\book\image\icon');
C.
new ImageIcon("c:\\book\\image\\icon");
D.
new ImageIcon('c:\\book\\image\\icon');
E.
new ImageIcon("c:\\book\image\\icon") if the c:\book is in the class path.