Sei sulla pagina 1di 6

Las ventanas de la libreria "javax.swing" se engloban en la clase JFrame.

Lo nico que tenemos que hacer es extender la clase principal de nuestro programa con JFrame quedando asi: import javax.swing.JFrame; public class MiPrograma extends JFrame {} Ya que tenemos el JFrame en nuestra clase principal nos falta poner el constructor por default: import javax.swing.JFrame; public class MiPrograma extends Jframe { public MiPrograma() { super(); } } Ahora solo falta ponerle lo que tu quieras, un ejemplo de esto es el titulo de tu ventana: import javax.swing.JFrame; public class MiPrograma extends JFrame {} Ya que tenemos el JFrame en nuestra clase principal nos falta poner el constructor por default: import javax.swing.JFrame; public class MiPrograma extends Jframe { public MiPrograma() { super(); } public MiPrograma (String title) { } } Pero recuerda que sin un "main" no puedes ejecutar tu programa

, asi es que nos falta agregar el main y agregar tambien la instancia para mandar llamar a nuestra ventana: import javax.swing.JFrame; public class MiPrograma extends Jframe { public MiPrograma() { super(); } public MiPrograma (String title) { } public static void main (java.lang.String args[]) { JFrame MiVentana = new JFrame("MI PRIMER VENTANA" ); //llamamos a la clase y creamos un objeto llamado MiVentana MiVentana.setVisible(true);//le decimos al compilador que queremos que se vea la ventana MiVentana.setSize(200,200);//le damos el tamao deseado a nuestra ventana MiVentana.setDefaultCloseOperation(EXIT_ON_CLOSE);//le decimos que al dar clic en la X se cierre nuestra ventana } }

UNIDAD 2: JAVA APLICACIONES GRAFICAS JAVA TEMA 2: JAVA FRAME PROPIEDADES JAVA Frame es el componente, control u objeto principal de una aplicacion visual o grafica en java. Es el equivalente a FORM1 en los demas lenguajes visuales, como todos ellos Frame es un objeto normal que se deriva de una clase apropiada, tiene sus propiedades y se le pueden asociar sus eventos. Todo Programa Visual en java, empieza creando un objeto de este tipo, cargando y asignando sus propiedades como lo muestra el siguiente ejemplo: Ejemplo 1)

import java.lang.*; import java.awt.*; import java.awt.event.*;

public class prog1 {

//declaracion, creacion e inicializacion de componentes, objetos y variables

// en este caso un solo objeto la ventana

static Frame ventana= new Frame();

// parte principal de programa

public static void main(String[] args)

// area de definicion de propiedades de el objeto

ventana.setTitle("mi programa");

ventana.setBackground(java.awt.Color.pink);

ventana.setSize(200,250);

ventana.setVisible(true);

// se agrega un escuchador para que cierre la ventana con la x de arriba

ventana.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {System.exit(0);} });

}; // termina main

} // termina clase

CORRIDA:

Como ya se indico, todo programa en java, debe ser un clase y toda aplicacin o programa debe tener un metodo main. El programa empieza creando un objeto llamado ventana, derivado de la clase FRAME, observar y aprender el formato de creacion, es formato similar para cuando se crean mas objetos. Es el objeto ventana quien debera cargarse con los demas componentes y o controles que tendra la aplicacin. En este programa ejemplo, se muestra como asociar eventos a el objeto ventana, primero se quiere aclarar algunas cuestiones. Las propiedades o metodos derivados de la clase superior component de FRAME son: action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, addPropertyChangeListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getGraphics, getHeight, getInputMethodRequests, getLocation, getLocation, getLocationOnScreen, getName, getParent, getPeer, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isDisplayable, isDoubleBuffered, isEnabled, isFocusTraversable, isLightweight, isOpaque, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, reshape, resize, resize, setBackground, setBounds,

setBounds, setComponentOrientation, setDropTarget, setEnabled, setForeground, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, size, toString, transferFocus Como se observa, en el programa ejemplo solo se usan muy poquitas de ellas. Pero an hay mas, FRAME tambien puede usar las siguientes propiedades y metodos de la clase superior container y son: add, add, add, add, add, addContainerListener, addImpl, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getInsets, getLayout, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paint, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, remove, removeAll, removeContainerListener, setFont, setLayout, update, validate, validateTree De aqui tambien se tomarn algunas para el programa ejemplo. Pero sigue la mata dando, FRAME tambien puede usar metodos y propiedades de la clase superior windows: addWindowListener, applyResourceBundle, applyResourceBundle, dispose, getFocusOwner, getGraphicsConfiguration, getInputContext, getListeners, getLocale, getOwnedWindows, getOwner, getToolkit, getWarningString, hide, isShowing, pack, postEvent, processEvent, processWindowEvent, removeWindowListener, setCursor, show, toBack, toFront Observar que tambien de aqui se tomarn varias para el programa ejemplo. Pero!!! FRAME tambien tiene sus propias propiedades y metodos que son: Method Summary void protected void addNotify() Makes this Frame displayable by connecting it to a native screen resource. finalize() We have to remove the (hard) reference to weakThis in the Vector, otherwise the WeakReference instance will never get garbage collected. getAccessibleContext() Gets the AccessibleContext associated with this Frame. getCursorType() Deprecated. As of JDK version 1.1, replaced by Component.getCursor(). getFrames() Returns an array containing all Frames created by the application. getIconImage() Gets the image to be displayed in the minimized icon for this frame. getMenuBar() Gets the menu bar for this frame.

AccessibleContext int static Frame[] Image MenuBar

int String boolean protected String void void void void void void void void

getState() Gets the state of this frame. getTitle() Gets the title of the frame. isResizable() Indicates whether this frame is resizable by the user. paramString() Returns the parameter String of this Frame. remove(MenuComponent m) Removes the specified menu bar from this frame. removeNotify() Makes this Frame undisplayable by removing its connection to its native screen resource. setCursor(int cursorType) Deprecated. As of JDK version 1.1, replaced by Component.setCursor(Cursor). setIconImage(Image image) Sets the image to displayed in the minimized icon for this frame. setMenuBar(MenuBar mb) Sets the menu bar for this frame to the specified menu bar. setResizable(boolean resizable) Sets whether this frame is resizable by the user. setState(int state) Sets the state of this frame. setTitle(String title) Sets the title for this frame to the specified string.

Como notas finales: 1.- tambien de aqui se tomarn algunas para el programa ejemplo. 2.- Todo este material acerca de metodos y propiedades, lo encuentran buscando la clase correspondiente ( en este caso FRAME) en doc y api que estan en el sitio de java.sun.com/>www.java.sun.com 3.-Es necesario que localizen este material, para que lo tengan a la mano, pueden bajar el api completo del sitio: sun.com/j2se/1.3/docs.http://java.sun.com/j2se/1.3/docs.html 4.- Es la ultima vez que se muestran todas la propiedades y metodos de un componente, de aqui en adelante deberan tener la documentacin del componente a la mano (inciso anterior), para poder usar un componente a plenitud en sus programas o aplicaciones

Potrebbero piacerti anche