The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Every 每个JComponent
can have one or more borders. JComponent
都可以有一个或多个边界。Borders are incredibly useful objects that, while not themselves components, know how to draw the edges of Swing components. 边界是非常有用的对象,虽然它本身不是组件,但知道如何绘制Swing组件的边。Borders are useful not only for drawing lines and fancy edges, but also for providing titles and empty space around components.边框不仅可用于绘制线条和花哨的边缘,还可用于在组件周围提供标题和空白空间。
Our examples set borders on 示例设置了JPanel
s, JLabel
s, and custom subclasses of JComponent
. JPanel
、JLabel
和JComponent
的自定义子类的边界。Although technically you can set the border on any object that inherits from 虽然从技术上讲,您可以在继承自JComponent
, the look and feel implementation of many standard Swing components doesn't work well with user-set borders. JComponent
的任何对象上设置边界,但许多标准Swing组件的外观实现与用户设置边界并不兼容。In general, when you want to set a border on a standard Swing component other than 通常,当您想在除JPanel
or JLabel
, we recommend that you put the component in a JPanel
and set the border on the JPanel
.JPanel
或JLabel
之外的标准Swing组件上设置边框时,我们建议您将组件放在JPanel
中,并在JPannel
上设置边框。
To put a border around a 要在JComponent
, you use its setBorder
method. JComponent
周围放置边框,可以使用其setBorder
方法。You can use the 您可以使用BorderFactory
class to create most of the borders that Swing provides. BorderFactory
类创建Swing提供的大多数边界。If you need a reference to a border say, because you want to use it in multiple components you can save it in a variable of type 如果需要对边框的引用例如,因为您想在多个组件中使用它可以将其保存在Border
. Border
类型的变量中。Here is an example of code that creates a bordered container:以下是创建带边框容器的代码示例:
JPanel pane = new JPanel(); pane.setBorder(BorderFactory.createLineBorder(Color.black));
Here's a picture of the container, which contains a label component. 这是容器的图片,其中包含标签组件。The black line drawn by the border marks the edge of the container.由边框绘制的黑线标记容器的边缘。
The rest of this page discusses the following topics:本页其余部分讨论以下主题:
The following pictures show an application called 下图显示了一个名为BorderDemo
that displays the borders Swing provides. BorderDemo
的应用程序,它显示了Swing提供的边界。We show the code for creating these borders a little later, in Using the Borders Provided by Swing.稍后,我们在使用Swing提供的边框中展示了创建这些边界的代码。
Click the Launch button to run the BorderDemo example using Java™ Web Start (download JDK 7 or later). 单击Launch按钮,使用Java™Web启动运行BorderDemo示例(下载JDK 7或更高版本)。Alternatively, to compile and run the example yourself, consult the example index.或者,要自己编译和运行示例,请参考示例索引。
The next picture shows some matte borders. 下图显示了一些哑光边框。When creating a matte border, you specify how many pixels it occupies at the top, left, bottom, and right of a component. 创建蒙版边框时,可以指定它在组件的顶部、左侧、底部和右侧占据的像素数。You then specify either a color or an icon for the matte border to draw. 然后指定要绘制的无光边框的颜色或图标。You need to be careful when choosing the icon and determining your component's size; otherwise, the icon might get chopped off or have mismatch at the component's corners.在选择图标和确定组件大小时,您需要小心;否则,该图标可能会被切掉或在组件的角处不匹配。
The next picture shows titled borders. Using a titled border, you can convert any border into one 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 feel uses an etched border. 例如,Java外观中的默认标题边框使用灰色线,而Windows外观中的缺省标题边框使用蚀刻边框。By default, the title straddles the upper left of the border, as shown at the top of the following figure.默认情况下,标题横跨边框的左上角,如下图顶部所示。
The next picture shows compound borders. 下图显示复合边框。With compound borders, you can combine any two borders, which can themselves be compound borders.使用复合边界,可以组合任意两个边界,这两个边界本身可以是复合边界。
The code that follows shows how to create and set the borders you saw in the preceding figures. 下面的代码显示了如何创建和设置您在前面的图中看到的边界。You can find the program's code in 您可以在BorderDemo.java
.BorderDemo.java
中找到该程序的代码。
//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 borders jComp1.setBorder(blackline); jComp2.setBorder(raisedbevel); jComp3.setBorder(loweredbevel); jComp4.setBorder(empty); //Matte borders ImageIcon icon = createImageIcon("images/wavy.gif", "wavy-line border icon"); //20x22 jComp5.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 borders TitledBorder 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 borders Border 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);
As you probably noticed, the code uses the 正如您可能注意到的,代码使用BorderFactory
class to create each border. BorderFactory
类创建每个边框。The BorderFactory
class, which is in the javax.swing
package, returns objects that implement the Border
interface.javax.swing
包中的BorderFactory
类返回实现Border
接口的对象。
The Border
interface, as well as its Swing-provided implementations, is in the javax.swing.border
package. Border
接口及其Swing提供的实现位于javax.swing.border
包中。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 您通常不需要直接使用border包中的任何内容,除非指定特定于特定border类的常量或引用Border
type.Border
类型。
If 如果BorderFactory
doesn't offer you enough control over a border's form, then you might need to directly use the API in the border package — or even define your own border. BorderFactory
无法为您提供对边框表单的足够控制,那么您可能需要直接使用边框包中的API—甚至定义自己的边界。In addition to containing the 除了包含Border接口外,Border包还包含实现您已经看到的边界的类:Border
interface, the border package contains the classes that implement the borders you've already seen: LineBorder
, EtchedBorder
, BevelBorder
, EmptyBorder
, MatteBorder
, TitledBorder
, and CompoundBorder
. LineBorder
、EtchedBorder
、BevelBorder
、EmptyBorder
、MatteBorder
、TitledBorder
和CompoundBorder
。The border package also contains a class named border包还包含一个名为SoftBevelBorder
, which produces a result similar to BevelBorder
, but with softer edges.SoftBevelBorder
的类,该类生成的结果类似于BevelBoorder
,但边缘较软。
If none of the Swing borders is suitable, you can implement your own border. 如果没有合适的Swing边界,您可以实现自己的边界。Generally, you do this by creating a subclass of the 通常,您可以通过创建AbstractBorder
class. AbstractBorder
类的子类来实现这一点。In your subclass, you must implement at least one constructor and the following two methods:在子类中,必须实现至少一个构造函数和以下两个方法:
paintBorder
JComponent
executes to draw the border.JComponent
为绘制边框而执行的绘图代码。getBorderInsets
If a custom border has insets (and they typically have insets) you need to override both 如果自定义边框具有插入(并且它们通常具有插入),则需要重写AbstractBorder.getBorderInsets(Component c)
and AbstractBorder.getBorderInsets(Component c, Insets insets)
to provide the correct insets.AbstractBorder.getBorderInsets(Component c)
和AbstractBorder.getBorderInsets(Component c, Insets insets)
以提供正确的插入。
For examples of implementing borders, see the source code for the classes in the 有关实现边界的示例,请参阅javax.swing.border
package.javax.swing.border
包中类的源代码。
The following tables list the commonly used border methods. 下表列出了常用的边界方法。The API for using borders falls into two categories:使用边界的API分为两类:
Border createLineBorder(Color) Border createLineBorder(Color, int) |
java.awt.Color object that specifies the color of the line. java.awt.Color 对象。 |
Border createEtchedBorder() Border createEtchedBorder(Color, Color) Border createEtchedBorder(int) Border createEtchedBorder(int, Color, Color) |
Color arguments specify the highlight and shadow colors to be used. Color 参数指定要使用的高光和阴影颜色。int arguments allow the border methods to be specified as either EtchedBorder.RAISED or EtchedBorder.LOWERED . int 参数的方法允许将border方法指定为EtchedBorder.RAISED 或EtchedBorder.LOWERED 。int arguments create a lowered etched border.int 参数的方法会创建一个较低的蚀刻边界。 |
Border createLoweredBevelBorder() |
|
Border createRaisedBevelBorder() |
|
Border createBevelBorder(int, Color, Color) Border createBevelBorder(int, Color, Color, Color, Color) |
BevelBorder.RAISED or BevelBorder.LOWERED . BevelBorder.RAISED 或BevelBoorder.LOWERED 。 |
Border createEmptyBorder() Border createEmptyBorder(int, int, int, int) |
|
MatteBorder createMatteBorder(int, int, int, int, Color) MatteBorder createMatteBorder(int, int, int, int, Icon) |
|
TitledBorder createTitledBorder(String) TitledBorder createTitledBorder(Border) TitledBorder createTitledBorder(Border, String) TitledBorder createTitledBorder(Border, String, int, int) TitledBorder createTitledBorder(Border, String, int, int, Font) TitledBorder createTitledBorder(Border, String, int, int, Font, Color) |
|
CompoundBorder createCompoundBorder(Border, Border) |
void setBorder(Border) Border getBorder() |
JComponent .JComponent 的边界。 |
void setBorderPainted(boolean) boolean isBorderPainted() (in AbstractButton , JMenuBar , JPopupMenu , JProgressBar , and JToolBar ) |
Many examples in this lesson use borders. 本课中的许多示例使用边界。The following table lists a few interesting cases.下表列出了一些有趣的案例。
BorderDemo |
BorderFactory can create. BorderFactory 可以创建的每种类型的边框的示例。 | |
BoxAlignmentDemo |
||
BoxLayoutDemo |
||
ComboBoxDemo2 |