lunes, 25 de junio de 2012

EJERCICIOS RESUELTOS CON INTERFAZ EN SWING JAVA PACK 02

EJERCICIOS PACK 02

Este PACK 02 Contiene ejercicios un poco mas avanzado que el PACK 01, los ejercicios interactuan un poco con el USUARIO, pero el PACK 03 interactuara por completo con el usuario.
 
NOTA : PRIMERO les muestro los RESULTADOS por que quiero que se vayan haciendo a la idea de lo que se hara en el COD. FUENTE.

EJERCICIO 01
  • RESULTADO
  • COMO FUNCIONA EL MENU
  • COD. FUENTE
/**
 *
 *  DUDAS SOBRE ESTE CODIGO, shamirdhc31@gmail.com
 */
import javax.swing.*;
public class EJERCICIO_01 extends JFrame
{
    public JMenu MENU_ARCHIVO,MENU_JAVA;
    public JMenuItem ITEM_ABRIR,ITEM_GUARDAR,ITEM_CERRAR,ITEM_JAVADHC;
    public JMenuBar MENU_BAR;

    public EJERCICIO_01()
    {
        super("MI MENU CON JFRAME...");
        this.setSize(600,400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.ITEM_ABRIR = new JMenuItem("ABRIR");
        this.ITEM_GUARDAR = new JMenuItem("GUARDAR");
        this.ITEM_CERRAR = new JMenuItem("CERRAR");
        this.ITEM_JAVADHC = new JMenuItem("JAVADHC");

        this.MENU_ARCHIVO = new JMenu("ARCHIVO");
        this.MENU_ARCHIVO.add(this.ITEM_ABRIR);
        this.MENU_ARCHIVO.add(this.ITEM_GUARDAR);
        this.MENU_ARCHIVO.addSeparator();
        this.MENU_ARCHIVO.add(this.ITEM_CERRAR);

        this.MENU_JAVA = new JMenu("JAVA");
        this.MENU_JAVA.add(this.ITEM_JAVADHC);

        this.MENU_BAR = new JMenuBar();
        this.MENU_BAR.add(this.MENU_ARCHIVO);
        this.MENU_BAR.add(this.MENU_JAVA);

        this.setJMenuBar(this.MENU_BAR);
    }

    public static void main(String[] ARGS)
    {
        //-- HACEMOS QUE SE INICIALIZE NUESTRA VENTANA JFRAME

        EJERCICIO_01 MI_INTERFAZ = new EJERCICIO_01();

        //-- HACEMOS QUE NUESTRA VENTANA SE VISIBLE

        MI_INTERFAZ.setVisible(true);
    }
}



EJERCICIO 02
  • RESULTADO
  • COD. FUENTE
/**
 *
 *  DUDAS SOBRE ESTE CODIGO, shamirdhc31@gmail.com
 */
import javax.swing.*;
import java.awt.*;
public class EJERCICIO_02 extends JFrame
{
    public JButton BTN_MUSICA,BTN_VIDEO,BTN_INTERNET,BTN_JAVADHC,BTN_INFO;
    public JToolBar CAJA_HERRAMIENTA_01,CAJA_HERRAMIENTA_02;
    public ImageIcon IMG_MUSICA,IMG_VIDEO,IMG_INTERNET,IMG_JAVADHC,IMG_INFO;

    public EJERCICIO_02() 
    {
        super("MI MENU CON JFRAME...");
        this.setSize(600,400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.IMG_VIDEO = new ImageIcon("IMAGENES/VIDEO.PNG");
        this.IMG_MUSICA = new ImageIcon("IMAGENES/MUSICA.PNG");
        this.IMG_INTERNET = new ImageIcon("IMAGENES/INTERNET.PNG");
        this.IMG_JAVADHC = new ImageIcon("IMAGENES/JAVADHC.PNG");
        this.IMG_INFO = new ImageIcon("IMAGENES/INFO.PNG");

        this.BTN_VIDEO = new JButton("VIDEO",this.IMG_VIDEO);
        this.BTN_MUSICA = new JButton("MUSICA",this.IMG_MUSICA);
        this.BTN_INTERNET = new JButton("INTERNET",this.IMG_INTERNET);
        this.BTN_JAVADHC = new JButton("JAVADHC",this.IMG_JAVADHC);
        this.BTN_INFO = new JButton("http://javadhc.blogspot.com/",this.IMG_INFO);

        this.CAJA_HERRAMIENTA_01 = new JToolBar();
        this.CAJA_HERRAMIENTA_01.add(this.BTN_VIDEO);
        this.CAJA_HERRAMIENTA_01.add(this.BTN_MUSICA);
        this.CAJA_HERRAMIENTA_01.add(this.BTN_INTERNET);

        this.CAJA_HERRAMIENTA_02 = new JToolBar();
        this.CAJA_HERRAMIENTA_02.add(this.BTN_JAVADHC);
        this.CAJA_HERRAMIENTA_02.add(this.BTN_INFO);

        BorderLayout DISTRIBUIDOR = new BorderLayout();
        this.setLayout(DISTRIBUIDOR);

        this.add(this.CAJA_HERRAMIENTA_01,BorderLayout.NORTH);
        this.add(this.CAJA_HERRAMIENTA_02,BorderLayout.SOUTH);
    }
    public static void main(String[] ARGUMENTOS)
    {
        EJERCICIO_02 MI_INTERFAZ = new EJERCICIO_02();
        MI_INTERFAZ.setVisible(true);
    }
}

EJERCICIO 03
  • RESULTADO
  • COD. FUENTE
/**
 *
 *  DUDAS SOBRE ESTE CODIGO, shamirdhc31@gmail.com
 */
import javax.swing.*;
import java.awt.*;
public class EJERCICIO_03 extends JFrame
{
    public MI_PANEL PANEL_01, PANEL_02;
    public JScrollPane DESLIZADOR;

    public EJERCICIO_03()
    {
        super("CAJAS DE TEXTO CON DESLIZADOR... JAVADHC");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        String NOMBRE_CAJA_01, NOMBRE_CAJA_02;

        do
        {
            JOptionPane.showMessageDialog(null,"SE VOLVERA A PREGUNTAR SI LOS NOMBRES ESTAN EN BLANCO");
            NOMBRE_CAJA_01 = JOptionPane.showInputDialog(null,"INDIQUE EL NOMBRE DE LA CAJA DE TEXTO 01","NOMBRE DE CAJA",JOptionPane.INFORMATION_MESSAGE);
            NOMBRE_CAJA_02 = JOptionPane.showInputDialog(null,"INDIQUE EL NOMBRE DE LA CAJA DE TEXTO 02","NOMBRE DE CAJA",JOptionPane.INFORMATION_MESSAGE);
        }
        while(NOMBRE_CAJA_01.isEmpty() || NOMBRE_CAJA_02.isEmpty());

        FlowLayout DISTRIBUIDOR = new FlowLayout();
        this.setLayout(DISTRIBUIDOR);

        PANEL_01 = new MI_PANEL(NOMBRE_CAJA_01);
        PANEL_02 = new MI_PANEL(NOMBRE_CAJA_02);

        this.add(PANEL_01);
        this.add(PANEL_02);
    }
    public static void main(String[] ARGUMENTOS)
    {
        EJERCICIO_03 MI_INTERFAZ = new EJERCICIO_03();
        MI_INTERFAZ.pack();
        MI_INTERFAZ.setVisible(true);
    }
}

class MI_PANEL extends JPanel
{
    public JScrollPane DESLIZADOR;
    public JTextArea COMENTARIOS;
    public JTextField NOMBRE_CAJA;

    public MI_PANEL(String NOMBRE)
    {
        super();

        this.NOMBRE_CAJA = new JTextField();
        this.NOMBRE_CAJA.setText("CAJA " + NOMBRE);
        this.NOMBRE_CAJA.setEditable(false);
        this.add(this.NOMBRE_CAJA);

        this.COMENTARIOS = new JTextArea(10,20);
        this.COMENTARIOS.setText("ESCRIBA AQUI SUS COMENTARIOS DE SU CAJA " + NOMBRE);

        this.DESLIZADOR = new JScrollPane(this.COMENTARIOS);
        this.add(this.DESLIZADOR);
    }
}

EJERCICIO 04
  • RESULTADO
  • COD. FUENTE
/**
 *
 *  DUDAS SOBRE ESTE CODIGO, shamirdhc31@gmail.com
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EJERCICIO_04 extends JFrame implements ActionListener
{
    public JLabel LETRA_NUMERO_01,LETRA_NUMERO_02,LETRA_RESULTADO;
    public JTextField NUMERO_01,NUMERO_02,RESULTADO;
    public JButton SUMAR;

    public EJERCICIO_04()
    {
        super("SUMA DE NUMEROS... JAVADHC");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(210,160);

        this.LETRA_NUMERO_01 = new JLabel("NUMERO 01");
        this.LETRA_NUMERO_02 = new JLabel("NUMERO 02");
        this.LETRA_RESULTADO = new JLabel("RESULTADO");

        this.NUMERO_01 = new JTextField(10);
        this.NUMERO_02 = new JTextField(10);
        this.RESULTADO = new JTextField(10);
        this.RESULTADO.setEditable(false);

        this.SUMAR = new JButton("PULSE PARA SUMAR");
        this.SUMAR.addActionListener(this);

        FlowLayout DISTRIBUIDOR = new FlowLayout();
        this.setLayout(DISTRIBUIDOR);

        this.add(this.LETRA_NUMERO_01);
        this.add(this.NUMERO_01);
        this.add(this.LETRA_NUMERO_02);
        this.add(this.NUMERO_02);
        this.add(this.SUMAR);
        this.add(this.LETRA_RESULTADO);
        this.add(this.RESULTADO);
    }

    public void actionPerformed(ActionEvent EVENTO)
    {
        float AUX_NUMERO_01,AUX_NUMERO_02,AUX_RESULTADO;

        try
        {
            AUX_NUMERO_01 = Float.parseFloat(this.NUMERO_01.getText());
            AUX_NUMERO_02 = Float.parseFloat(this.NUMERO_02.getText());
            AUX_RESULTADO = AUX_NUMERO_01 + AUX_NUMERO_02;

            this.RESULTADO.setText(String.valueOf(AUX_RESULTADO));
        }
        catch(Exception E)
        {
            this.RESULTADO.setText("ERROR AL SUMAR");
        }
    }
    public static void main(String[] ARGUMENTOS)
    {
        EJERCICIO_04 MI_INTERFAZ = new EJERCICIO_04();
        MI_INTERFAZ.setResizable(false);
        MI_INTERFAZ.setVisible(true);
    }
}


EJERCICIO 05
  • RESULTADO
  • COD. FUENTE
/**
 *
 *  DUDAS SOBRE ESTE CODIGO, shamirdhc31@gmail.com
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class EJERCICIO_05 extends JFrame implements ActionListener
{
    JRadioButton SUMAR,RESTAR,MULTIPLICAR,DIVIDIR;
    ButtonGroup OPERACIONES;
    JButton EJECUTAR_OPERACION;
    JLabel LETRA_NUMERO_01,LETRA_NUMERO_02,LETRA_RESULTADO;
    JTextField NUMERO_01,NUMERO_02,RESULTADO;

    public EJERCICIO_05()
    {
        super("SUMA CON 4 OPERACIONES");
        this.setSize(210,210);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.LETRA_NUMERO_01 = new JLabel("NUMERO 01");
        this.LETRA_NUMERO_02 = new JLabel("NUMERO 02");
        this.LETRA_RESULTADO = new JLabel("RESULTADO");

        this.NUMERO_01 = new JTextField(10);
        this.NUMERO_02 = new JTextField(10);
        this.RESULTADO = new JTextField(10);

        this.EJECUTAR_OPERACION = new JButton("EJECUTAR OPERACION");
        this.EJECUTAR_OPERACION.addActionListener(this);

        this.SUMAR = new JRadioButton("SUMAR",true);
        this.RESTAR = new JRadioButton("RESTAR",false);
        this.MULTIPLICAR = new JRadioButton("MULTIPLICAR",false);
        this.DIVIDIR = new JRadioButton("DIVIDIR",false);

        this.OPERACIONES = new ButtonGroup();
        this.OPERACIONES.add(this.SUMAR);
        this.OPERACIONES.add(this.RESTAR);
        this.OPERACIONES.add(this.MULTIPLICAR);
        this.OPERACIONES.add(this.DIVIDIR);

        FlowLayout DISTRIBUIDOR = new FlowLayout();
        this.setLayout(DISTRIBUIDOR);

        this.add(this.LETRA_NUMERO_01);
        this.add(this.NUMERO_01);
        this.add(this.LETRA_NUMERO_02);
        this.add(this.NUMERO_02);

        this.add(this.SUMAR);
        this.add(this.RESTAR);
        this.add(this.MULTIPLICAR);
        this.add(this.DIVIDIR);

        this.add(this.EJECUTAR_OPERACION);

        this.add(this.LETRA_RESULTADO);
        this.add(this.RESULTADO);        
    }
    public void actionPerformed(ActionEvent EVENTO)
    {
        float AUX_NUMERO_01,AUX_NUMERO_02,AUX_RESULTADO;

        try
        {
            AUX_RESULTADO = 0;
            AUX_NUMERO_01 = Float.parseFloat(this.NUMERO_01.getText());
            AUX_NUMERO_02 = Float.parseFloat(this.NUMERO_02.getText());

            if(this.SUMAR.isSelected())
            {
                AUX_RESULTADO = AUX_NUMERO_01 + AUX_NUMERO_02;
            }
            else if(this.RESTAR.isSelected())
            {
                AUX_RESULTADO = AUX_NUMERO_01 - AUX_NUMERO_02;
            }
            else if(this.MULTIPLICAR.isSelected())
            {
                AUX_RESULTADO = AUX_NUMERO_01 * AUX_NUMERO_02;
            }
            else if(this.DIVIDIR.isSelected())
            {
                AUX_RESULTADO = AUX_NUMERO_01 / AUX_NUMERO_02;
            }

            this.RESULTADO.setText(String.valueOf(AUX_RESULTADO));
        }
        catch(Exception E)
        {
            this.RESULTADO.setText("ERROR AL EJECUTAR");
        }
    }
    public static void main(String[] ARGUMENTOS)
    {
        EJERCICIO_05 MI_INTERFAZ = new EJERCICIO_05();
        MI_INTERFAZ.setResizable(false);
        MI_INTERFAZ.setVisible(true);
    }
}

EJERCICIO 06
  • RESULTADO
  • COD. FUENTE
/**
 *
 *  DUDAS SOBRE ESTE CODIGO, shamirdhc31@gmail.com
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EJERCICIO_06 extends JFrame implements ActionListener
{
    public JButton VERDE,AMARILLO,ROJO;
    public JPanel COLOR_PANEL;

    public EJERCICIO_06() 
    {
        super("COLORES... JAVADHC");
        this.setSize(400,400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        this.VERDE = new JButton("VERDE");
        this.VERDE.setBackground(Color.GREEN);
        this.VERDE.addActionListener(this);
        this.AMARILLO = new JButton("AMARILLO");
        this.AMARILLO.setBackground(Color.YELLOW);
        this.AMARILLO.addActionListener(this);
        this.ROJO = new JButton("ROJO");
        this.ROJO.setBackground(Color.RED);
        this.ROJO.addActionListener(this);

        this.COLOR_PANEL = new JPanel();
        this.COLOR_PANEL.setPreferredSize(new Dimension(300,300));
        this.COLOR_PANEL.setBackground(Color.BLACK);

        FlowLayout DISTRIBUIDOR_FRAME = new FlowLayout();
        this.setLayout(DISTRIBUIDOR_FRAME);
        
        this.add(this.VERDE);
        this.add(this.AMARILLO);
        this.add(this.ROJO);
        this.add(this.COLOR_PANEL);
    }
    public void actionPerformed(ActionEvent EVENTO)
    {
        Object BOTON_SELECCIONADO = EVENTO.getSource();

        if(BOTON_SELECCIONADO == this.VERDE)
        {
            this.COLOR_PANEL.setBackground(Color.GREEN);
        }
        else if(BOTON_SELECCIONADO == this.AMARILLO)
        {
            this.COLOR_PANEL.setBackground(Color.YELLOW);
        }
        else if(BOTON_SELECCIONADO == this.ROJO)
        {
            this.COLOR_PANEL.setBackground(Color.RED);
        }
    }
 
    public static void main(String[] ARGUMENTOS)
    {
        EJERCICIO_06 MI_INTERFAZ = new EJERCICIO_06();
        MI_INTERFAZ.setResizable(false);
        MI_INTERFAZ.setVisible(true);
    }
}

DALE CLICK A LA IMAGEN PARA BAJARTE EL PROYECTO CON TODAS LOS EJERCICIOS



IMPORTANTE : "TODOS LOS CODIGOS INDICADOS AQUI SON ESCRITOS POR MI, ASI QUE CUALQUIER DUDA O EJERCICIO QUE NO PUEDAN RESOLVER, NO DUDEN EN MANDARME UN E-MAIL A MI CORREO"
shamirdhc31@gmail.com

7 comentarios:

  1. hola ....muy buen aporte .....pero me gustaria q me ayudes con conexion a BD.....gracias

    ResponderEliminar
    Respuestas
    1. Este comentario ha sido eliminado por el autor.

      Eliminar
    2. gracias hermano por todo por compartir y podras mostrarnos un proyecto tuyo de como crear un JTable en un JFrame pero desde codigo solo desde codigo???? crearla desde cero??

      Eliminar
  2. MUCHAS GRACIAS,EXELENTE PAGINA Y TIENE TODO SU CONTENIDO DEL TEMA AL 100,
    OJALA UBIERA MAS COMO UDS EN LA NET
    GRACIAS

    ResponderEliminar
  3. Hola que tal, quisiera pedirte un favor. tengo que realizar el siguiente ejercicio en java eclipse y no logro hacerlo. podrias ayudarme?
    1- Desarrollar un panel para el control de 3 compresores de aire comprimido, teniendo en cuenta que:
    Se debe poder seleccionar el porcentaje de aire numérica y gráficamente. Los botones [+] y [-] pueden incrementar o decrementar de 10 en 10%. Se debe tener en cuenta el tamaño de la letra indicado.

    2- Por otro lado se debe seleccionar el tipo de compresor, para ello se dispone de tres Radios con la indicación Compresor 1, Compresor 2 y Compresor 3, respectivamente. Al pulsar uno de ellos mostrar en el panel correspondiente, el compresor seleccionado.

    3- Cuando se presiona en botón [Accionar], se debe mostrar en un panel la leyenda Correcto o Incorrecto. Se mostrará correcto en los siguientes casos:
    a) Si está seleccionado el Compresor 1 y el porcentaje se encuentre entre 10 y 40.
    b) Si está seleccionada la Compresor 2 y el porcentaje se encuentre entre 50 y 80.
    c) Si está seleccionada la Compresor 3 y el porcentaje se encuentre entre 90 y 100.

    4- Al presionar el botón [Inicializar], se debe fijar el porcentaje en cero y los paneles en blanco.

    5- Desarrollar un Menú que permita cambiar la selección de los compresores.

    ResponderEliminar