martes, 23 de enero de 2018

clase AbstractAction

su clase proporciona implementaciones predeterminadas para la interfaz de acción JFC. Los comportamientos estándar como los métodos get y set para las propiedades del objeto Action (icono, texto y habilitado) se definen aquí. El desarrollador solo necesita subclasificar esta clase abstracta y definir el método actionPerformed.

tambien tiene la funcionalidad de operar cuando se tiene multiples enventos

cuenta con diferentes metodos de la interfas Action y que programadores se dieron el tiempo de convertirlo en una clase abstracta para no poder utilizar todos los metodos si no se requieren:

putValue(Action.NAME,nombre); putValue(); nos permite guardar el parametro que pasa como nombre para luego utilizarlo

getValue(Action.NAME); nos permite recuperar la informacion que se guardo en Action.NAME

PARA MAS INFORMACION VISTAR LA PAGINA DE LA API DE JAVA 


EJEMPLO

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cursojava;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.security.Principal;
import javafx.scene.layout.GridPane;
import javax.swing.*;

/**
 *
 * @author USUARIO
 */
public class CursoJava {

    public static void main(String[] args) {
        new PrincipalC();

    }
}

class PrincipalC extends JFrame {

    public PrincipalC() {
        setVisible(true);
        setBounds(100, 200, 500, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(new PaintAction());
    }
}

class PaintAction extends JPanel {

    PaintAction a;

    public PaintAction() {
        AccionColor accionAmarillo = new AccionColor("Amarillo", new ImageIcon("src/cursojava/1.png"), Color.YELLOW);
        AccionColor accionVerde = new AccionColor("Verde", new ImageIcon("src/cursojava/2.png"), Color.GREEN);
        AccionColor accionRojo = new AccionColor("Rojo", new ImageIcon("src/cursojava/icono.png"), Color.RED);

        add(new JButton(accionAmarillo));
        add(new JButton(accionRojo));
        add(new JButton(accionVerde));

        /*
        JButton amarillo = new JButton("Amarillo");
        JButton rojo = new JButton("Rojo");
        JButton verde = new JButton("Verde");
        add(amarillo);
        add(rojo);
        add(verde);*/
    }

    private class AccionColor extends AbstractAction {

        public AccionColor(String nombre, Icon icono, Color color_Boton) {
            putValue(Action.NAME, nombre);
            putValue(Action.SMALL_ICON, icono);
            putValue(Action.SHORT_DESCRIPTION, "Poner la lamina de color " + nombre);
            putValue("color de fondo", color_Boton);

        }

        @Override
        public void actionPerformed(ActionEvent ae) {
            Color c = (Color) getValue("color de fondo");
            setBackground(c);
        }

    }

}

Share:

1 comentario:

BTemplates.com

Buscar este blog

Archivo del Blog

Con tecnología de Blogger.