/** * TricolorPanel.java * @author Jeff Ondich, 27 October 2010 * * Support class for the LayoutExample.java program. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TricolorPanel extends JPanel implements ActionListener { private JPanel topSubpanel; private JPanel middleSubpanel; private JPanel bottomSubpanel; public TricolorPanel(Color topColor, Color middleColor, Color bottomColor) { this.setOpaque(true); this.setLayout(new GridLayout(0, 1)); this.topSubpanel = new JPanel(); this.topSubpanel.setOpaque(true); this.topSubpanel.setBackground(topColor); this.add(this.topSubpanel); this.middleSubpanel = new JPanel(); this.middleSubpanel.setOpaque(true); this.middleSubpanel.setBackground(middleColor); this.add(this.middleSubpanel); this.bottomSubpanel = new JPanel(); this.bottomSubpanel.setOpaque(true); this.bottomSubpanel.setBackground(bottomColor); this.add(this.bottomSubpanel); } public void actionPerformed(ActionEvent event) { // Rotate the background colors of the three subpanels. Color topColor = this.topSubpanel.getBackground(); Color middleColor = this.middleSubpanel.getBackground(); Color bottomColor = this.bottomSubpanel.getBackground(); this.topSubpanel.setBackground(bottomColor); this.middleSubpanel.setBackground(topColor); this.bottomSubpanel.setBackground(middleColor); } }