LAB 40 – GUI (Frames and Panels)

Lab Exercises

Topics

n      To introduce components and containers used in graphical user interfaces.

n      To write a GUI using frames and panels

n      To use the BlueJ IDE to write and test the application

 

Using BlueJ to start a new application

1)       Starting BlueJ - double-click icon.

2)       Creating a new project - select New Project from the Project menu and save the project as GUI.

3)       Click on New Class and type NestedPanels as the class name and select Class as the class type

4)       Right-click on the NestedPanels class and select Open Editor

5)       Review the default code generated by BlueJ.

6)       Replace the code generated by BlueJ with the following; update the comments.

//********************************************************************

//  NestedPanels.java       Author: Lewis/Loftus

//

//  Demonstrates a basic component hierarchy.

//********************************************************************

import java.awt.*;

import javax.swing.*;

 

public class NestedPanels

{

   //-----------------------------------------------------------------

   //  Presents two colored panels nested within a third.

   //-----------------------------------------------------------------

   public static void main (String[] args)

   {

      JFrame frame = new JFrame ("Nested Panels");

      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

 

      // Set up first subpanel

      JPanel subPanel1 = new JPanel();

      subPanel1.setPreferredSize (new Dimension(150, 100));

      subPanel1.setBackground (Color.green);

      JLabel label1 = new JLabel ("One");

      subPanel1.add (label1);

 

      // Set up second subpanel

      JPanel subPanel2 = new JPanel();

      subPanel2.setPreferredSize (new Dimension(150, 100));

      subPanel2.setBackground (Color.red);

      JLabel label2 = new JLabel ("Two");

      subPanel2.add (label2);

 

      // Set up primary panel

      JPanel primary = new JPanel();

      primary.setBackground (Color.blue);

      primary.add (subPanel1);

      primary.add (subPanel2);

 

      frame.getContentPane().add(primary);

      frame.pack();

      frame.setVisible(true);

   }

}

7)       To run the program, right-click on the NestedPanels class and select and select void main (String[] arguments)

a)       This should pop-up a terminal window with results

b)       Experiment with resizing the frame and observe the effect on the components.

8)       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.

9)       Compile and run the modified program. Again, experiment with resizing the frame and observe the effect on the components.

10)   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 panel before adding the other panels. Compile and run the program. What was the effect of this panel?

 

 

QUESTIONS: Submit NestedPanels.java to the DropBox in WebCT.