Deck 15: Advanced Gui Topics

Full screen (f)
exit full mode
Question
A ____ is placed at the top of a container and contains user options.

A) glass pane
B) menu bar
C) content pane
D) containment hierarchy
Use Space or
up arrow
down arrow
to flip the card.
Question
What is the parent class of JPanel?

A) Object
B) Component
C) JComponent
D) Container
Question
Use the ____ layout manager when you need to add components that are displayed one at a time.

A) BorderLayout
B) GridLayout
C) GridBagLayout
D) CardLayout
Question
When you create a simple scroll pane using the constructor that takes no arguments, horizontal and vertical scroll bars appear only if they are needed.
Question
With ____, a redrawn JPanel is displayed only when it is complete; this provides the viewer with updated screens that do not flicker while being redrawn.

A) single buffering
B) double buffering
C) auto filtering
D) double framing
Question
If you do not specify alignment, Components are right-aligned in a FlowLayout Container by default.
Question
The following statement produces a dark purple color that has green and blue components, but no red:
Color darkPurple = new Color(100, 0, 100);
Question
When components in a Swing UI require more display area than they have been allocated, you can use a ____ container to hold the components and allow the user to display the components using scroll bars.

A) JScrollingPane
B) ScrollLayout
C) JPanel
D) JScrollPane
Question
A common mistake when using BorderLayout is to add a Component to a content pane or frame without naming a region, resulting in some of the components not being visible.
Question
The FlowLayout class contains ____ constants you can use to align Components with a Container.

A) two
B) three
C) five
D) six
Question
The Color class contains ____ predefined color constants.

A) 7
B) 12
C) 13
D) 15
Question
The wildcard in the import java.awt.* statement imports all types in the java.awt package, including java.awt.Color and java.awt.Font.
Question
Java automatically converts the add(), remove(), and setLayoutManager() statements to more complete versions that include _____.

A) getContentPane()
B) glassPane()
C) getJFrame()
D) addAll()
Question
Layout managers are interface classes that align components to prevent crowding or overlapping.
Question
You use FlowLayout when you want fixed rows and columns of components, and when you want the components to fill available space.
Question
An additional layered pane exists above the root pane, but it is not often used explicitly by Java programmers.
Question
Use the ____ layout manager when you add components to a maximum of five sections.

A) BorderLayout
B) GridLayout
C) GridBagLayout
D) CardLayout
Question
Any component you place on the screen has a vertical, or x-axis, position as well as a horizontal, or y-axis, position in the window.
Question
With GridLayout, you can specify the number of rows only; you can use ____ for the number of columns and let the layout manager determine the number of columns.

A) null
B) 0
C) 99
D) -1
Question
When you use BorderLayout, you are required to add components into each of the five regions.
Question
Each JMenu can contain options, called JMenuItems, or can contain submenus that are ____.

A) JMenuBars
B) JMenuChildren
C) JSubMenus
D) JMenus
Question
A(n) ____________________ is a plain, borderless surface that can hold lightweight UI components.
Question
You use the getModifiers() method with an InputEvent object, and you can assign the return value to a(n) ____ variable.

A) String
B) boolean
C) int
D) double
Question
____________________ are lists of user options; they are commonly added features in GUI programs.
Question
Which of the following statements will set the background color of a button named stop to a color of red?

A) stop.setBackground(Color.RED);
B) stop.Backcolor = RED;
C) red.setBackground(Color.RED);
D) setBack.stop.Color.RED;
Question
Which of the following is NOT a method of the KeyListener interface?

A) keyTyped()
B) keyPressed()
C) keyClicked()
D) keyReleased()
Question
Clicking a component results in a(n) ____.

A) ItemEvent
B) WindowEvent
C) ActionEvent
D) MouseEvent
Question
The ____________________ manager is the default manager class for all content panes.
Question
The parent class for all event objects is named ____, which descends from the Object class.

A) EventObject
B) Event
C) ParentEvent
D) AWTEvent
Question
The focusGained(FocusEvent) handler is defined in the ____ interface.

A) FocusListener
B) ComponentListener
C) AdjustmentListener
D) ActionListener
Question
When you type "A", two ____ key codes are generated: Shift and "a".

A) action
B) virtual
C) event
D) default
Question
A(n) ____ implements all methods in an interface and provides an empty body for each method.

A) action key
B) viewport
C) mnemonic
D) adapter class
Question
Which of the following statements will correctly add a JMenuBar named myBar to a JFrame?

A) myBar = setJMenuBar
B) setJMenuBar(myBar)
C) JMenuBar.setJMenuBar(myBar)
D) JMenuBar = new JMenuBar(myBar)
Question
If you wanted to see the x-coordinate of a user click, you would use the ____ method of the MouseEvent class.

A) getClick()
B) getX()
C) getY()
D) getHoriz()
Question
A(n) _________________________ is a tree of components that has a top-level container as its root (that is, at its uppermost level).
Question
The parent class of MouseEvent is ____.

A) AWTEvent
B) EventObject
C) InputEvent
D) UserEvent
Question
The state of a JCheckBoxMenuItem or JRadioButtonMenuItem can be determined with the ____ method.

A) state()
B) getState()
C) getSelected()
D) isSelected()
Question
Clicking an item in a list box results in a(n) ____.

A) ItemEvent
B) WindowEvent
C) ActionEvent
D) MouseEvent
Question
You use the ____________________ interface when you are interested in actions the user initiates from the keyboard.
Question
The JMenus are added to the JMenuBar using the ____ method.

A) addMenu()
B) add()
C) addNewMenu()
D) setMenu()
Question
When do you need to worry about using the getContentPane() method?
Question
How would you arrange columns into equal rows and columns? Provide an example.
Question
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
A type of event generated by mouse manipulation
Question
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
Use this class to greatly increase the number of possible component arrangements
Question
What is an adapter class?
Question
What are the four methods defined in the Component class for adding event listeners?
Question
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
Use when you need to set size, placement, and alignment constraints for every component that you add
Question
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
Allows the user to scroll initially invisible parts of the pane into view
Question
Describe the difference between an ActionEvent and a MouseEvent.
Question
How do you find the red, green, and blue components of a color?
Question
Describe the way in which components expand to fill their layout areas in FlowLayout and BorderLayout.
Question
Write the statement to create a Color object named myGray with red, green, and blue values of 100 each.
Question
Describe the two CardLayout constructors.
Question
To force the display of a scroll bar, you can use class variables defined in the ScrollPaneConstants class. What are they?
Question
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
Use when you need to add components into a grid of rows and columns; each component is the same size
Question
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
Resides above the content pane
Question
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
The default size of a component
Question
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
Can be used with the setBackground() and setForeground() methods of the Component class
Question
What is a mnemonic? Provide an example of adding a mnemonic.
Question
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
A key that does not generate a character, such as a function key
Question
Write the code to create a scroll pane named myScroll that will display an image named scenery with a vertical scroll bar always present and a horizontal scroll bar as needed.
Question
import javax.swing.*;
import java.awt.*;
public class JDemoGridLayout extends JFrame
{
private JButton b1 = new JButton("Button 1");
private JButton b2 = new JButton("Button 2");
private JButton b3 = new JButton("Button 3");
private JButton b4 = new JButton("Button 4");
private JButton b5 = new JButton("Button 5");
____________________________________________
private Container con = getContentPane();
public JDemoGridLayout()
{
con.setLayout(layout);
con.add(b1);
con.add(b2);
con.add(b3);
con.add(b4);
con.add(b5);
setSize(200, 200);
}
public static void main(String[] args)
{
JDemoGridLayout frame = new JDemoGridLayout();
frame.setVisible(true);
}
}
Using the above code, write the statement in the shaded line provided to establish a GridLayout with three horizontal rows and two vertical columns, with horizontal and vertical gaps of five pixels each.
Question
Write the statement to set the background color of a button named goBtn to a color of green.
Question
Write the statement to establish a GridLayout with three horizontal rows and six vertical columns in a Container named myGrid.
Question
import java.awt.*;
import javax.swing.*;
import java.awt.Color;
public class Checkerboard extends JFrame
{
private final int ROWS = 8;
private final int COLS = 8;
private final int GAP = 2;
private final int NUM = ROWS * COLS;
private int x;
private JPanel pane = new JPanel
(new GridLayout(ROWS, COLS, GAP, GAP));
private JPanel[] panel = new JPanel[NUM];
private Color color1 = Color.WHITE;
private Color color2 = Color.BLUE;
private Color tempColor;
public Checkerboard()
{
super("Checkerboard");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(pane);
for(x = 0; x < NUM; ++x)
{
panel[x] = new JPanel();
pane.add(panel[x]);
if(x % COLS == 0)
{
tempColor = color1;
color1 = color2;
color2 = tempColor;
}
if(x % 2 == 0)
panel[x].setBackground(color1);
else
panel[x].setBackground(color2);
}
}
public static void main(String[] args)
{
Checkerboard frame = new Checkerboard();
final int SIZE = 300;
frame.setSize(SIZE, SIZE);
frame.setVisible(true);
}
}
The above code creates a loop to fill even-positioned squares with one color and odd-positioned squares with another color, resulting in a checkerboard pattern. Describe how JPanels and a GridLayout are used to achieve this effect.
Question
import javax.swing.*;
import java.awt.*;
public class JDemoBorderLayout extends JFrame
{
private JButton nb = new JButton("North Button");
private JButton sb = new JButton("South Button");
private JButton eb = new JButton("East Button");
private JButton wb = new JButton("West Button");
private JButton cb = new JButton("Center Button");
private Container con = getContentPane();
public JDemoBorderLayout()
{
con.setLayout(new BorderLayout());
_________________________________________
_________________________________________
_________________________________________
_________________________________________
_________________________________________
setSize(400, 150);
}
public static void main(String[] args)
{
JDemoBorderLayout frame = new JDemoBorderLayout();
frame.setVisible(true);
}
}
Using the above code, write the statements in the shaded lines provided to add components to each of the five regions (north, south, east, west and center).
Question
List and describe the four constructors that can be used when creating a JPanel object.
Question
Using a FlowLayout object named myLayout, write the statement to set the layout of a content pane to left.
Question
import java.awt.*;
import javax.swing.*;
import java.awt.Color;
public class JFrameWithColor extends JFrame
{
private final int SIZE = 180;
private Container con = getContentPane();
private JButton button =
new JButton("Press Me");
public JFrameWithColor()
{
super("Frame");
setSize(SIZE, SIZE);
con.setLayout(new FlowLayout());
con.add(button);
_____________________________________
_____________________________________
_____________________________________
}
public static void main(String[] args)
{
JFrameWithColor frame =
new JFrameWithColor();
frame.setVisible(true);
}
}
In the first shaded line provided, write the statement to set the background color of the JFrame's content pane to gray. Using the remaining two shaded lines, write the statements to set the JButton foreground color to white and the background color to blue.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/69
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 15: Advanced Gui Topics
1
A ____ is placed at the top of a container and contains user options.

A) glass pane
B) menu bar
C) content pane
D) containment hierarchy
B
2
What is the parent class of JPanel?

A) Object
B) Component
C) JComponent
D) Container
C
3
Use the ____ layout manager when you need to add components that are displayed one at a time.

A) BorderLayout
B) GridLayout
C) GridBagLayout
D) CardLayout
D
4
When you create a simple scroll pane using the constructor that takes no arguments, horizontal and vertical scroll bars appear only if they are needed.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
5
With ____, a redrawn JPanel is displayed only when it is complete; this provides the viewer with updated screens that do not flicker while being redrawn.

A) single buffering
B) double buffering
C) auto filtering
D) double framing
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
6
If you do not specify alignment, Components are right-aligned in a FlowLayout Container by default.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
7
The following statement produces a dark purple color that has green and blue components, but no red:
Color darkPurple = new Color(100, 0, 100);
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
8
When components in a Swing UI require more display area than they have been allocated, you can use a ____ container to hold the components and allow the user to display the components using scroll bars.

A) JScrollingPane
B) ScrollLayout
C) JPanel
D) JScrollPane
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
9
A common mistake when using BorderLayout is to add a Component to a content pane or frame without naming a region, resulting in some of the components not being visible.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
10
The FlowLayout class contains ____ constants you can use to align Components with a Container.

A) two
B) three
C) five
D) six
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
11
The Color class contains ____ predefined color constants.

A) 7
B) 12
C) 13
D) 15
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
12
The wildcard in the import java.awt.* statement imports all types in the java.awt package, including java.awt.Color and java.awt.Font.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
13
Java automatically converts the add(), remove(), and setLayoutManager() statements to more complete versions that include _____.

A) getContentPane()
B) glassPane()
C) getJFrame()
D) addAll()
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
14
Layout managers are interface classes that align components to prevent crowding or overlapping.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
15
You use FlowLayout when you want fixed rows and columns of components, and when you want the components to fill available space.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
16
An additional layered pane exists above the root pane, but it is not often used explicitly by Java programmers.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
17
Use the ____ layout manager when you add components to a maximum of five sections.

A) BorderLayout
B) GridLayout
C) GridBagLayout
D) CardLayout
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
18
Any component you place on the screen has a vertical, or x-axis, position as well as a horizontal, or y-axis, position in the window.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
19
With GridLayout, you can specify the number of rows only; you can use ____ for the number of columns and let the layout manager determine the number of columns.

A) null
B) 0
C) 99
D) -1
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
20
When you use BorderLayout, you are required to add components into each of the five regions.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
21
Each JMenu can contain options, called JMenuItems, or can contain submenus that are ____.

A) JMenuBars
B) JMenuChildren
C) JSubMenus
D) JMenus
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
22
A(n) ____________________ is a plain, borderless surface that can hold lightweight UI components.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
23
You use the getModifiers() method with an InputEvent object, and you can assign the return value to a(n) ____ variable.

A) String
B) boolean
C) int
D) double
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
24
____________________ are lists of user options; they are commonly added features in GUI programs.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
25
Which of the following statements will set the background color of a button named stop to a color of red?

A) stop.setBackground(Color.RED);
B) stop.Backcolor = RED;
C) red.setBackground(Color.RED);
D) setBack.stop.Color.RED;
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
26
Which of the following is NOT a method of the KeyListener interface?

A) keyTyped()
B) keyPressed()
C) keyClicked()
D) keyReleased()
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
27
Clicking a component results in a(n) ____.

A) ItemEvent
B) WindowEvent
C) ActionEvent
D) MouseEvent
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
28
The ____________________ manager is the default manager class for all content panes.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
29
The parent class for all event objects is named ____, which descends from the Object class.

A) EventObject
B) Event
C) ParentEvent
D) AWTEvent
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
30
The focusGained(FocusEvent) handler is defined in the ____ interface.

A) FocusListener
B) ComponentListener
C) AdjustmentListener
D) ActionListener
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
31
When you type "A", two ____ key codes are generated: Shift and "a".

A) action
B) virtual
C) event
D) default
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
32
A(n) ____ implements all methods in an interface and provides an empty body for each method.

A) action key
B) viewport
C) mnemonic
D) adapter class
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
33
Which of the following statements will correctly add a JMenuBar named myBar to a JFrame?

A) myBar = setJMenuBar
B) setJMenuBar(myBar)
C) JMenuBar.setJMenuBar(myBar)
D) JMenuBar = new JMenuBar(myBar)
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
34
If you wanted to see the x-coordinate of a user click, you would use the ____ method of the MouseEvent class.

A) getClick()
B) getX()
C) getY()
D) getHoriz()
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
35
A(n) _________________________ is a tree of components that has a top-level container as its root (that is, at its uppermost level).
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
36
The parent class of MouseEvent is ____.

A) AWTEvent
B) EventObject
C) InputEvent
D) UserEvent
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
37
The state of a JCheckBoxMenuItem or JRadioButtonMenuItem can be determined with the ____ method.

A) state()
B) getState()
C) getSelected()
D) isSelected()
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
38
Clicking an item in a list box results in a(n) ____.

A) ItemEvent
B) WindowEvent
C) ActionEvent
D) MouseEvent
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
39
You use the ____________________ interface when you are interested in actions the user initiates from the keyboard.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
40
The JMenus are added to the JMenuBar using the ____ method.

A) addMenu()
B) add()
C) addNewMenu()
D) setMenu()
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
41
When do you need to worry about using the getContentPane() method?
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
42
How would you arrange columns into equal rows and columns? Provide an example.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
43
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
A type of event generated by mouse manipulation
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
44
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
Use this class to greatly increase the number of possible component arrangements
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
45
What is an adapter class?
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
46
What are the four methods defined in the Component class for adding event listeners?
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
47
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
Use when you need to set size, placement, and alignment constraints for every component that you add
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
48
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
Allows the user to scroll initially invisible parts of the pane into view
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
49
Describe the difference between an ActionEvent and a MouseEvent.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
50
How do you find the red, green, and blue components of a color?
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
51
Describe the way in which components expand to fill their layout areas in FlowLayout and BorderLayout.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
52
Write the statement to create a Color object named myGray with red, green, and blue values of 100 each.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
53
Describe the two CardLayout constructors.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
54
To force the display of a scroll bar, you can use class variables defined in the ScrollPaneConstants class. What are they?
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
55
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
Use when you need to add components into a grid of rows and columns; each component is the same size
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
56
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
Resides above the content pane
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
57
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
The default size of a component
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
58
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
Can be used with the setBackground() and setForeground() methods of the Component class
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
59
What is a mnemonic? Provide an example of adding a mnemonic.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
60
Match each term with the correct statement below.
a.GridBagLayout
f.action key
b.JScrollPane
g.Color class
c.JPanel
h.MouseEvent
d.glass pane
i.preferred size
e.GridLayout
A key that does not generate a character, such as a function key
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
61
Write the code to create a scroll pane named myScroll that will display an image named scenery with a vertical scroll bar always present and a horizontal scroll bar as needed.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
62
import javax.swing.*;
import java.awt.*;
public class JDemoGridLayout extends JFrame
{
private JButton b1 = new JButton("Button 1");
private JButton b2 = new JButton("Button 2");
private JButton b3 = new JButton("Button 3");
private JButton b4 = new JButton("Button 4");
private JButton b5 = new JButton("Button 5");
____________________________________________
private Container con = getContentPane();
public JDemoGridLayout()
{
con.setLayout(layout);
con.add(b1);
con.add(b2);
con.add(b3);
con.add(b4);
con.add(b5);
setSize(200, 200);
}
public static void main(String[] args)
{
JDemoGridLayout frame = new JDemoGridLayout();
frame.setVisible(true);
}
}
Using the above code, write the statement in the shaded line provided to establish a GridLayout with three horizontal rows and two vertical columns, with horizontal and vertical gaps of five pixels each.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
63
Write the statement to set the background color of a button named goBtn to a color of green.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
64
Write the statement to establish a GridLayout with three horizontal rows and six vertical columns in a Container named myGrid.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
65
import java.awt.*;
import javax.swing.*;
import java.awt.Color;
public class Checkerboard extends JFrame
{
private final int ROWS = 8;
private final int COLS = 8;
private final int GAP = 2;
private final int NUM = ROWS * COLS;
private int x;
private JPanel pane = new JPanel
(new GridLayout(ROWS, COLS, GAP, GAP));
private JPanel[] panel = new JPanel[NUM];
private Color color1 = Color.WHITE;
private Color color2 = Color.BLUE;
private Color tempColor;
public Checkerboard()
{
super("Checkerboard");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(pane);
for(x = 0; x < NUM; ++x)
{
panel[x] = new JPanel();
pane.add(panel[x]);
if(x % COLS == 0)
{
tempColor = color1;
color1 = color2;
color2 = tempColor;
}
if(x % 2 == 0)
panel[x].setBackground(color1);
else
panel[x].setBackground(color2);
}
}
public static void main(String[] args)
{
Checkerboard frame = new Checkerboard();
final int SIZE = 300;
frame.setSize(SIZE, SIZE);
frame.setVisible(true);
}
}
The above code creates a loop to fill even-positioned squares with one color and odd-positioned squares with another color, resulting in a checkerboard pattern. Describe how JPanels and a GridLayout are used to achieve this effect.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
66
import javax.swing.*;
import java.awt.*;
public class JDemoBorderLayout extends JFrame
{
private JButton nb = new JButton("North Button");
private JButton sb = new JButton("South Button");
private JButton eb = new JButton("East Button");
private JButton wb = new JButton("West Button");
private JButton cb = new JButton("Center Button");
private Container con = getContentPane();
public JDemoBorderLayout()
{
con.setLayout(new BorderLayout());
_________________________________________
_________________________________________
_________________________________________
_________________________________________
_________________________________________
setSize(400, 150);
}
public static void main(String[] args)
{
JDemoBorderLayout frame = new JDemoBorderLayout();
frame.setVisible(true);
}
}
Using the above code, write the statements in the shaded lines provided to add components to each of the five regions (north, south, east, west and center).
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
67
List and describe the four constructors that can be used when creating a JPanel object.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
68
Using a FlowLayout object named myLayout, write the statement to set the layout of a content pane to left.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
69
import java.awt.*;
import javax.swing.*;
import java.awt.Color;
public class JFrameWithColor extends JFrame
{
private final int SIZE = 180;
private Container con = getContentPane();
private JButton button =
new JButton("Press Me");
public JFrameWithColor()
{
super("Frame");
setSize(SIZE, SIZE);
con.setLayout(new FlowLayout());
con.add(button);
_____________________________________
_____________________________________
_____________________________________
}
public static void main(String[] args)
{
JFrameWithColor frame =
new JFrameWithColor();
frame.setVisible(true);
}
}
In the first shaded line provided, write the statement to set the background color of the JFrame's content pane to gray. Using the remaining two shaded lines, write the statements to set the JButton foreground color to white and the background color to blue.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 69 flashcards in this deck.