Background color de uma janela usando JColorChooser

2 respostas
FelipeMonfardini

Olá! Bem, estou tentando colorir o fundo da minha janela usando o ColorChoser
Consegui colorir a label mas não estou conseguindo colorir o fundo da janela.

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
// Classe teste e cria janela
public class teste extends JFrame {
// Inicia o botão e a label aqui pois vou precisar usar em outra classe
JLabel label = new JLabel("Escolha a cor:");
JButton button = new JButton("Cor");
public static void main(String[] args) {
    //cria-se a classe teste
	new teste();
  }
public teste() {
	JFrame janela = new JFrame("Java");
    this.setSize(300, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel painel = new JPanel();
    label.setForeground(null);
    painel.add(label);
    //Coloco um escutar de botão e crio o nome da classe da função do botão
    button.addActionListener(new Escutador());
    painel.add(button);
    this.add(painel);
    this.setVisible(true);
  }
//cria-se a classe com o nome da função do botão que implements o Action Listener
class Escutador implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    	
    	Color cor = JColorChooser.showDialog(null, "Escolha uma cor", label.getForeground());
    	 if (cor != null)
    	        label.setForeground(cor);
      
    }
  }
}
Agradeço desde já grato!

2 Respostas

D
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;



public class TestColorChooser implements ActionListener {
	private JFrame frame;
	private JButton button;
	private JColorChooser colorChooser;

	public TestColorChooser() {
		initComponents();
	}
	
	private void initComponents() {
		this.frame = new JFrame();
		this.frame.setSize(300, 300);
		this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.button = new JButton("Mudar Cor");
		this.button.addActionListener(this);
		this.frame.add(this.button, BorderLayout.SOUTH);
		this.frame.setVisible(true);
		this.colorChooser = new JColorChooser();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Color color = this.colorChooser.showDialog(null, "Selecione",
				this.frame.getContentPane().getBackground()); // se trataando de JFrames o background é o contentPane
		this.frame.getContentPane().setBackground(color);
	}
	
	public static void main(String[] args) {
		new TestColorChooser();
	}
}

isso deve ajudar

D
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;



public class TestColorChooser implements ActionListener {
	private JFrame frame;
	private JButton button;
	private JColorChooser colorChooser;

	public TestColorChooser() {
		initComponents();
	}
	
	private void initComponents() {
		this.frame = new JFrame();
		this.frame.setSize(300, 300);
		this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.button = new JButton("Mudar Cor");
		this.button.addActionListener(this);
		this.frame.add(this.button, BorderLayout.SOUTH);
		this.frame.setVisible(true);
		this.colorChooser = new JColorChooser();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Color color = this.colorChooser.showDialog(null, "Selecione",
				this.frame.getContentPane().getBackground()); // se trataando de JFrames o background é o contentPane
		this.frame.getContentPane().setBackground(color);
	}
	
	public static void main(String[] args) {
		new TestColorChooser();
	}
}

isso deve ajudar

Criado 7 de julho de 2012
Ultima resposta 7 de jul. de 2012
Respostas 2
Participantes 2