viernes, 18 de enero de 2008

JButton

Para realizar un estudio riguroso y detallado del Componente JButton, lo mejor sería visitar la dirección de Sun Microsystem siguiente

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JButton.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 y las propiedades y métodos mas utilizados.


Descripción:

El componente JButton permite al usuario ejecutar una accción o un procedimiento.




Como se muestra en la imagen anterior, un botón Swing puede mostrar tanto texto como una imagen, e incluso ambos. En este último caso el texto puede situarse en cualquier lugar respecto de la imagen. La letra subrayada de cada texto de botón muestra el mnemónico -- la tecla alternativa -- para cada botón.
(Descargar el programa ejemplo desarrollado en Netbeans 6.0.1 aqui).

Cuando un botón se desactiva, cambia su aspecto. Sin embargo, podríamos proporcionar una imagen para que substituya la imagen normal y de esta manera el botón tendrá una apariencia cuando está activado y otra cuando no lo está.

Constructores:

JButton(String, Icon), JButton(String), JButton(Icon), JButton()

Cada uno de los métodos anteriores crea un botón para tener el texto/imagen especificado.
El botón izquierdo del ejemplo anterior ha sido creado mediante la instrucción


bIzq = new javax.swing.JButton();


Propiedades:



action : Se utiliza para fijar un procedimiento común a varios componentes.
background: Se utiliza para establecer el color del fondo del botón.
componetPopupMenu: Se utiliza para asignar un menu Popup al botón.
font: Se utiliza para fijar el font del texto del botón.
foreground: Se utiliza para establecer el color del texto del botón.
mnemonic: Se utiliza para fijar una tecla rápida (Alt + Letra) o atajo de teclado del botón.
text: Se utiliza para establecer el texto del botón.
toolTipText: Se utiliza para fijar el texto de la caja de sugerencias del botón.

Otras Propiedades (1/2):



Otras Propiedades (2/2):


Set or get the button's properties according to values from the Action instance.
Set or get the text displayed by the button.
Set or get the image displayed by the button when the button isn't selected or pressed.
Set or get the image displayed by the button when it is disabled. If you do not specify a disabled image, then the look and feel creates one by manipulating the default image.
Set or get the image displayed by the button when it is being pressed.
Set or get the image displayed by the button when it is selected. If you do not specify a disabled selected image, then the look and feel creates one by manipulating the selected image.

Use setRolloverIcon(someIcon) to make the button display the specified icon when the cursor passes over it. The setRolloverSelectedIcon method lets you specify the rollover icon when the button is selected — this is useful for two-state buttons such as toggle buttons. Setting the rollover icon automatically calls setRollover(true), enabling rollover.

Set or get where in the button its contents should be placed. The AbstractButton class allows any one of the following values for horizontal alignment: RIGHT, LEFT, CENTER (the default), LEADING, and TRAILING. For vertical alignment: TOP, CENTER (the default), and BOTTOM.
Set or get where the button's text should be placed, relative to the button's image. The AbstractButton class allows any one of the following values for horizontal position: LEFT, CENTER, RIGHT, LEADING, and TRAILING (the default). For vertical position: TOP, CENTER (the default), and BOTTOM.
Set or get the number of pixels between the button's border and its contents.
Set or get whether the button should look different when it has the focus.
Set or get whether the border of the button should be painted.
Set or get the amount of space between the text and the icon displayed in this button.
Set or get the keyboard alternative to clicking the button. One form of the setMnemonic method accepts a character argument; however, the Swing team recommends that you use an int argument instead, specifying a KeyEvent.VK_X constant.
Set or get a hint as to which character in the text should be decorated to represent the mnemonic. Note that not all look and feels may support this.
Set or get the name of the action performed by the button.
Add or remove an object that listens for action events fired by the button.
Add or remove an object that listens for item events fired by the button.
Set or get whether the button is selected. Makes sense only for buttons that have on/off state, such as check boxes.
Set or get the keyboard alternative to clicking the button. One form of the setMnemonic method accepts a character argument; however, the Swing team recommends that you use an int argument instead, specifying a KeyEvent.VK_X constant.
Set or get a hint as to which character in the text should be decorated to represent the mnemonic. Note that not all look and feels may support this.
Set or get the name of the action performed by the button.
Add or remove an object that listens for action events fired by the button.
Add or remove an object that listens for item events fired by the button.
Set or get whether the button is selected. Makes sense only for buttons that have on/off state, such as check boxes.
Programmatically perform a "click". The optional argument specifies the amount of time (in milliseconds) that the button should look pressed.
Set or get the amount of time (in milliseconds) required between mouse press events for the button to generate corresponding action events.

No hay comentarios: