import java.awt.Color; import javax.swing.JLayeredPane; public class LayeredFrame_Correct extends javax.swing.JFrame { private final Color[] colors = new Color[]{Color.BLUE, Color.PINK, Color.GREEN, Color.YELLOW, Color.CYAN, Color.WHITE}; private int colorIndex; /** Creates new form LayeredFrame */ public LayeredFrame_Correct() { initComponents(); setTitle("Correct Manual Code"); colorIndex = 0; } private Color getNextColor() { if (colorIndex < colors.length - 1) { colorIndex++; } else { colorIndex = 0; } return colors[colorIndex]; } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") private void initComponents() { layeredPane = new javax.swing.JLayeredPane(); lowerPanel = new javax.swing.JPanel(); colorButton = new javax.swing.JButton(); upperPanel = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); lowerPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2)); // Call setLayer() BEFORE adding component to JLayeredPane // This is optional, since objects are added at DEFAULT_LAYER unless otherwise specified layeredPane.setLayer(lowerPanel, JLayeredPane.DEFAULT_LAYER); layeredPane.add(lowerPanel); lowerPanel.setBounds(10, 10, 380, 280); colorButton.setText("Change Color"); colorButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { changeLowerPanelBackground(evt); } }); layeredPane.add(colorButton); colorButton.setBounds(140, 330, 120, 25); upperPanel.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.red, 2)); // Call setLayer() BEFORE adding component to JLayeredPane layeredPane.setLayer(upperPanel, JLayeredPane.MODAL_LAYER); layeredPane.add(upperPanel); upperPanel.setBounds(10, 10, 380, 140); getContentPane().add(layeredPane, java.awt.BorderLayout.CENTER); setBounds(0, 0, 418, 440); } private void changeLowerPanelBackground(java.awt.event.ActionEvent evt) { Color nextColor = getNextColor(); lowerPanel.setBackground(nextColor); lowerPanel.repaint(); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new LayeredFrame_Correct().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton colorButton; private javax.swing.JLayeredPane layeredPane; private javax.swing.JPanel lowerPanel; private javax.swing.JPanel upperPanel; // End of variables declaration }