---
title: "Java Swing学习笔记4_JColorChooser"
author: "Hode"
date: "2009-04-11T13:52:52Z"
tags: []
slug: "java-swing-4-jcolorchooser"
draft: false
lastmod: "2009-04-12T23:57:21Z"
id: 365559
url: "https://hode.iteye.com/blog/365559"
---

**JColorChooser**

		
```java

banner = new JLabel("Welcome,please select the color", JLabel.CENTER);
		banner.setForeground(Color.yellow);
		banner.setAutoscrolls(true);
		banner.setFont(new Font("SansSerif", Font.BOLD, 24));
		banner.setOpaque(true);
		banner.setBackground(Color.blue);
        banner.setPreferredSize(new Dimension(100, 65));

		JPanel bannerPanel = new JPanel();
		bannerPanel.setLayout(new BorderLayout());
		bannerPanel.add(banner);

		chooser = new JColorChooser();
		// custom your color chooser,so custom the abstractcolorchooserpanel
		AbstractColorChooserPanel panels[] = { new CrayonPanel() };
		chooser.setChooserPanels(panels);
		chooser.setColor(banner.getForeground());
        
		chooser.getSelectionModel().addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				banner.setForeground(chooser.getColor());
			}
		});

		// pagestart,pageend,linestart,lineend,center(like
		// north,south,west,east,center),the former is recommonded;
		add(bannerPanel, BorderLayout.PAGE_START);
		add(chooser, BorderLayout.PAGE_END);
```


**AbstractColorChooserPanel**

如果要实现自定义的colorchooser，则只是需要继承该抽象类而已。

```java

   /**
      * Invoked automatically when the model's state changes.
      * It is also called by <code>installChooserPanel</code> to allow
      * you to set up the initial state of your chooser.
      * Override this method to update your <code>ChooserPanel</code>.
      */
    public abstract void updateChooser();

    /**
     * Builds a new chooser panel.
     */
    protected abstract void buildChooser();

    /**
     * Returns a string containing the display name of the panel.
     * @return the name of the display panel
     */
    public abstract String getDisplayName();

 /**
     * Returns the small display icon for the panel.
     * @return the small display icon
     */
    public abstract Icon getSmallDisplayIcon();

    /**
     * Returns the large display icon for the panel.
     * @return the large display icon
     */
    public abstract Icon getLargeDisplayIcon();

```