⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 colorchooser.html

📁 jsf、swing的官方指南
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<div class="linkBHEAD"><a href="tabbedpane.html">How to Use Tabbed Panes</a></div><div class="linkBHEAD"><a href="table.html">How to Use Tables</a></div><div class="linkBHEAD"><a href="textarea.html">How to Use Text Areas</a></div><div class="linkBHEAD"><a href="textfield.html">How to Use Text Fields</a></div><div class="linkBHEAD"><a href="toolbar.html">How to Use Tool Bars</a></div><div class="linkBHEAD"><a href="tooltip.html">How to Use Tool Tips</a></div><div class="linkBHEAD"><a href="tree.html">How to Use Trees</a></div><div class="linkAHEAD"><a href="icon.html">How to Use Icons</a></div><div class="linkAHEAD"><a href="border.html">How to Use Borders</a></div><div class="linkAHEAD"><a href="problems.html">Solving Common Component Problems</a></div></div>    </div>    <div id=MainFlow class=MainFlow_indented>            <span id=BreadCrumbs>                <a href=../../index.html target=_top>Home Page</a>                &gt;                <a href=../index.html target=_top>Creating a GUI with JFC/Swing</a>                &gt;                <a href=index.html target=_top>Using Swing Components</a>            </span>            <div class=NavBit>                <a target=_top href=button.html>&laquo;&nbsp;Previous</a>&nbsp;&bull;&nbsp;<a target=_top href=../TOC.html>Trail</a>&nbsp;&bull;&nbsp;<a target=_top href=combobox.html>Next&nbsp;&raquo;</a>            </div>            <div id=PageTitle>How to Use Color Choosers</div>            <blockquote>Use the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JColorChooser.html"><code>JColorChooser</code></a> class to provide users with a palette of colors to choose from.A color chooser is a component that you can placeanywhere within your program's GUI.The <code>JColorChooser</code> API also makes it easyto bring up a <a href="dialog.html">dialog</a>(modal or not) that contains a color chooser.<p>Here's a pictureof an application that uses a color chooser to setthe text color in a banner:<p><center><IMG SRC="../../figures/uiswing/components/ColorChooserDemoMetal.png" WIDTH="447" HEIGHT="493" ALIGN="BOTTOM" ALT="A snapshot of ColorChooserDemo, which contains a standard color chooser."></center></p><font color=red>[PENDING: Please add call-outs to various parts of the color chooser:<ul><li> color chooser (everything inside and including the "Choose Text Color"     border)<li> preview panel (everything inside and including the "Preview" border)<li> chooser panel (everything inside but NOT including the tabbed pane)]</ul></font><p>You can <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/ColorChooserDemo.jnlp">run&nbsp;ColorChooserDemo</a> (it requires release 6)using <a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java<sup><font size=-2>TM</font></sup> Web Start</a>.  The source code for the program is in<a class="SourceLink" target="_blank" href="examples/ColorChooserDemo.java"><code>ColorChooserDemo.java</code></a>.<p>The color chooser consists of everything within thebox labeled <strong>Choose Text Color</strong>.This is what a standard color chooser looks likein the Java Look & Feel.It contains two parts, a tabbed pane and a preview panel.The three tabs in the tabbed paneselect <em>chooser panels</em>.The <em>preview panel</em>below the tabbed panedisplays the currently selected color.<p>Here's the code from the example that createsa <code>JColorChooser</code> instanceand adds it to a container:<blockquote><pre>public class ColorChooserDemo extends JPanel ... {    public ColorChooserDemo() {        super(new BorderLayout());        banner = new JLabel("Welcome to the Tutorial Zone!",                            JLabel.CENTER);        banner.setForeground(Color.yellow);        . . .        tcc = new JColorChooser(banner.getForeground());        . . .        add(tcc, BorderLayout.PAGE_END);    }</pre></blockquote>The <code>JColorChooser</code> constructor in the previous code snippettakes a <code>Color</code> argument,which specifies the chooser's initially selected color.If you don't specify the initial color,then the color chooser displays<code>Color.white</code>.See the <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Color.html"><code>Color</code> API documentation</a> for a list of color constants you can use.<p><a name="changelistener">A color chooser uses an instance of</a><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/colorchooser/ColorSelectionModel.html"><code>ColorSelectionModel</code></a> to contain and manage the current selection.The color selection model fires a change eventwhenever the user changes the color in the color chooser.The example program registers a change listener with thecolor selection model so that it can update thebanner at the top of the window.The following code registers and implements the change listener:<blockquote><pre>tcc.getSelectionModel().addChangeListener(this);. . .public void stateChanged(ChangeEvent e) {    Color newColor = tcc.getColor();    banner.setForeground(newColor);}</pre></blockquote>See<a href="../events/changelistener.html">How to Write a Change Listener</a>for general information about change listeners and change events.<p>A basic color chooser,like the one used in the example program,is sufficient for many programs.However,the color chooser API allows you to customize a color chooserby providing it with a preview panel of your own design,by adding your own chooser panels to it, orby removing existing chooser panels from the color chooser.Additionally, the <code>JColorChooser</code> class provides twomethods that make it easy to use a color chooser within a dialog.<p>The rest of this section discusses these topics:<ul><li> <a href="#advancedexample">Another Example: ColorChooserDemo2</a><li> <a href="#dialog">Showing a Color Chooser in a Dialog</a><li> <a href="#previewpanel">Removing or Replacing the Preview Panel</a><li> <a href="#chooserpanel">Creating a Custom Chooser Panel</a><li> <a href="#api">The Color Chooser API</a><li> <a href="#eg">Examples that Use Color Choosers</a></ul><a name="advancedexample"></blockquote><h3>Another Example: ColorChooserDemo2</h3></a><blockquote>Now let's turn our attention to<a href="examples/index.html#ColorChooserDemo2">ColorChooserDemo2</a>,a modified version of the previous demo program thatuses more of the <code>JColorChooser</code> API.You can <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/ColorChooserDemo2.jnlp">run&nbsp;ColorChooserDemo2</a> (it requires release 6)using <a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java<sup><font size=-2>TM</font></sup> Web Start</a>.  Here's a picture of ColorChooserDemo2:<p><center><IMG SRC="../../figures/uiswing/components/ColorChooserDemo2Metal.png" WIDTH="387" HEIGHT="425" ALIGN="BOTTOM" ALT="A snapshot of ColorChooserDemo, which contains a custom color chooser."></center></p><p>This program customizes the banner'stext color chooser in these ways:<ul><li> Removes the preview panel<li> Removes all of the default chooser panels<li> Adds a custom chooser panel</ul><a href="#previewpanel">Removing or Replacing the Preview Panel</a>covers the first customization.<a href="#chooserpanel">Creating a Custom Chooser Panel</a>discusses the last two.<p>This program also adds a button that brings upa color chooser in a dialog,which you can use to set the banner's background color.</blockquote><a name="dialog"><h3>Showing a Color Chooser in a Dialog</h3></a><blockquote>The <code>JColorChooser</code> class provides two class methods to make it easy to use a color chooser in adialog.ColorChooserDemo2 uses one of these methods,<code>showDialog</code>,to display the background color chooser when the userclicks the <strong>Show Color Chooser...</strong> button.Here's the single line of code from the examplethat brings up the background color chooser in a dialog:<blockquote><pre>Color newColor = JColorChooser.showDialog(                     ColorChooserDemo2.this,                     "Choose Background Color",                     banner.getBackground());</pre></blockquote>The first argument is the parent for the dialog,the second is the dialog's title, andthe third is the initially selected color.<p>The dialog disappears under three conditions:the user chooses a color and clicks the <strong>OK</strong> button,the user cancels the operation with the <strong>Cancel</strong> button,or the user dismisses the dialog with a frame control.If the user chooses a color,the <code>showDialog</code> method returns the new color.If the user cancels the operation or dismisses the window,the method returns <code>null</code>.Here's the code from the example that updatesthe banner's background coloraccording to the value returned by <code>showDialog</code>:<blockquote><pre>if (newColor != null) {    banner.setBackground(newColor);}</pre></blockquote>The dialog created by <code>showDialog</code> is modal.If you want a non-modal dialog,you can use <code>JColorChooser</code>'s<code>createDialog</code> method to create the dialog.This method also lets you specify action listeners for the<strong>OK</strong> and <strong>Cancel</strong>buttons in the dialog window.Use <code>JDialog</code>'s <code>show</code> methodto display the dialog created by this method.For an example that uses this method, see<a href="table.html#editor">Specifying Other Editors</a>in the<a href="table.html">How to Use Tables</a> section.</blockquote><a name="previewpanel"><h3>Removing or Replacing the Preview Panel</h3></a><blockquote>By default, the color chooser displays a preview panel.ColorChooserDemo2removes the text color chooser'spreview panel with this line of code:<blockquote><pre>tcc.setPreviewPanel(new JPanel());</pre></blockquote>This effectively removes the preview panel becausea plain <code>JPanel</code> has no size and no default view.To set the preview panel back to the default,use <code>null</code> as the argument to <code>setPreviewPanel</code>.<p>To provide a custom preview panel,you also use <code>setPreviewPanel</code>.The component you pass into the method shouldinherit from <code>JComponent</code>,specify a reasonable size,and provide a customized view of the current color.To get notified when the user changes the colorin the color chooser,the preview panel must registeras a change listener on the color chooser's color selection modelas described <a href="#changelistener">previously</a>.</blockquote><a name="chooserpanel"><h3>Creating a Custom Chooser Panel</h3></a><blockquote>The default color chooser provides three chooser panels:<ul><li> Swatches &#151; for choosing a color from a collection of swatches.<li> HSB &#151; for choosing a color using the Hue-Saturation-Brightness      color model.<li> RGB &#151; for choosing a color using the Red-Green-Blue     color model.</ul>You can extend the default color chooserby adding chooser panels of your own design with<code>addChooserPanel</code>,or you can limit it by removing chooser panelswith <code>removeChooserPanel</code>.<p>If you want to remove all of the default chooser panelsand add one or more of your own, you can do this with a singlecall to <code>setChooserPanels</code>.ColorChooserDemo2uses this method to replace the default chooser panelswith an instance of<a class="SourceLink" target="_blank" href="examples/CrayonPanel.java"><code>CrayonPanel</code></a>,a custom chooser panel.Here's the call to <code>setChooserPanels</code> fromthat example:<blockquote><pre>//Override the chooser panels with our own.AbstractColorChooserPanel panels[] = { new CrayonPanel() };tcc.setChooserPanels(panels);</pre></blockquote>The code is straighforward:it creates an array containing the <code>CrayonPanel</code>.Next the code calls <code>setChooserPanels</code> to set the contents of the array as the color chooser'schooser panels.<p><code>CrayonPanel</code> is a subclass of<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/colorchooser/AbstractColorChooserPanel.html"><code>AbstractColorChooserPanel</code></a> and overrides the five abstract methods defined in its superclass:<dl><dt> <strong><code>void buildChooser()</code></strong><dd> Creates the GUI that comprises the chooser panel.     The example creates four toggle buttons     &#151; one for each crayon &#151;     and adds them to the chooser panel.<dt> <strong><code>void updateChooser()</code></strong><dd> This method is called whenever the chooser panel is displayed.     The example's implementation of this method selects     the toggle button that represents the currently     selected color.<blockquote><pre>public void updateChooser() {    Color color = getColorFromModel();    if (Color.red.equals(color)) {        redCrayon.setSelected(true);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -