import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
public class Test extends JFrame {
private JPanel parts[];
private final static Color COLORS[] = {
Color.red, Color.green, Color.blue, Color.magenta, Color.yellow, Color.black
};
public Test() {
super("Delt i fire");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
parts = new JPanel[4];
for (int i = 0; i < parts.length; i++) {
parts[i] = new ColoredPanel(i);
getContentPane().add(parts[i]);
}
setSize(600, 400);
setVisible(true);
}
private class ColoredPanel extends JPanel {
private int index;
public ColoredPanel(int colorIndex) {
this.index = colorIndex;
setBackground(COLORS[index % COLORS.length]);
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
setBackground(COLORS[(++index) % COLORS.length]);
}
});
}
}
public static void main(String args[]) {
new Test();
}
}