📄 border.html
字号:
<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="nolinkAHEAD">How to Use Borders</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=icon.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=problems.html>Next »</a> </div> <div id=PageTitle>How to Use Borders</div> <blockquote>Every <code>JComponent</code>can have one or more borders.Borders are incredibly useful objectsthat, while not themselves components,know how to drawthe edges of Swing components.Borders are useful not only for drawing lines and fancy edges,but also for providing titles and empty space around components.<blockquote><hr><strong>Note:</strong> Our examples set borders on <code>JPanel</code>s,<code>JLabel</code>s, and custom subclasses of<code>JComponent</code>. Although technically you can set the borderon any object that inherits from <code>JComponent</code>, the look and feelimplementation of many standard Swing components doesn't work well withuser-set borders. In general, when you want to set a border on astandard Swing component other than <code>JPanel</code> or <code>JLabel</code>,we recommend that you put the component in a <code>JPanel</code> and setthe border on the <code>JPanel</code>.<p>The GTK+ look and feel handles borders differentlythan other look and feels. Please refer to the<a class="OutsideLink" target="_blank" href="http://java.sun.com/j2se/1.4.2/docs/guide/swing/1.4/Post1.4.html#gtk">1.4.2 release notes</a> for details.<hr></blockquote><p>To put a border around a <code>JComponent</code>,you use its<code>setBorder</code> method.You can use the <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/BorderFactory.html"><code>BorderFactory</code></a> class to create most of the borders that Swing provides.If you need a reference to a border — say, because youwant to use it in multiple components — you can saveit in a variable of type<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/border/Border.html"><code>Border</code></a>. Here is an example of code that creates a bordered container:<p><blockquote><pre>JPanel pane = new JPanel();pane.setBorder(BorderFactory.createLineBorder(Color.black));</pre></blockquote><p>Here's a picture of the container,which contains a label component.The black line drawn by the bordermarks the edge of the container.<p><center><IMG SRC="../../figures/uiswing/components/BorderDemo-line.png" WIDTH="300" HEIGHT="38" ALIGN="BOTTOM" ALT="A line border"></center></p>The rest of this page discusses the following topics:<ul><li> <a href="#demo">The BorderDemo Example</a><li> <a href="#code">Using the Borders Provided by Swing</a><li> <a href="#custom">Creating Custom Borders</a><li> <a href="#api">The Border API</a><li> <a href="#eg">Examples of Using Borders</a></ul></blockquote><h3><a name="demo">The BorderDemo Example</a></h3><blockquote>The following pictures show an applicationcalled <code>BorderDemo</code> thatdisplays the borders Swing provides.We show the code for creating these bordersa little later,in <a href="#code">Using the Borders Provided by Swing</a>.<p>You can <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/BorderDemo.jnlp">run BorderDemo</a> using<a href=http://java.sun.com/products/javawebstart>Java<sup><font size=-2>TM</font></sup> Web Start</a>.Or, to compile and run the example yourself, consult the<a href="examples/index.html#BorderDemo">example index</a>.<p><center><IMG SRC="../../figures/uiswing/components/BorderDemo1.png" WIDTH="328" HEIGHT="336" ALIGN="BOTTOM" ALT="BorderDemo: Simple Borders"></center></p>The next picture shows some matte borders.When creating a matte border,you specify how many pixels it occupiesat the top, left, bottom, and right of a component.You then specify either a color or an iconfor the matte border to draw.You need to be careful when choosing the iconand determining your component's size;otherwise, the icon mightget chopped off or have mismatch at the component's corners.<p><center><IMG SRC="../../figures/uiswing/components/BorderDemo2.png" WIDTH="328" HEIGHT="336" ALIGN="BOTTOM" ALT="BorderDemo: Matte Borders"></center></p>The next picture shows titled borders.Using a titled border,you can convert any border intoone that displays a text description.If you don't specify a border,a look-and-feel-specific border is used.For example, the default titled border in the Java look and feel uses a gray line,and the default titled border in the Windows look and feeluses an etched border.By default, the title straddles the upper leftof the border,as shown at the top of the following figure.<p><center><IMG SRC="../../figures/uiswing/components/BorderDemo3.png" WIDTH="328" HEIGHT="336" ALIGN="BOTTOM" ALT="BorderDemo: Titled Borders"></center></p>The next picture shows compound borders.With compound borders, you can combine any two borders,which can themselves be compound borders.<p><center><IMG SRC="../../figures/uiswing/components/BorderDemo4.png" WIDTH="328" HEIGHT="336" ALIGN="BOTTOM" ALT="BorderDemo: Compound Borders"></center></p></blockquote><h3><a name="code">Using the Borders Provided by Swing</a></h3><blockquote>The code that follows shows how to create and set the borders yousaw in the preceding figures. You can find the program's code in<a class="SourceLink" target="_blank" href="examples/BorderDemo.java"><code>BorderDemo.java</code></a>.<p><blockquote><pre>//Keep references to the next few borders,//for use in titles and compound borders.Border blackline, raisedetched, loweredetched, raisedbevel, loweredbevel, empty;blackline = BorderFactory.createLineBorder(Color.black);raisedetched = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);raisedbevel = BorderFactory.createRaisedBevelBorder();loweredbevel = BorderFactory.createLoweredBevelBorder();empty = BorderFactory.createEmptyBorder();//Simple bordersjComp1.setBorder(blackline);jComp2.setBorder(raisedbevel);jComp3.setBorder(loweredbevel);jComp4.setBorder(empty);//Matte bordersImageIcon icon = createImageIcon("images/wavy.gif", "wavy-line border icon"); //20x22jComp5.setBorder(BorderFactory.createMatteBorder( -1, -1, -1, -1, icon));jComp6.setBorder(BorderFactory.createMatteBorder( 1, 5, 1, 1, Color.red));jComp7.setBorder(BorderFactory.createMatteBorder( 0, 20, 0, 0, icon));//Titled bordersTitledBorder title;title = BorderFactory.createTitledBorder("title");jComp8.setBorder(title);title = BorderFactory.createTitledBorder( blackline, "title");title.setTitleJustification(TitledBorder.CENTER);jComp9.setBorder(title);title = BorderFactory.createTitledBorder( loweredetched, "title");title.setTitleJustification(TitledBorder.RIGHT);jComp10.setBorder(title);title = BorderFactory.createTitledBorder( loweredbevel, "title");title.setTitlePosition(TitledBorder.ABOVE_TOP);jComp11.setBorder(title);title = BorderFactory.createTitledBorder( empty, "title");title.setTitlePosition(TitledBorder.BOTTOM);jComp12.setBorder(title);//Compound bordersBorder compound;Border redline = BorderFactory.createLineBorder(Color.red);//This creates a nice frame.compound = BorderFactory.createCompoundBorder( raisedbevel, loweredbevel);jComp13.setBorder(compound);//Add a red outline to the frame.compound = BorderFactory.createCompoundBorder( redline, compound);jComp14.setBorder(compound);//Add a title to the red-outlined frame.compound = BorderFactory.createTitledBorder( compound, "title", TitledBorder.CENTER, TitledBorder.BELOW_BOTTOM);jComp15.setBorder(compound);</pre></blockquote>As you probably noticed,the code uses the <code>BorderFactory</code> classto create each border.The <code>BorderFactory</code> class,which is in the <code>javax.swing</code> package,returns objects that implement the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/border/Border.html"><code>Border</code></a> interface.<p>The <code>Border</code> interface,as well as its Swing-provided implementations,is in the <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/border/package-summary.html"><code>javax.swing.border</code></a> package.You often don't need to directly use anything in the border package,except when specifying constants that are specific to a particular border class or when referring to the <code>Border</code> type.</blockquote><h3><a name="custom">Creating Custom Borders</a></h3><blockquote>If <code>BorderFactory</code> doesn't offer you enough controlover a border's form,then you might need to directly use the API in the border package —or even define your own border.In addition to containing the <code>Border</code> interface,the border packagecontains the classes that implement theborders you've already seen:<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/border/LineBorder.html"><code>LineBorder</code></a>,<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/border/EtchedBorder.html"><code>EtchedBorder</code></a>,<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/border/BevelBorder.html"><code>BevelBorder</code></a>,<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/border/EmptyBorder.html"><code>EmptyBorder</code></a>, <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/border/MatteBorder.html"><code>MatteBorder</code></a>, <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/border/TitledBorder.html"><code>TitledBorder</code></a>, and<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/border/CompoundBorder.html"><code>CompoundBorder</code></a>.The border package also contains a class named <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/border/SoftBevelBorder.html"><code>SoftBevelBorder</code></a>, which produces a result similar to <code>BevelBorder</code>,but with softer edges.<p>If none of the Swing bordersis suitable, you can implement your own border.Generally, you do this by creating a subclass
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -