Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: Help changing a JPanel background color
|
Posted: Feb 22, 2003 11:36 AM
|
|
The following is some code which uses several different ways to determine what was clicked and what container the item clicked is in.
/* JPanelColorChanger.java * @author: Charles Bell * @version: Feb 22, 2003 */
import java.awt.*; import java.awt.event.*;
import javax.swing.*;
/** Basic JFrame application with three JPanels. Each JPanel * has a different Border. An ActionListener is added to * each JPanel. Three JButtons are added to each JPanel to * set its background color. */ public class JPanelColorChanger extends JFrame implements ActionListener{ private JPanel topPanel; private JPanel middlePanel; private JPanel bottomPanel; private boolean inAnApplet = true;
/** Constructs a JPanelColorChanger object and initializes the GUI. */ public JPanelColorChanger(){ super("JPanelColorChanger"); } /** Called when this is run as an application. */ public static void main(String[] args){ JPanelColorChanger colorChanger = new JPanelColorChanger(); colorChanger.inAnApplet = false; colorChanger.init(); }
/** Initializes the JFrame GUI with three JPanels. * An ActionListener is added to each JPanel. * Three JButtons are added to each JPanel to * set its background color. */ public void init(){ /* Set the application to Exit if the user * closes the JFrame window. If in an applet * skip it. */ if (!inAnApplet) setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); System.out.println("inAnApplet: " + inAnApplet); /* Initialize the three JPanels */ topPanel = new JPanel(); middlePanel = new JPanel(); bottomPanel = new JPanel(); /* Add JButtons to each JPanel so the user * can change the background color of the JPanel. */ JButton redButton1 = new JButton("Red"); JButton greenButton1 = new JButton("Green"); JButton blueButton1 = new JButton("Blue"); JButton redButton2 = new JButton("Red"); JButton greenButton2 = new JButton("Green"); JButton blueButton2 = new JButton("Blue");
JButton redButton3 = new JButton("Red"); JButton greenButton3 = new JButton("Green"); JButton blueButton3 = new JButton("Blue"); topPanel.add(redButton1, BorderLayout.WEST); topPanel.add(greenButton1, BorderLayout.CENTER); topPanel.add(blueButton1, BorderLayout.EAST); middlePanel.add(redButton2, BorderLayout.WEST); middlePanel.add(greenButton2, BorderLayout.CENTER); middlePanel.add(blueButton2, BorderLayout.EAST); bottomPanel.add(redButton3, BorderLayout.WEST); bottomPanel.add(greenButton3, BorderLayout.CENTER); bottomPanel.add(blueButton3, BorderLayout.EAST); /* Add ActionListener to the JButtons. * Since this class implements the ActionListener * interface, it can serve as a ActionListener. */ redButton1.addActionListener(this); greenButton1.addActionListener(this); blueButton1.addActionListener(this); redButton2.addActionListener(this); greenButton2.addActionListener(this); blueButton2.addActionListener(this); redButton3.addActionListener(this); greenButton3.addActionListener(this); blueButton3.addActionListener(this); /* Add the JPanels to the contentPane of the JFrame. */ getContentPane().add(topPanel, BorderLayout.NORTH); getContentPane().add(middlePanel, BorderLayout.CENTER); getContentPane().add(bottomPanel, BorderLayout.SOUTH); /* Pack the dimensions down to the preferred sizes. */ pack(); /* Center the JFrame. */ setLocationRelativeTo(null); /* Display the JFrame. */ show(); } /** Implements the ActionListener interface. * Uses the action command string obtained from * the getActionCommand method of ActionEvent to * set the color selection. * Uses the ActionEvent getSource() method to find * component which fired the ActionEvent. Uses * instanceof to test whether the action event source * was a JButton. If so, tests if each JPanel is the * ancestor of the JButton component using the Container * isAncestorOf method and sets the background color. * Also gets the Parent container object of the JButton. * to tell what container object the action event * originated in. */ public void actionPerformed(ActionEvent actionEvent){ System.out.println(actionEvent.getActionCommand()); Color color = null; if (actionEvent.getActionCommand().compareTo("Red") == 0) color = Color.red; if (actionEvent.getActionCommand().compareTo("Green") == 0) color = Color.green; if (actionEvent.getActionCommand().compareTo("Blue") == 0) color = Color.blue; if (actionEvent.getSource() instanceof JButton) { System.out.println("JButton clicked"); if (topPanel.isAncestorOf((JButton)actionEvent.getSource())){ System.out.println("JButton in topPanel clicked"); topPanel.setBackground(color); }else if (middlePanel.isAncestorOf((JButton)actionEvent.getSource())){ System.out.println("JButton in middlePanel clicked"); middlePanel.setBackground(color); }else if (bottomPanel.isAncestorOf((JButton)actionEvent.getSource())){ System.out.println("JButton in bottomPanel clicked"); bottomPanel.setBackground(color); } if (((JButton)actionEvent.getSource()).getParent().equals(topPanel)) System.out.println("topPanel clicked"); if (((JButton)actionEvent.getSource()).equals(middlePanel)) System.out.println("middlePanel clicked"); if (((JButton)actionEvent.getSource()).equals(bottomPanel)) System.out.println("bottomPanel clicked"); } } }
|
|