Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Creating Nested Panels in Java using BlueJ IDE, Lab Reports of Computer Science

A step-by-step guide on creating a graphical user interface (gui) using frames and panels in java with the bluej ide. Students will learn how to write and test the application by creating a new project, setting up subpanels with different colors and labels, and adding them to the primary panel. The document also includes instructions on running and experimenting with the program.

Typology: Lab Reports

Pre 2010

Uploaded on 08/04/2009

koofers-user-efx
koofers-user-efx 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
LAB 40 – GUI (Frames and Panels)
Lab Exercises
Topics
To introduce components and containers used in graphical user interfaces.
To write a GUI using frames and panels
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);
pf2

Partial preview of the text

Download Creating Nested Panels in Java using BlueJ IDE and more Lab Reports Computer Science in PDF only on Docsity!

LAB 40 – GUI (Frames and Panels)

Lab Exercises

Topics

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

 To write a GUI using frames and panels

 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); } }

  1. 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.
  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 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.