You are on page 1of 9
Java Avanzado, Guia 9 acultad: Ingenier: scuela: Computacién Asignatura: Java Avanzado Tema: Fundamentos de Swing (Parte II) Contenido En esta guia se introducird al desarrollo de aplicaciones en Java basadas en Swing. Con Swing se pueden desarrollar aplicacién Java de Escritorio con entorno Grafico (GUI). (Ob jetive Especifico a) Aprender a utilizar y codificar en Java componentes de la interfaz gréfica tales como cajas de texto, combo box, text areas, botones, entre otros. b) Aprender a agregar componentes de la interfaz gréfica GUI en contenidores de medio nivel y nivel alto c) Aprender a gestionar eventos para los componentes de interfaz grafica de usuario GUI. Material y Equipo a) Netbeans b) JDK Java Introduccion Teorica Swing es una biblioteca grafica para Java que incluye widgets para interfaz gréfica de usuario tales como cajas de texto, botones, desplegables y tablas. Los Componentes Swing proveen un entorno de GUI para aplicaciones Java y Applet, ademés los componentes Swing son una coleccién de componentes visuales como reemplazo de componentes AWT (Abstract Window Toolkit ). java Avanzado, Guia 9 Procedimiento la clase Conver es una clase definida por el usuario orientada a realizar conversion de temperaturas de grados centigrados a grados fahrenheit, la clase Conver hace uso de gestién de contenedores swing de alto y medio nivel, utiliza objetos atémicos swing y ademas contiene cédigo de gestion de eventos swing. Digitar e1 cédigo de la clase Conver siguiente y ejecutarlo como muestran las figuras 1 y 2: Conver. java public class Conver extends javax.swing.dFrame ‘ public Conver () ‘ setsize(300, 200); setTitle("Conversién de temperaturas"); initComponents (}; private void initComponents() { jlbGradosC = new javax.swing.JLabel(); jtfGradosc = new javax.swing.JTextPield(); jlbGradosF = new javax.swing.JLabel(); jtfGradosF = new javax.swing.JTextfield(); jbtaceptar - new javax.swing.JButton(); addWindowListener (new java.awt.event .Windowadapter() { public void windowClosing(java.aut event .Windowzvent evt) { exitForm(evt); public void windowOpened (java.awt.event .WindowEvent evt) ( formiindowopened (evt) ; Me getContentPane () . setLayout (null) ; jlbGradosC.setText ("Centigrados"); getContentPane () .add(j1bGradosc) + jlbGradosC.setBounds (12, 28, 116, 14); jt£¢radosC.setHorizontalAlignment (javax.swing.JTextField. RIGHT); jtfGradosC.setText ("0.00"); jtfGradosC.addFocusListener (new java.awt .event .FocusAdapter () { Java Avanzado, Guia 9 public void focusGained(java.awt.event .FocusEvent evt) { jt £CradosFocusGained (evt); ’ De jt fGradosC.addkeyListener (new java.awt.event.KeyAdapter() { public void keyTyped(java-awt.event.KeyEvent evt) ( jt £GradosKeyTyped(evt) ; ’ Me getContentPane() .add (jt £Gradosc) ; jt fGradosC.setBounds (132, 28, 144, 20); jlbGradosF.setText ("Fahrenheit"); getContentPane () .add(j1bGradosF) ; jlbGradosF.setBounds (12, 68, 104, 24); jt £GradosF .setHorizontalAlignment (javax.swing.JTextField.RIGHT) ; jtfGradosF.setText ("32.00"); jtfGradosF.addFocusListener (new java.awt .event .FocusAdapter() { public void focusGained(java.awt.event .FocusEvent evt) { jt £GradosFocusGained(evt); ’ MG jtfGradosF.addkeyListener (new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.Keyévent evt) ( jt £GradosKeyTyped(evt) ; ’ Me getContentPane() .add(jtfGradosF) ; jtfGradosF.setBounds (132, 72, 144, 20); btAceptar.setMnemonic('A'); jbtAceptar.setText ("Convertir"); getRootPane() .setDefaultButton (jbtAceptar) ; jbtAceptar.addActionListener (new java.awt.event ActionListener () { public void actionPerformed(java.awt.event-ActionEvent evt) { jbtAceptarActionPerformed (evt) ; ) De getContentPane () .add(jbtAceptar) + jbtAceptar.setBounds (132, 120, 144, 24); private void formindowOpened (java.awt.event WindowEvent evt) { jt fGradosC.requestFocus () i java Avanzado, Guia 9 private void exitForm(java.awt.event .WindowEvent evt) { System.exit (0); private void jbtAceptarActionPerformed (java.awt .event .ActionEvent jevty try 4 double grados; if(objJTextField == jt£Gradosc) ( grados = Double.parseDouble (jt fGradosC.getText ()) * 9.0 / 5.0 + 32.0; String texto = String.format("%.2£", grados) ; jtfGradosF.setText (texto) ; b if (objJTextPield == jtfGradosF) ( grados =(Double.parseDouble (jtfGradosF .getText ()) - 32.0) * 5.0 / 9.0; String texto = String.format ("%.2£", grados); jt#GradosC. set Text (texto) + Ik ) cateh (NumberFormatexception e) [ it £GradosC. set Text ("0.00"); jtfGradosF.set Text ("32.00"); private void jt£GradosKeyTyped(java.awt.event .KeyEvent evt) { obidTextField = evt.getSource(); private void jt£GradosFocusGained (java.awt .event.FocusEvent evt) { javax.swing.JTextField objEnfocado = (Javax. swing. JTextField) evt .getSource(); objEnfocado.selectAll(); public static void main(Stringl] args) 4 try ‘ javax. swing. UIManager. set LookAndFeel ( javax. swing. UIManager.getSystemLookAndFee1ClassName()) ; catch (Exception e) ‘ System.out-printin("No se pudo establecer el aspecto deseado: " ted: Java Avanzado, Guia 9 new Conver ().setVisible (true); private javax.swing.JButton jbtAceptar; private javax.swing.JLabel 31bGradosC; private javax.swing.JLabel 31bGrados®; private javax.swing.JTextField jt£Gradosc; private javax.swing.JTextfield jt£Grados®; private Object objJTextField; jalter@walter-desktop:~/waltersanchez$ javac Conver. java Ja ltergualter-desktop:~/waltersanchez$ Java Conver Figura 1 Ge eee Centicrades Fahrenheit 32.00 Figura 2 java Avanzado, Guia 9 la clase Reloj es una clase definida por el usuario orientada a implementar ¢1 funcionamiento de un reloj digital, la clase Reloj hace uso de gestién de contenedores swing de alto y medio nivel, utiliza objetos atémicos swing y adem4s contiene cédigo de gestién de eventos swing. Digitar el cédigo de la clase Reloj siguiente y ejecutarlo como muestran las figuras 3 y 4: Reloj. java public class Reloj extends javax.swing.JFrame { public Reloj() { initComponents (); setSize(290, 260); setTitle("Reloj Electronico"); initothersComponents (); private void initcomponents() { jlbHoraActual = new javax.swing.JLabel (1; getContentPane () .setLayout (null); setResizable (false); addWindowListener (new java.awt .event .WindowAdapter () t public void windowClosing (java.awt.event .WindowEvent evt) { exitForm(evt); } a jibHoraactual.setFont (new java.awt.Font ("Arial", 1, 24))i jibHoraActual.setHorizontalAlignment (javax.swing.SwingConstant 8.CENTER) j jlbHoraActual.setText ("00:00:00"); getContentPane () .add(jlbHoraActual) ; jlbHoraActual.setBounds (40, 90, 210, 40); pack (7 Java Avanzado, Guia 9 private void exitForm(java.awt.event.WindowEvent evt) { System.exit (0); private void initothersComponents () { java.awt event ActionListener al = new java.awt.event .ActionListener () { public void actionPerformed(java.awt .event .ActionEvent evt) { onTimer () timerl = new javax.swing.Timer (1000, al}; timerl.start (); private void onTimer() { java.util.Date hora = new java.util.Date(); String patron = "hh:mm:ss"; java.text.SimplepateFormat formato java.text .SimpleDateFormat (patron) ; jlbHoraactual.set Text (formato. format (hora) )j new public static void main(String[] args) ( new Reloj().setVisible (true); private javax.swing.JLabel jlbHoraActual; private javax.swing.Timer timerl; Java Avanzado, Guia 9 ter-desktop: ~/waltersanchez jaltergualter-desktop:~/waltersanchezS javac Reloj. java jaltergualter-desktop:~/waltersanchez$ java Relo} walter @walter-desktop:—/waltersanchezS Figura 3 Reloj Electronico 11:58:46 Figura 4 Java Avanzado, Guia 9 Hoja de Guia 9: Introduccién a Swing (Parte II) ALumn Cc) Byazuacton : M4 = 10 wot APLICACION DEL | Del covocnuzenzo | 04 a acrem0o zorat | 100

You might also like