Один из вариантов
public class Enter {
public static void main(String... args){
new ColorLayout();
}
}
import java.awt.*;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class ColorLayout extends JFrame{
public ColorLayout() {
this.add(new ColorPanel());
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(new Dimension(200,200));
this.setVisible(true);
}
public class ColorPanel extends JPanel{
private JButton button;
private JPanel buttonpanel;
public ColorPanel() {
buttonpanel = new JPanel();
buttonpanel.setBackground(Color.decode("#eeeeee"));
button = new JButton("Жми, чтобы узнать цвет");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println(buttonpanel.getBackground());
}
});
buttonpanel.add(button);
this.add(buttonpanel);
}
}
}