elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Tutorial básico de Quickjs


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Clase para interactuar con el usuario mediante interfaz gráfica simple
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Clase para interactuar con el usuario mediante interfaz gráfica simple  (Leído 3,748 veces)
JmpNull

Desconectado Desconectado

Mensajes: 13


Proyecto de Ingeniero Informático


Ver Perfil
Clase para interactuar con el usuario mediante interfaz gráfica simple
« en: 17 Agosto 2009, 22:38 pm »

Buenas!

Como últimamente me aburro bastante (hasta empezar otra vez la uni en 2o curso de Ingeniería Informática ;D), hoy he programado una clase estática que sirve para mostrar y recibir datos mediante simples mensajes por pantalla. Es muy simple, pero bastante útil a la hora de hacer programas simples o pedir datos en un cierto momento. Respetando el modelo MVC, es estática simplemente porque la interfaz de interacción con el usuario (en la mayoría de casos) tiene que ser independiente del programa para poder implementar el software en consola, web, etc. Espero que sirva de ayuda y os sea de interés!

Próximamente subiré algunos programas que he hecho durante estos casi 3 meses de vacaciones (lo que hace el aburrimiento  :xD).

Saludos a todos! :)

Nombre de la clase: SimpleGui.java

Código
  1. /*
  2.  * Copyright 2009 Juan Carlos <juancarloslinux@gmail.com>
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17.  * MA 02110-1301, USA.
  18.  */
  19.  
  20. import javax.swing.JOptionPane;
  21. import javax.swing.JTextArea;
  22.  
  23. /**
  24.  * Clase estatica para pedir datos mediante interfaz grafica simple.
  25.  *
  26.  * @author JmpNull
  27.  */
  28. public class SimpleGui {
  29.  
  30.    /**
  31.      * Este metodo se encarga de leer un objeto Integer.
  32.      *
  33.      * @param mensaje El mensaje que va a mostrar
  34.      * @return El valor de retorno
  35.      */
  36.    public static Integer getInteger(String mensaje) {
  37.        Integer i = null;
  38.        boolean valido = false;
  39.        while (!valido) {
  40.            try {
  41.                i = Integer.parseInt(JOptionPane.showInputDialog(mensaje));
  42.                valido = true;
  43.            } catch (NumberFormatException e) {
  44.                printError("La entrada no es valida");
  45.                i = null;
  46.            }
  47.        }
  48.        return i;
  49.    }
  50.  
  51.    /**
  52.      * Este metodo se encarga de leer un objeto Double.
  53.      *
  54.      * @param mensaje El mensaje que va a mostrar
  55.      * @return El valor de retorno
  56.      */
  57.    public static Double getDouble(String mensaje) {
  58.        Double i = null;
  59.        boolean valido = false;
  60.        while (!valido) {
  61.            try {
  62.                i = Double.parseDouble(JOptionPane.showInputDialog(mensaje));
  63.                valido = true;
  64.            } catch (NumberFormatException e) {
  65.                printError("La entrada no es valida");
  66.                i = null;
  67.            }
  68.        }
  69.        return i;
  70.    }
  71.  
  72.    /**
  73.      * Este metodo se encarga de leer un objeto Float.
  74.      *
  75.      * @param mensaje El mensaje que va a mostrar
  76.      * @return El valor de retorno
  77.      */
  78.    public static Float getFloat(String mensaje) {
  79.        Float i = null;
  80.        boolean valido = false;
  81.        while (!valido) {
  82.            try {
  83.                i = Float.parseFloat(JOptionPane.showInputDialog(mensaje));
  84.                valido = true;
  85.            } catch (NumberFormatException e) {
  86.                printError("La entrada no es valida");
  87.                i = null;
  88.            }
  89.        }
  90.        return i;
  91.    }
  92.  
  93.    /**
  94.      * Este metodo se encarga de leer un objeto Character.
  95.      *
  96.      * @param mensaje El mensaje que va a mostrar
  97.      * @return El valor de retorno
  98.      */
  99.    public static Character getCharacter(String mensaje) {
  100.        Character salida = null;
  101.        boolean valido = false;
  102.        while (!valido) {
  103.            String valor = JOptionPane.showInputDialog(mensaje);
  104.  
  105.            if (valor.length() == 0) {
  106.                salida = ' ';
  107.                valido = true;
  108.            } else if (valor.length() == 1) {
  109.                salida = valor.charAt(0);
  110.                valido = true;
  111.            } else {
  112.                printError("La entrada no es valida");
  113.            }
  114.        }
  115.        return salida;
  116.    }
  117.  
  118.    /**
  119.      * Este metodo se encarga de mostrar al usuario un mensaje de pregunta.
  120.      *
  121.      * @param entrada El mensaje de pregunta
  122.      * @return El resultado
  123.      */
  124.    public static Integer getQuestion(String entrada) {
  125.        return JOptionPane.showConfirmDialog(null, entrada);
  126.        // 0 para yes
  127.        // 1 para no
  128.        // ? para cancel o x
  129.    }
  130.  
  131.    /**
  132.      * Este metodo se encarga de leer un objeto String.
  133.      *
  134.      * @param mensaje El mensaje que va a mostrar
  135.      * @return El valor de retorno
  136.      */
  137.    public static String getString(String mensaje) {
  138.        return JOptionPane.showInputDialog(mensaje);
  139.    }
  140.  
  141.    /**
  142.      * Este metodo muestra por pantalla un mensaje de tipo texto plano.
  143.      *
  144.      * @param entrada El mensaje a mostrar
  145.      */
  146.    public static void printPlain(String entrada) {
  147.        JOptionPane.showMessageDialog(null, entrada, "Salida",
  148.                JOptionPane.PLAIN_MESSAGE);
  149.    }
  150.  
  151.    /**
  152.      * Este metodo muestra por pantalla un mensaje de tipo error.
  153.      *
  154.      * @param entrada El mensaje a mostrar
  155.      */
  156.    public static void printError(String entrada) {
  157.        JOptionPane.showMessageDialog(null, entrada, "Error",
  158.                JOptionPane.ERROR_MESSAGE);
  159.    }
  160.  
  161.    /**
  162.      * Este metodo muestra por pantalla un mensaje de tipo pregunta.
  163.      *
  164.      * @param entrada El mensaje a mostrar
  165.      */
  166.    public static void printQuestion(String entrada) {
  167.        JOptionPane.showMessageDialog(null, entrada, "Salida",
  168.                JOptionPane.QUESTION_MESSAGE);
  169.    }
  170.  
  171.    /**
  172.      * Este metodo muestra por pantalla un mensaje de tipo informativo.
  173.      *
  174.      * @param entrada El mensaje a mostrar
  175.      */
  176.    public static void printInformation(String entrada) {
  177.        JOptionPane.showMessageDialog(null, entrada, "Salida",
  178.                JOptionPane.INFORMATION_MESSAGE);
  179.    }
  180.  
  181.    /**
  182.      * Este metodo muestra por pantalla un mensaje de tipo aviso.
  183.      *
  184.      * @param entrada El mensaje a mostrar
  185.      */
  186.    public static void printWarning(String entrada) {
  187.        JOptionPane.showMessageDialog(null, entrada, "Salida",
  188.                JOptionPane.WARNING_MESSAGE);
  189.    }
  190.  
  191.    /**
  192.      * Este metodo muestra por pantalla un mensaje dentro de un objeto
  193.      * JTextArea.
  194.      *
  195.      * @param entrada El objeto JTextArea
  196.      */
  197.    public static void println(JTextArea entrada) {
  198.        JOptionPane.showMessageDialog(null, entrada);
  199.    }    
  200.  
  201.    /**
  202.      * Este metodo muestra por pantalla un mensaje de tipo estandar.
  203.      *
  204.      * @param entrada El mensaje a mostrar
  205.      */
  206.    public static void println(String entrada) {
  207.        JOptionPane.showMessageDialog(null, entrada);
  208.    }    
  209.  
  210.    // IDEM
  211.    public static void println(Integer entrada) {
  212.        JOptionPane.showMessageDialog(null, String.valueOf(entrada));
  213.    }
  214.  
  215.    public static void println(Double entrada) {
  216.        JOptionPane.showMessageDialog(null, String.valueOf(entrada));
  217.    }
  218.  
  219.    public static void println(Float entrada) {
  220.        JOptionPane.showMessageDialog(null, String.valueOf(entrada));
  221.    }
  222.  
  223.    public static void println(Character entrada) {
  224.        JOptionPane.showMessageDialog(null, String.valueOf(entrada));
  225.    }
  226. }
  227.  
  228.  



« Última modificación: 17 Agosto 2009, 22:41 pm por JmpNull » En línea

En el mundo hay 10 tipos de personas…Los que leen binario y los que no.
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines