import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * Write a program that shows a frame with three buttons labeled “Red”, * “Green”, and “Blue”, and a label containing an icon showing a circle that is * initially red. As the user clicks the buttons, the fill color of the circle should * change. When you change the color, you need to invoke the repaint * method on the label. The call to repaint ensures that the paintIcon method * is called so that the icon can be repainted with the new color. */ public class C4Q14 { public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(() -> { javax.swing.JFrame frame = new javax.swing.JFrame("C4Q14"); frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400); // frame.setLayout(new java.awt.BorderLayout()); javax.swing.JLabel label = new javax.swing.JLabel(new CircleIcon(java.awt.Color.RED, 300)); frame.add(label, java.awt.BorderLayout.CENTER); javax.swing.JPanel panel = new javax.swing.JPanel(); panel.setLayout(new java.awt.FlowLayout()); javax.swing.JButton redButton = new javax.swing.JButton("Red"); redButton.addActionListener((e) -> { label.setIcon(new CircleIcon(java.awt.Color.RED, 300)); label.repaint(); }); panel.add(redButton); javax.swing.JButton greenButton = new javax.swing.JButton("Green"); greenButton.addActionListener((e) -> { label.setIcon(new CircleIcon(java.awt.Color.GREEN, 300)); label.repaint(); }); greenButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { label.setIcon(new CircleIcon(java.awt.Color.GREEN, 300)); label.repaint(); } }); panel.add(greenButton); javax.swing.JButton blueButton = new javax.swing.JButton("Blue"); blueButton.addActionListener((e) -> { label.setIcon(new CircleIcon(java.awt.Color.BLUE, 300)); label.repaint(); }); panel.add(blueButton); // frame.add(panel, java.awt.BorderLayout.SOUTH); frame.setVisible(true); }); } }