In each of the following questions, choose the layout manager(s)
most naturally suited for the described layout. Assume that the
container controlled by the layout manager is a JPanel.
[Hint: Two sections that might help are components/componentsA
Visual Index to Swing Components and layout/usingchoosingTips
on Choosing a Layout Manager]
Question 1. The container has
one component that should take up as much space as possible
a. BorderLayout
b. GridLayout
c. GridBagLayout
d. a and b
e. b and c
Answer 1: d. BorderLayout and GridLayout
easily deal with this situation. Although you could use GridBagLayout,
it's much more complex than necessary.
Question 2. The container has a row of components that should all be displayed at the same size, filling the container&146;s entire area.
uiswing/QandE/Layout2-1 35059ALIGNNATURALSIZEFLAG3ALTLayout2-1 uiswing/QandE/Layout2-2 44481ALIGNNATURALSIZEFLAG3ALTLayout2-2a. FlowLayout
b. GridLayout
c. BoxLayout
d. a and b
Answer 2: b. This type of same-size layout &151;
whether in a row, a column, or a grid &151; is what GridLayout
is best at.
Question 3. The container displays
a number of components in a column, with any extra space going
between the first two components.
| uiswing/QandE/Layout3-1 95137ALIGNNATURALSIZEFLAG3ALTLayout3-1 | uiswing/QandE/Layout3-2 224181ALIGNNATURALSIZEFLAG3ALTLayout3-2 |
a. FlowLayout
b. BoxLayout
c. GridLayout
d. BorderLayout
Answer 3: b. BoxLayout lays out
components in either a column or a row. You can specify extra space using
an invisible component.
Question 4. The container can display three completely different components at different times, depending perhaps on user input or program state. Even if the components&146; sizes differ, switching from one component to the next shouldn&146;t change the amount of space devoted to the component.
uiswing/QandE/Layout4-1 357131ALIGNNATURALSIZEFLAG3ALTLayout4-1uiswing/QandE/Layout4-2 356131ALIGNNATURALSIZEFLAG3ALTLayout4-2
a. SpringLayout
b. BoxLayout
c. CardLayout
d. GridBagLayout
Answer 4: c. CardLayout exists to
allow components to share the same space. Although it's simpler to use a JTabbedPane
component to control an area, CardLayout is the solution when you
don't want tabs.
Exercise 1. Implement the layout
described and shown in question 1.
Answer 1: See Layout1.javaexample-1dot4/Layout1.javasourceIcon (in a .java source file)
Here's the code that implements the layout:
Exercise 2. Implement the layout
described and shown in question 2.
Answer 2: See Layout2.javaexample-1dot4/Layout2.javasourceIcon (in a .java source file)
Here's the code that implements the layout:
Exercise 3. Implement the layout
described and shown in question 3.
Answer 3: See Layout3.javaexample-1dot4/Layout3.javasourceIcon (in a .java source file)
Here's the code that implements the layout:
Exercise 4. Implement the layout
described and shown in question 4.
Answer 4: See Layout4.javaexample-1dot4/Layout4.javasourceIcon (in a .java source file)
Here's the code that implements the layout:
Exercise 5. By adding a single line of code, make the program you wrote for Exercise 2 display the components from right-to-left, instead of from left-to-right.
uiswing/QandE/Layout2-3 35059ALIGNNATURALSIZEFLAG3ALTLayout2-3Answer 5: You can change the horizontal orientation
using the http://java.sun/j2se/1.4.2/docs/api/java/awt/ComponentsetComponentOrientation(java.awtponentOrientation)setComponentOrientation
method defined by the Component class. For example:
p.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
Bhopal news
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100