-
AutorEntradas
-
7 septiembre, 2023 a las 12:31 am #23709
JaimeParticipantemi codigo no funciona, no cambia el color de fondo.
package graficos; ///VIDEO 81
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PruebaAcciones {
<span style=”white-space: normal;”> public static void main(String[] args) {</span>
<span style=”white-space: normal;”> marcoAccion marco= new marcoAccion();</span>
<span style=”white-space: normal;”> marco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span>
<span style=”white-space: normal;”> marco.setVisible(true); </span>
<span style=”white-space: normal;”> }</span>
}
class marcoAccion extends JFrame{
<span style=”white-space: normal;”> public marcoAccion() {</span>
<span style=”white-space: normal;”> setTitle(“Prueba Acciones”);</span>
<span style=”white-space: normal;”> setBounds(600,150,600,300);</span>
<span style=”white-space: normal;”> PanelAccion lamina= new PanelAccion();</span>
<span style=”white-space: normal;”> add(lamina);</span>
<span style=”white-space: normal;”> }</span>
}
class PanelAccion extends JPanel{
<span style=”white-space: normal;”> public PanelAccion() {</span>
AccionColor accionAmarillo =new AccionColor(“Amarillo “, new ImageIcon(“src/graficos/smalIcon.png”),Color.yellow);
AccionColor accionAzul =new AccionColor(“Azul “, new ImageIcon(“src/graficos/smalIcon.png”),Color.blue);
AccionColor accionRojo =new AccionColor(“Rojo “, new ImageIcon(“src/graficos/smalIcon.png”),Color.red);
<span style=”white-space: normal;”> /*JButton botonAmarrillo = new JButton(accionAmarillo);</span>
<span style=”white-space: normal;”> add(botonAmarillo);*/</span>
<span style=”white-space: normal;”> add(new JButton(accionAmarillo));</span>
<span style=”white-space: normal;”> add(new JButton(accionAzul));</span>
<span style=”white-space: normal;”> add(new JButton(accionRojo));</span>
<span style=”white-space: normal;”> /*JButton botonAmarillo = new JButton(“Amarillo”);</span>
<span style=”white-space: normal;”> JButton botonAzul = new JButton(“Azul”);</span>
<span style=”white-space: normal;”> JButton botonRojo = new JButton(“Rojo”);</span>
<span style=”white-space: normal;”> add(botonAmarillo);</span>
<span style=”white-space: normal;”> add(botonAzul);</span>
<span style=”white-space: normal;”> add(botonRojo);*/ </span>
<span style=”white-space: normal;”> InputMap mapaEntrada=getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);</span>
<span style=”white-space: normal;”> //KeyStroke teclaAmarillo=KeyStroke.getKeyStroke(“ctrl A”);</span>
<span style=”white-space: normal;”> mapaEntrada.put(KeyStroke.getKeyStroke(“ctrl A”),” fondoAmarillo”);</span>
<span style=”white-space: normal;”> mapaEntrada.put(KeyStroke.getKeyStroke(“ctrl B”),” fondoAzul”);</span>
<span style=”white-space: normal;”> mapaEntrada.put(KeyStroke.getKeyStroke(“ctrl R”),” fondoRojo”);</span>
<span style=”white-space: normal;”> ActionMap mapaAccion = getActionMap();</span>
<span style=”white-space: normal;”> mapaAccion.put(“fondoAmarillo”, accionAmarillo);</span>
<span style=”white-space: normal;”> mapaAccion.put(“fondoAzul”, accionAzul);</span>
<span style=”white-space: normal;”> mapaAccion.put(“fondoRojo”, accionRojo);</span>
}
<span style=”white-space: normal;”> private class AccionColor extends AbstractAction{</span>
<span style=”white-space: normal;”> public AccionColor(String nombre, Icon icono, Color colorBoton) {</span>
<span style=”white-space: normal;”> putValue(Action.NAME, nombre);</span>
<span style=”white-space: normal;”> putValue(Action.SMALL_ICON, icono);</span>
<span style=”white-space: normal;”> putValue(Action.SHORT_DESCRIPTION, “Poner lamina de color “+nombre);</span>
<span style=”white-space: normal;”> putValue(“ColorDeFondo”, colorBoton);</span>
<span style=”white-space: normal;”> }</span>
<span style=”white-space: normal;”> public void actionPerformed(ActionEvent e) {</span>
<span style=”white-space: normal;”> Color c =(Color) getValue(“colorDeFondo”);</span>
<span style=”white-space: normal;”> setBackground(c);</span>
<span style=”white-space: normal;”> System.out.println(“Nombre: “+getValue(Action.NAME)+ “. Descripcion: “+ getValue(Action.SHORT_DESCRIPTION));</span>
<span style=”white-space: normal;”> } </span>
}
}
-
AutorEntradas
- Debes estar registrado para responder a este debate.