Question
The program NestedPanels.java is from Listing 3.8 of the text. Save the program to your directory and do the following: 1. Compile and run the
The program NestedPanels.java is from Listing 3.8 of the text. Save the program to your directory and do the following:
1. Compile and run the program. Experiment with resizing the frame and observe the effect on the components.
2. Modify the program by adding a third subpanel that is twice as wide, but the same height, as the other two subpanels. Choose your own label and color for the subpanel (the color should not be red, green, or blue). Add the panel to the primary panel after the other two panels.
3. Compile and run the modified program. Again, experiment with resizing the frame and observe the effect on the components.
4. Now add a statement to the program to set the preferred size of the primary panel to 320 by 260. (What would be the purpose of this?). Compile and run the program to see if anything changed.
5. Now add another panel with background color blue and size 320 by 20. Add a "My Panels" label to this panel and then add this panel to the primary
import java.awt.*; import javax.swing.*; public class NestedPanels {
public static void main (String[] args) { JFrame frame = new JFrame ("Nested Panels"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); JPanel subPanel1 = new JPanel(); subPanel1.setPreferredSize (new Dimension(150, 100)); subPanel1.setBackground (Color.green); JLabel label1 = new JLabel ("One"); subPanel1.add (label1); JPanel subPanel2 = new JPanel(); subPanel2.setPreferredSize (new Dimension(150, 100)); subPanel2.setBackground (Color.red); JLabel label2 = new JLabel ("Two"); subPanel2.add (label2); JPanel primary = new JPanel(); primary.setBackground (Color.blue); primary.add (subPanel1); primary.add (subPanel2); frame.getContentPane().add(primary); frame. pack () ; frame.setVisible(true); } }
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