JTagua

Inicio » Swing » Objetos » JPanel

Archivo de la categoría: JPanel

JPanel

Para realizar un estudio riguroso y detallado del Componente JPanel, le recomiendo que visite la dirección de Sun Microsystem siguiente

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JPanel.html

y además consultar el tutorial http://java.sun.com/docs/books/tutorial/uiswing/TOC.html

Veremos aquí una descripción simple de dicho componente analizando los constructores, las propiedades y los métodos mas utilizados.

Descripción:

JPanel es un contenedor de propósito general para componentes o controles de peso ligero. Como todos los contenedores, utiliza un Controlador de Distribución para posicionar y dimensionar sus componentes. Como todos los componentes Swing, JPanel permite añadirle bordes y determinar si utiliza el doble buffer para aumentar el rendimiento.

(Descargar el programa ejemplo desarrollado en Netbeans 6.0.1 aquí).

En la figura anterior pueden verse dos paneles etiquetados como «Sistema Métrico Decimal» y «Sistema Métrico U.S.», ambos paneles contendrán otros controles que permitirán convertir datos de un sistema métrico a otro.

Constructores:
JPanel(), JPanel(boolean), JPanel(LayoutManager), JPanel(LayoutManager, boolean)
Los métodos anterior permiten crear un panel. El parámetro boolean determina la estrategía de buffer del panel, de modo que si tiene un valor true indica doble buffer. El parámetro LayoutManager proporciona el controlador de distribución para el nuevo panel. Si no se especifica, el panel utiliza FlowLayout para distribuir sus componentes.

El primer panel del ejemplo anterior ha sido creado mediante la instrucción

jPanel1 = new javax.swing.JPanel();

Propiedades:

background: Se utiliza para establecer el color del fondo del Panel.

border: Establece el tipo de borde del panel

componentPopupMenu: Se utiliza para asignar un menu Popup al Panel.

foreground: Se utiliza para establecer el color del texto del Panel.

toolTipText: Se utiliza para fijar el texto de la caja de sugerencias del Panel.

Otras Propiedades:

Métodos:

void add(Component) , void add(Component, int) , void add(Component, Object)
void add(Component, Object, int) , void add(String, Component)

Adds the specified component to the panel. When present, the int parameter is the index of the component within the container. By default, the first component added is at index 0, the second is at index 1, and so on. The Object parameter is layout manager dependent and typically provides information to the layout manager regarding positioning and other layout constraints for the added component. The String parameter is similar to the Object parameter.

int getComponentCount()

Gets the number of components in this panel.

Component getComponent(int) , Component getComponentAt(int, int)
Component getComponentAt(Point) , Component[] getComponents()

Gets the specified component or components. You can get a component based on its index or x, y position.

void remove(Component) , void remove(int) , void removeAll()

Removes the specified component(s).

void setLayout(LayoutManager), LayoutManager getLayout()

Sets or gets the layout manager for this panel. The layout manager is responsible for positioning the panel’s components within the panel’s bounds according to some philosophy.