Thursday, April 19, 2012

Specify JPanel size inside a JPanel which size is also being specified

Design Free Website with Free Internet Marketing Tools

Program Description:

This is another JPanel Demonstration which gives you the idea on how to specify a JPanel size inside another JPanel which size is also being specified. This is very useful if you are handling multiple panels with different sizes and again, layout plays a bigger role in manipulating JPanel size which mean you must know what layout to which panel. Knowing the boundery of your layout can help you create a very nice GUI Design. So I hope this topic can assist you.

Output:
Code:

/**
 * File: jpanelSize2.java
 * Tiltle: Specify JPanel size inside a JPanel which size is also being specified.
 * Author: http://java-program-sample.blogspot.com/
 */

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;

public class jpanelSize2 extends JFrame {
 
 //Constructing JPanel Components
 JPanel outer = new JPanel();
 JPanel inner = new JPanel();

 //Setting up GUI
    public jpanelSize2() {
     
     //Setting up the Title of the Window
     super("Specify JPanel size inside a JPanel");

     //Set Size of the Window (WIDTH, HEIGHT)
     setSize(300,300);

     //Exit Property of the Window
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     

     //Setting up the container ready for the components to be added.
     Container pane = getContentPane();
     setContentPane(pane);

     //Setting up the container layout
     FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
     pane.setLayout(flow);
     
     //Setting up the property of our inner panel
     inner.setBorder(BorderFactory.createTitledBorder("Inner Panel (220,210) size"));
     inner.setBackground(Color.yellow);
     inner.setPreferredSize(new Dimension(220,210));
     
     //Setting up the property of our outer panel
     outer.setBorder(BorderFactory.createTitledBorder("Outer Panel (270,255) size"));
     outer.setBackground(Color.cyan);
     outer.setPreferredSize(new Dimension(270,255));
     
     //Adding our inner panel inside outer panel
     outer.add(inner);
  
  //Adding outer panel to the container
  pane.add(outer);
  
     /**Set all the Components Visible.
      * If it is set to "false", the components in the container will not be visible.
      */
     setVisible(true);
    }
    
 //Main Method
    public static void main (String[] args) {
     jpanelSize2 js2 = new jpanelSize2();
 }
}

Important Part of the Program:

//Setting up the property of our inner panel
     inner.setBorder(BorderFactory.createTitledBorder("Inner Panel (220,210) size"));
     inner.setBackground(Color.yellow);
     inner.setPreferredSize(new Dimension(220,210));
     
     //Setting up the property of our outer panel
     outer.setBorder(BorderFactory.createTitledBorder("Outer Panel (270,255) size"));
     outer.setBackground(Color.cyan);
     outer.setPreferredSize(new Dimension(270,255));
     
     //Adding our inner panel inside outer panel
     outer.add(inner);

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hostgator Discount Code