Question
Learning Activity: Layout Managers Objectives Gain familiarity with layout managers. Instructions All of these exercises for the lab are based on the Layout.java class. The
Learning Activity: Layout Managers
Objectives
Gain familiarity with layout managers.
Instructions
All of these exercises for the lab are based on the Layout.java class. The lab exercise is broken into a series of different portions or "cases". Each case consists as a set of method calls for a Layout instance; they are given as an application method. You can either add an application method to the Layout class, or make these method calls interactively within your chosen IDE. I have also provided a class which runs each case depending on the command-line argument. In each case, you need to answer the questions posed for that case. Note: the cases are independent. You will submit the answers for the questions in a text file. We will discuss the "Challenge Questions" in class next week.
Case 1
Consider the following application method for Layout.
public static void main(String[] args) { Layout theApp = new Layout(); }
Answer the following questions:
How many JButton instances exist in the application?
How many JButton instances are shown?
What happens as the user resizes the window?
Case 2
Consider the following application method for Layout.
public static void main(String[] args) { Layout theApp = new Layout(); theApp.useFlowLayout(); }
Answer the following questions:
What does the window look like? (Refer to images page at the bottom.)
Are the buttons all the same size?
What determines the size of the buttons?
What happens as the user resizes the window, making it considerably narrower? Wider?
Case 3
Consider the following application method for Layout.
public static void main(String[] args) { Layout theApp = new Layout(); theApp.useFlowLayout(); theApp.pack(); }
Answer the following questions:
What does the window look like? (Refer to images page at the bottom.)
What does the pack method do?
Case 4
Consider the following application method for Layout.
public static void main(String[] args) { Layout theApp = new Layout(); theApp.useGridLayout(); }
Answer the following questions:
What does the window look like? (Refer to images page at the bottom.)
What determines the size of the buttons?
What happens as the user resizes the window?
Case 5
Consider the following application method for Layout.
public static void main(String[] args) { Layout theApp = new Layout(); theApp.useBorderLayout(); }
Answer the following questions:
What does the window look like? (Refer to images page at the bottom.)
What happens as the user resizes the window?
What determines the size of the buttons?
Hint: What happens if you add the call theApp.pack() to the end of main?
Case 6
Consider the following application method for Layout.
public static void main(String[] args) { Layout theApp = new Layout(); theApp.useToolbar(); }
Answer the following questions:
What does the window look like? (Refer to images page at the bottom.)
When the window first appears, how many are visible? Explain.
Hint: What happens if the user resizes the window?
Case 7
Consider the following application method for Layout.
public static void main(String[] args) { Layout theApp = new Layout(); theApp.useToolbarII(); }
Answer the following questions:
What does the window look like? (Refer to images page at the bottom.)
Why is "Ernie ..." now visible?
Hint: Read the documentation for setPreferredSize. This method is inherited from JComponent.
Case 8
Consider the following application method for Layout.
public static void main(String[] args) { Layout theApp = new Layout(); theApp.useCustomLayout(); }
Answer the following questions:
What does the window look like? (Refer to images page at the bottom.)
What happens as the user resizes the window?
What happens when c.validate() is called at the end of the useCustomLayout method?
Case 9
Consider the following application method for Layout.
public static void main(String[] args) { Layout theApp = new Layout(); theApp.useBoxLayout(); }
Answer the following questions:
What does the window look like? (Refer to images page at the bottom.)
What happens as the user resizes the window?
Briefly describe the difference between BoxLayout (case 9) and FlowLayout (case 6) for the toolbar.
Hint: The javax.swing.Box class provides an easy way to use javax.swing.BoxLayout. For more information, see the API documentation for these two classes.
"Can you figure it out?" Questions
These cases are for discussion. Understanding the "odd" behavior presented by these cases will help develop a good understanding of the way layout managers work. So, these cases are not part of the learning activity, per se.
Case 10
Consider the following alternate application methods for Layout.
public static void main(String[] args) { Layout theApp = new Layout(); theApp.useToolbarII(); theApp.useGridLayout(); }
public static void main(String[] args) { Layout theApp = new Layout(); theApp.useToolbarII(); theApp.useFlowLayout(); }
Answer the following questions:
What do the windows look like?
The arrangement of buttons is different due to the call to useToolbarII. Explain.
Hint: What happens if a call to resetToolbar is added to either of these application methods?
Case 11
Consider the following application method for Layout.
public static void main(String[] args) { Layout theApp = new Layout(); theApp.useGridLayout(); theApp.useBorderLayout(); }
Answer the following questions:
What does the window look like?
What happens as the user resizes the window?
Briefly explain the behavior of "Charlie".
Case 12
Consider the following application method for Layout.
public static void main(String[] args) { Layout theApp = new Layout(); theApp.useBorderLayout(); theApp.showAButton(); }
Answer the following questions:
What buttons appear?
What happens as the user moves the mouse over the window?
What happens if the user resizes the window?
Briefly explain this behavior.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started