Color the preceding code yields the border figure figure
Java Swing – O’Reilly
13.3 The CompoundBorder Class
13.3.1 Properties
Table 13.14, CompoundBorder Properties | ||||||
---|---|---|---|---|---|---|
|
|
|||||
|
||||||
|
public CompoundBorder(Border outsideBorder, Border insideBorder)
Creates a compound border object with the specified inside and outside borders.
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) Forces the border to paint itself with the graphics context of the component.
13.3.4 The BorderFactory Class
The BorderFactory class (which is in the javax.swing package) allows the
user to call various static methods to create borders. Instead of
declaring new instances for each border, the factory class attempts to
reuse previously defined (cached) borders, thus saving memory. It's a
good idea to get in the habit of using this factory if you create a lot
of borders in your application. Currently, only a few borders are
actually cached. But in future releases, additional border caching may
be added to this class.
public static Border createEmptyBorder(int top, int left, int bottom, int right) Creates an EmptyBorder with the specified size.
public static Border createEtchedBorder()
Creates a default EtchedBorder. This method returns a cached border
rather than creating a new one.
public static Border createLoweredBevelBorder()
Creates a lowered BevelBorder. This method returns a cached border
rather than creating a new one.
public static Border createRaisedBevelBorder()
Creates a raised BevelBorder. This method returns a cached border rather
than creating a new one.
public static TitledBorder createTitledBorder(Border border) Creates a TitledBorder from the Border.
public static TitledBorder createTitledBorder(Border border,
String title)
Creates a TitledBorder from the Border. The border's title will be the
title string passed in, positioned at the upper left of the border.
Creates a TitledBorder from the Border passed in. The border's title, justification, and position, as well as the font, are all passed in.
public static TitledBorder createTitledBorder(Border border, String title, int titleJustification, int titlePosition, Font titleFont, Color titleColor)
Creating your own border is simple when you extend the AbstractBorder class. You need to define three things: whether the border is opaque, what its insets are, and how to draw the border. To accomplish this, you must implement paintBorder(), both isBorderOpaque() methods, and getBorderInsets(). The hard part of coming up with your own border is doing something creative with the Graphics primitives in the paintBorder() method. A reminder: make sure that you do not paint inside the insets region that you define for yourself. Otherwise, you could be painting over the component you intend to border.
Let's take a look at a simple border:
// Paint a tall wall around the component
for (int i = 0; i < sinkLevel; i++) {
g.drawRoundRect(x+i, y+i, w-i-1, h-i-1, sinkLevel-i, sinkLevel); g.drawRoundRect(x+i, y+i, w-i-1, h-i-1, sinkLevel, sinkLevel-i); g.drawRoundRect(x+i, y, w-i-1, h-1, sinkLevel-i, sinkLevel);
- 349 -
} |
|
---|
Here is an excerpt from the source that you can use to surround a slider with this border; Figure 13.15 shows the result.
Figure 13.15. A custom curved border
This chapter discusses Swing menus and toolbars. Menus are by far the
larger and more extensible of the two, so they encompass most of the
chapter. Swing offers the programmer a fully
reimplemented menu system from AWT 1.1. This re-engineering allows you
to use the Swing menus in a variety of eye-catching ways, and provides
much more freedom to lay out menu components.
Toolbars are a new addition to Swing. Toolbars allow the programmer to group buttons, combo boxes, and other components together in repositionable panels; these tools can assist the user in performing many common tasks. You can add any component to a Swing toolbar, even non-Swing