
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ColorButton1 extends JFrame implements ActionListener { private JButton[] buttons = new JButton[3]; // 3 buttons with color private JLabel label = new JLabel(); private Container contentPane = getContentPane(); private JPanel northPanel = new JPanel(new GridLayout(1,0)); // 1 row public ColorButton1() { buttons[0] = new JButton(); buttons[0].setBackground(Color.red); buttons[0].setActionCommand("red"); buttons[0].addActionListener(this); northPanel.add(buttons[0]); buttons[1] = new JButton(); buttons[1].setBackground(Color.blue); buttons[1].setActionCommand("blue"); buttons[1].addActionListener(this); northPanel.add(buttons[1]); buttons[2] = new JButton(); buttons[2].setBackground(Color.green); buttons[2].setActionCommand("green"); buttons[2].addActionListener(this); northPanel.add(buttons[2]); // set a bigger size of northPanel northPanel.setPreferredSize(new java.awt.Dimension(300,50)); label.setOpaque(true); contentPane.add(northPanel, BorderLayout.NORTH); contentPane.add(label); } public void actionPerformed(ActionEvent e) { String color = e.getActionCommand(); if(color.equals("red")) { label.setBackground(Color.red); } else if(color.equals("blue")) { label.setBackground(Color.blue); } else if(color.equals("green")) { label.setBackground(Color.green); } } public static void main(String[] args) { JFrame f = new ColorButton1(); f.setSize(300,150); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } } |
setActionCommand(String actionCommand)e.getActionCommand() to find out which button you click
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ColorButton2 extends JFrame implements ActionListener { private JButton[] buttons = new JButton[3]; // 3 buttons with color private JLabel label = new JLabel(); private Container contentPane = getContentPane(); private JPanel northPanel = new JPanel(new GridLayout(1,0)); // 1 row public ColorButton2() { buttons[0] = new JButton(); buttons[0].setBackground(Color.red); buttons[0].addActionListener(this); northPanel.add(buttons[0]); buttons[1] = new JButton(); buttons[1].setBackground(Color.blue); buttons[1].addActionListener(this); northPanel.add(buttons[1]); buttons[2] = new JButton(); buttons[2].setBackground(Color.green); buttons[2].addActionListener(this); northPanel.add(buttons[2]); // set a bigger size of northPanel northPanel.setPreferredSize(new java.awt.Dimension(300,50)); label.setOpaque(true); contentPane.add(northPanel, BorderLayout.NORTH); contentPane.add(label); } public void actionPerformed(ActionEvent e) { if(e.getSource() == buttons[0]) { label.setBackground(Color.red); } else if(e.getSource() == buttons[1]) { label.setBackground(Color.blue); } else if(e.getSource() == buttons[2]) { label.setBackground(Color.green); } } public static void main(String[] args) { JFrame f = new ColorButton2(); f.setSize(300,150); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } } |
e.getSource() to find out which button you click. e.getSource() == button[i].
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ColorButton3 extends JFrame implements ActionListener { private JButton[] buttons; private Color[] colors = {Color.red, Color.blue, Color.green, Color.yellow, Color.pink}; private JLabel label = new JLabel(); private Container contentPane = getContentPane(); private JPanel northPanel = new JPanel(new GridLayout(1,0)); // 1 row public ColorButton3() { buttons = new JButton[colors.length]; for(int i = 0; i < buttons.length; i++) { buttons[i] = new JButton(); buttons[i].setBackground(colors[i]); buttons[i].addActionListener(this); northPanel.add(buttons[i]); } // set a bigger size of northPanel northPanel.setPreferredSize(new java.awt.Dimension(300,50)); label.setOpaque(true); contentPane.add(northPanel, BorderLayout.NORTH); contentPane.add(label); } public void actionPerformed(ActionEvent e) { // e.getSource() returns Component // type casting to JButton JButton button = (JButton)e.getSource(); Color color = button.getBackground(); label.setBackground(color); } public static void main(String[] args) { JFrame f = new ColorButton3(); f.setSize(300,150); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } } |
(JButton)e.getSource() to get the button you click. e.getSource() returns Component
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ColorButton4 extends JFrame { private JButton[] buttons = new JButton[3]; // 3 buttons with color private JLabel label = new JLabel(); private Container contentPane = getContentPane(); private JPanel northPanel = new JPanel(new GridLayout(1,0)); // 1 row public ColorButton4() { buttons[0] = new JButton(); buttons[0].setBackground(Color.red); buttons[0].addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { label.setBackground(Color.red); } } ); northPanel.add(buttons[0]); buttons[1] = new JButton(); buttons[1].setBackground(Color.blue); buttons[1].addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { label.setBackground(Color.blue); } } ); northPanel.add(buttons[1]); buttons[2] = new JButton(); buttons[2].setBackground(Color.green); buttons[2].addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { label.setBackground(Color.green); } } ); northPanel.add(buttons[2]); // set a bigger size of northPanel northPanel.setPreferredSize(new java.awt.Dimension(300,50)); label.setOpaque(true); contentPane.add(northPanel, BorderLayout.NORTH); contentPane.add(label); } public static void main(String[] args) { JFrame f = new ColorButton4(); f.setSize(300,150); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } } |
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ColorButton5 extends JFrame { private JButton[] buttons; private Color[] colors = {Color.red, Color.blue, Color.green, Color.yellow, Color.pink}; private JLabel label = new JLabel(); private Container contentPane = getContentPane(); private JPanel northPanel = new JPanel(new GridLayout(1,0)); // 1 row public ColorButton5() { buttons = new JButton[colors.length]; for(int i = 0; i < buttons.length; i++) { buttons[i] = new JButton(); buttons[i].setBackground(colors[i]); buttons[i].addActionListener(new Listener()); northPanel.add(buttons[i]); } // set a bigger size of northPanel northPanel.setPreferredSize(new java.awt.Dimension(300,50)); label.setOpaque(true); contentPane.add(northPanel, BorderLayout.NORTH); contentPane.add(label); } public static void main(String[] args) { JFrame f = new ColorButton5(); f.setSize(300,150); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } // inner class class Listener implements ActionListener { public void actionPerformed(ActionEvent e) { Color color = ((JButton)e.getSource()).getBackground(); label.setBackground(color); } } } |
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ColorButton6 extends JFrame { private JButton[] buttons; private Color[] colors = {Color.red, Color.blue, Color.green, Color.yellow, Color.pink}; protected JLabel label = new JLabel(); private Container contentPane = getContentPane(); private JPanel northPanel = new JPanel(new GridLayout(1,0)); // 1 row public ColorButton6() { buttons = new JButton[colors.length]; for(int i = 0; i < buttons.length; i++) { buttons[i] = new JButton(); buttons[i].setBackground(colors[i]); buttons[i].addActionListener(new Listener(this)); northPanel.add(buttons[i]); } // set a bigger size of northPanel northPanel.setPreferredSize(new java.awt.Dimension(300,50)); label.setOpaque(true); contentPane.add(northPanel, BorderLayout.NORTH); contentPane.add(label); } public static void main(String[] args) { JFrame f = new ColorButton6(); f.setSize(300,150); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } } // inner class class Listener implements ActionListener { ColorButton6 colorButton; public Listener(ColorButton6 colorButton) { this.colorButton = colorButton; } public void actionPerformed(ActionEvent e) { Color color = ((JButton)e.getSource()).getBackground(); colorButton.label.setBackground(color); } } |
private JLabel label to protected JLabel label so that the listener can access the member.