martes, 21 de marzo de 2017

Eventos en Java


package eventos;
import java.awt.*;
public class Principal extends javax.swing.JFrame {
    public Principal() {
        initComponents();
    }
  private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                               
        // TODO add your handling code here:
        CheckBox Check= new CheckBox();
        Check.setVisible(true);
    }                                        

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        ListBox List =new ListBox();
        List.setVisible(true);
    }                                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO ad your handling code here:

        String eleccion = this.cmbMetodos.getSelectedItem().toString();
         if(eleccion.equals("Naranja")){
           this.getContentPane().setBackground(Color.ORANGE);
        }
        if(eleccion.equals("Amarrillo")){
           this.getContentPane().setBackground(Color.YELLOW);
        }
        if(eleccion.equals("Azul")){
           this.getContentPane().setBackground(Color.BLUE);
        }
        if(eleccion.equals("Verde")){
           this.getContentPane().setBackground(Color.GREEN);
        }
        if(eleccion.equals("Negro")){
           this.getContentPane().setBackground(Color.BLACK);
        }
        if(eleccion.equals("Rojo")){
           this.getContentPane().setBackground(Color.RED);
        }
        if(eleccion.equals("Morado")){
           this.getContentPane().setBackground(Color.MAGENTA);
        }
        if(eleccion.equals("Rosa")){
           this.getContentPane().setBackground(Color.PINK);
        }
        if(eleccion.equals("Gris")){
           this.getContentPane().setBackground(Color.gray);
        }
        if(eleccion.equals("Azul Claro")){
           this.getContentPane().setBackground(Color.CYAN);
        }
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         

         Principal.this.setSize(1000, 800);
    }                                        

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        System.exit(0);
    }                                        

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        if (jLabel.isVisible()){
            jLabel.setVisible(false);
        }
        else{
            jLabel.setVisible(true);
        }       
    }                                        
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        Principal.this.setSize(500, 400);
    }


public class ListBox extends javax.swing.JFrame {
    
    public ListBox() {
        initComponents();
        //Método de arranque.
        inicio();

    }
private DefaultListModel modelList; 
   private void inicio(){
        String [] nom  = { ""};
//DefaultListModel para manejar las lista.
          modelList = new DefaultListModel();
//Agregamos datos a la lista utilizando for
        for (String string : nom) {
       modelList.addElement(string);
       jList1.setModel(modelList);
       jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
       jList1.setSelectedIndex(0);
       jList1.setVisibleRowCount(7);
    }
   }
    private void eliminar(){
  //Método para eliminar elementos de la lista Se optiene el índice del elemento seleccionado
     int selectedIndex = jList1.getSelectedIndex();
 //Si selectedIndex es igual a ‐1, no hay elemento seleccionado
      if(selectedIndex!=-1){
      modelList.remove(selectedIndex);
      }
    else{
   JOptionPane.showMessageDialog(null, "Seleccione un elemento de la lista.");
   }
}
    //Método para comprobar datos repetidos
     protected boolean enLista(String name) {
       return modelList.contains(name);
}
     private void agregar(){
//Método para agregar elementos a la lista
//Comprobamos que el campo de datos no este vacio
         if(txtTexto.getText().isEmpty()){
           JOptionPane.showMessageDialog(null, "Campo vacio.");
         }
           else{
//Utilizando el método enLista, comprobamos que no se repitan los datos.
         if(enLista(txtTexto.getText())){
          JOptionPane.showMessageDialog(null, "Nombre repetido..");
          }
//Agregamos elemento y limpiamos campo.
         else{
          modelList.addElement(txtTexto.getText());
         txtTexto.setText(null);
         }
       }

     }

private void BtnRegresarActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        Principal prin = new Principal();
        prin.setVisible(true);
        prin.setMaximumSize(null);
        this.dispose();
    }                                           

    private void BtnAñadirActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
       agregar();   
    }                                         

    private void BtnEliminarActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        eliminar();

    } 



public class CheckBox extends javax.swing.JFrame {

    public CheckBox() {
        initComponents();
    }
    
  public String ValidarDatos() {//Metodo para comprobar que los datos esten completos
        String msj = "";
        if (txtTexto.getText().equals("")) {//Si txtID esta vacio
            msj += "Por favor digite el Nombre \n";
            txtTexto.requestFocusInWindow();
        }      
        return msj;//devuelve msj   
        //validar que el usuario no existe
    }
  private void BtnMostrarActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        JTextField Resultado = txtTexto;
        if (ValidarDatos().equals("")) {
        try{
            JOptionPane.showMessageDialog(null, Resultado);
            if(jCheckBox1.isSelected() == true){
                JOptionPane.showMessageDialog(null, "Usted  es Mayor de edad");
            }else{
                JOptionPane.showMessageDialog(null, "Usted no es mayor de edad");
            }
             if (jCheckBox2.isSelected() ==true){
                JOptionPane.showMessageDialog(null, "Usted tiene Hijos");
             }
             else{
                   JOptionPane.showMessageDialog(null, "Usted no tiene Hijos");
             }
             if (jCheckBox3.isSelected() ==true){
                 JOptionPane.showMessageDialog(null,"Usted estudió una Carrera");
             }
             else{
                 JOptionPane.showMessageDialog(null,"Usted no estudió una Carrera");
             }
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
            CheckBox Check = new CheckBox();
        }
      }
        else{
         JOptionPane.showMessageDialog(rootPane, "ERROR!! \n" + ValidarDatos(), "Verificando Dato",
                JOptionPane.ERROR_MESSAGE);   
        }
    }                                          

    private void BtnRegresarActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        Principal prin = new Principal();
        prin.setVisible(true);
        prin.setMaximumSize(null);
        this.dispose();
    }              

No hay comentarios:

Publicar un comentario