📄 jcomponent.html
字号:
<br><b>Lesson:</b> Using Swing Components </div> <div id=LeftBar class=LeftBar_shown> <div id=Contents> <div class="linkLESSON"><a href="index.html">Using Swing Components</a></div><div class="linkAHEAD"><a href="components.html">A Visual Index to the Swing Components</a></div><div class="linkAHEAD"><a href="html.html">Using HTML in Swing Components</a></div><div class="linkAHEAD"><a href="toplevel.html">Using Top-Level Containers</a></div><div class="linkAHEAD"><a href="model.html">Using Models</a></div><div class="nolinkAHEAD">The JComponent Class</div><div class="linkAHEAD"><a href="text.html">Using Text Components</a></div><div class="linkBHEAD"><a href="generaltext.html">Text Component Features</a></div><div class="linkBHEAD"><a href="textapi.html">The Text Component API</a></div><div class="linkAHEAD"><a href="componentlist.html">How to Use Various Components</a></div><div class="linkBHEAD"><a href="applet.html">How to Make Applets</a></div><div class="linkBHEAD"><a href="button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a></div><div class="linkBHEAD"><a href="colorchooser.html">How to Use Color Choosers</a></div><div class="linkBHEAD"><a href="combobox.html">How to Use Combo Boxes</a></div><div class="linkBHEAD"><a href="dialog.html">How to Make Dialogs</a></div><div class="linkBHEAD"><a href="editorpane.html">How to Use Editor Panes and Text Panes</a></div><div class="linkBHEAD"><a href="filechooser.html">How to Use File Choosers</a></div><div class="linkBHEAD"><a href="formattedtextfield.html">How to Use Formatted Text Fields</a></div><div class="linkBHEAD"><a href="frame.html">How to Make Frames (Main Windows)</a></div><div class="linkBHEAD"><a href="internalframe.html">How to Use Internal Frames</a></div><div class="linkBHEAD"><a href="label.html">How to Use Labels</a></div><div class="linkBHEAD"><a href="layeredpane.html">How to Use Layered Panes</a></div><div class="linkBHEAD"><a href="list.html">How to Use Lists</a></div><div class="linkBHEAD"><a href="menu.html">How to Use Menus</a></div><div class="linkBHEAD"><a href="panel.html">How to Use Panels</a></div><div class="linkBHEAD"><a href="passwordfield.html">How to Use Password Fields</a></div><div class="linkBHEAD"><a href="progress.html">How to Use Progress Bars</a></div><div class="linkBHEAD"><a href="rootpane.html">How to Use Root Panes</a></div><div class="linkBHEAD"><a href="scrollpane.html">How to Use Scroll Panes</a></div><div class="linkBHEAD"><a href="separator.html">How to Use Separators</a></div><div class="linkBHEAD"><a href="slider.html">How to Use Sliders</a></div><div class="linkBHEAD"><a href="spinner.html">How to Use Spinners</a></div><div class="linkBHEAD"><a href="splitpane.html">How to Use Split Panes</a></div><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> > <a href=../index.html target=_top>Creating a GUI with JFC/Swing</a> > <a href=index.html target=_top>Using Swing Components</a> </span> <div class=NavBit> <a target=_top href=model.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=text.html>Next »</a> </div> <div id=PageTitle>The JComponent Class</div> <blockquote>With the exception of top-level containers,all Swing components whose names begin with "J"descend from the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html"><code>JComponent</code></a> class.For example,<code>JPanel</code>,<code>JScrollPane</code>,<code>JButton</code>, and<code>JTable</code>all inherit from <code>JComponent</code>.However, <code>JFrame</code> and <code>JDialog</code> don'tbecause they implement top-level containers.<p>The <code>JComponent</code> classextends the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html"><code>Container</code></a> class,which itself extends<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Component.html"><code>Component</code></a>.The <code>Component</code> class includes everything fromproviding layout hintsto supporting painting and events.The <code>Container</code> class has supportfor adding components to the container andlaying them out.This section's<a href="#api">API tables</a> summarize the most often usedmethods of <code>Component</code> and <code>Container</code>,as well as of <code>JComponent</code>.<a name="jcomponent"></blockquote><h3>JComponent Features</h3></a><blockquote>The <code>JComponent</code> class providesthe following functionality to its descendants:<ul><li> <a href="#tooltips">Tool tips</a><li> <a href="#borders">Painting and borders</a><li> <a href="#plug">Application-wide pluggable look and feel</a><li> <a href="#properties">Custom properties</a><li> <a href="#layout">Support for layout</a><li> <a href="#access">Support for accessibility</a><li> <a href="#dnd">Support for drag and drop</a><li> <a href="#buffer">Double buffering</a><li> <a href="#keyboardAction">Key bindings</a></ul><dl><dt> <a name="tooltips"><strong>Tool tips</strong></a><dd> By specifying a string with the <code>setToolTipText</code> method, you can provide help to users of a component. When the cursor pauses over the component, the specified string is displayed in a small window that appears near the component. See <a href="tooltip.html">How to Use Tool Tips</a> for more information.<p><dt> <a name="borders"><strong>Painting and borders</strong></a><dd> The <code>setBorder</code> method allows you to specify the border that a component displays around its edges. To paint the inside of a component, override the <code>paintComponent</code> method. See<a class="TutorialLink" target="_top" href="../components/border.html">How to Use Borders</a> and<a class="TutorialLink" target="_top" href="../painting/index.html">Performing Custom Painting</a> for details.</dd><p><dt> <a name="plug"><strong>Application-wide pluggable look and feel</strong></a><dd> Behind the scenes, each <code>JComponent</code> object has a corresponding <code>ComponentUI</code> object that performs all the drawing, event handling, size determination, and so on for that <code>JComponent</code>. Exactly which <code>ComponentUI</code> object is used depends on the current look and feel, which you can set using the <code>UIManager.setLookAndFeel</code> method. See<a class="TutorialLink" target="_top" href="../lookandfeel/plaf.html">How to Set the Look and Feel</a> for details.<p><dt> <a name="properties"><strong>Custom properties</strong></a><dd> You can associate one or more properties (name/object pairs) with any <code>JComponent</code>. For example, a layout manager might use properties to associate a constraints object with each <code>JComponent</code> it manages. You put and get properties using the <code>putClientProperty</code> and <code>getClientProperty</code> methods. For general information about properties, see<a class="TutorialLink" target="_top" href="../../essential/environment/properties.html">Properties</a>. <p><dt> <a name="layout"><strong>Support for layout</strong></a><dd> Although the <code>Component</code> class provides layout hint methods such as <code>getPreferredSize</code> and <code>getAlignmentX</code>, it doesn't provide any way to set these layout hints, short of creating a subclass and overriding the methods. To give you another way to set layout hints, the <code>JComponent</code> class adds setter methods — <code>setPreferredSize</code>, <code>setMinimumSize</code>, <code>setMaximumSize</code>, <code>setAlignmentX</code>, and <code>setAlignmentY</code>. See<a class="TutorialLink" target="_top" href="../layout/index.html">Laying Out Components Within a Container</a> for more information.<p><dt> <a name="access"><strong>Support for accessibility</strong></a><dd> The <code>JComponent</code> class provides API and basic functionality to help assistive technologies such as screen readers get information from Swing components, For more information about accessibility, see<a class="TutorialLink" target="_top" href="../misc/access.html">How to Support Assistive Technologies</a>.</dd><p><dt> <a name="dnd"><strong>Support for drag and drop</strong></a><dd>The <code>JComponent</code> class provides APIto set a component's transfer handler,which is the basis for Swing'sdrag and drop support.See<a class="TutorialLink" target="_top" href="../dnd/intro.html">Introduction to Drag and Drop and Data Transfer</a> for details.</dd></dt><p><dt> <a name="buffer"><strong>Double buffering</strong></a><dd> Double buffering smooths on-screen painting. For details, see<a class="TutorialLink" target="_top" href="../painting/index.html">Performing Custom Painting</a>.</dd><p><dt> <a name="keyboardAction"><strong>Key bindings</strong></a><dd>This feature makes components reactwhen the user presses a key on the keyboard.For example,in many look and feels when a button has the focus,typing the Space key is equivalent toa mouse click on the button.The look and feel automatically sets upthe bindings between pressing and releasing the Space keyand the resulting effects on the button.For more information about key bindings, see<a class="TutorialLink" target="_top" href="../misc/keybinding.html">How to Use Key Bindings</a>.</dl><p></blockquote><a name="api"><h3>The JComponent API</h3></a><blockquote>The <code>JComponent</code> class provides many new methodsand inherits many methodsfrom <code>Component</code> and <code>Container</code>.The following tables summarize the methodswe use the most.<UL> <LI><A HREF="#complookapi">Customizing Component Appearance</A> <LI><A HREF="#stateapi">Setting and Getting Component State</A> <LI><A HREF="#eventapi">Handling Events</A> <LI><A HREF="#custompaintingapi">Painting Components</A> <LI><A HREF="#containmentapi">Dealing with the Containment Hierarchy</A> <LI><A HREF="#layoutapi">Laying Out Components</A> <LI><A HREF="#sizeapi">Getting Size and Position Information</A> <LI><A HREF="#absoluteapi">Specifying Absolute Size and Position</A></UL><p><TABLE BORDER="1"><caption><A NAME="complookapi"><b>Customizing Component Appearance</b></A></caption><TR> <th> Method </th><th> Purpose</th> <TR> <TD><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#setBorder(javax.swing.border.Border)">void setBorder(Border)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#getBorder()">Border getBorder()</a> </TD> <TD> Set or get the border of the component. See<a class="TutorialLink" target="_top" href="../components/border.html">How to Use Borders</a> for details.</TD> </TR> <TR> <TD><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#setForegroundColor(java.awt.Color)">void setForeground(Color)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#setBackground(java.awt.Color)">void setBackground(Color)</a> <td> Set the foreground or background color for the component.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -