# Poner imagen en ejercicio java

[Inicio](https://www.todoexpertos.com/) › [Java](https://www.todoexpertos.com/temas/java) › [Ingeniería Informática](https://www.todoexpertos.com/temas/ingenieria-informatica) › [Desarrollo web](https://www.todoexpertos.com/temas/desarrollo-web) › [Programación](https://www.todoexpertos.com/temas/programacion) › [Java EE](https://www.todoexpertos.com/temas/java-ee)

- Fecha de publicación: 2018-06-22
- Enlace canónico (HTML): https://www.todoexpertos.com/preguntas/8to6cxa4vla5c678/poner-imagen-en-ejercicio-java

## Pregunta

*Por Anónimo* · 2018-06-22

Estoy haciendo un ejercicio que cambia el color de letra y el color de fondo pero necesito colocarle al botón de "Background" y al de "Foreground" una pequeña imagen que los represente. Este es el código:Ventana() { setSize(300, 200); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); crearPanel(); add(panel);blueB.addActionListener(this); yellowB.addActionListener(this); magentaB.addActionListener(this); cyanB.addActionListener(this); black.addActionListener(this); redB.addActionListener(this); blueF.addActionListener(this); yellowF.addActionListener(this); magentaF.addActionListener(this); cyanF.addActionListener(this); white.addActionListener(this); redF.addActionListener(this);}void crearPanel() {panel = new JPanel(); JPanel panelInferior = new JPanel(); JPanel panelSuperior = new JPanel(); JPanel panelDerecho = new JPanel(); JPanel panelIzquierdo = new JPanel();panelInferior.setLayout(new GridLayout(1, 2)); panelInferior.add(black = new JButton("BLACK")); panelInferior.add(white = new JButton("WHITE"));panelDerecho.setLayout(new GridLayout(6, 1)); panelDerecho.add(background = new JButton("BACKGROUNG")); panelDerecho.add(redB = new JButton("RED")); panelDerecho.add(blueB = new JButton("BLUE")); panelDerecho.add(yellowB = new JButton("YELLOW")); panelDerecho.add(magentaB = new JButton("MAGENTA")); panelDerecho.add(cyanB = new JButton("CYAN"));panelIzquierdo.setLayout(new GridLayout(6, 1)); panelIzquierdo.add(foreground = new JButton("FOREGROUNG")); panelIzquierdo.add(redF = new JButton("RED")); panelIzquierdo.add(blueF = new JButton("BLUE")); panelIzquierdo.add(yellowF = new JButton("YELLOW")); panelIzquierdo.add(magentaF = new JButton("MAGENTA")); panelIzquierdo.add(cyanF = new JButton("CYAN"));panel.setLayout(new BorderLayout()); panel.add(ejemplo = new JButton("ESTE ES UN EVENTO"), BorderLayout.NORTH); panel.add(area, BorderLayout.CENTER); panel.add(panelIzquierdo, BorderLayout.WEST); panel.add(panelDerecho, BorderLayout.EAST); panel.add(panelInferior, BorderLayout.SOUTH);}@Override public void actionPerformed(ActionEvent ae) { if (ae.getSource() == blueB) { area.setBackground(Color.BLUE); } else if (ae.getSource() == yellowB) { area.setBackground(Color.YELLOW); } else if (ae.getSource() == redB) { area.setBackground(Color.RED); } else if (ae.getSource() == cyanB) { area.setBackground(Color.CYAN); } else if (ae.getSource() == magentaB) { area.setBackground(Color.MAGENTA); } else if (ae.getSource() == white) { area.setBackground(Color.WHITE); } if (ae.getSource() == blueF) { area.setForeground(Color.BLUE); } else if (ae.getSource() == yellowF) { area.setForeground(Color.YELLOW); } else if (ae.getSource() == redF) { area.setForeground(Color.RED); } else if (ae.getSource() == cyanF) { area.setForeground(Color.CYAN); } else if (ae.getSource() == magentaF) { area.setForeground(Color.MAGENTA); } else if (ae.getSource() == black) { area.setForeground(Color.BLACK);}}

## Respuestas (0)

_Esta pregunta aún no tiene respuestas._

---
*Fuente: [https://www.todoexpertos.com/preguntas/8to6cxa4vla5c678/poner-imagen-en-ejercicio-java](https://www.todoexpertos.com/preguntas/8to6cxa4vla5c678/poner-imagen-en-ejercicio-java) — consultado el 2026-10-23. Al citar, enlaza la URL canónica e indica la fecha de consulta.*
