I l@ve RuBoard |
![]() ![]() |
13.1 Introducing BordersFigure 13-1 shows the standard borders that Swing provides. There are eight border styles: bevel, soft bevel, empty, etched, line, matte, titled, and compound. The MatteBorder gives you two borders in one: the border area can be filled with either a solid color or an icon. (This figure shows only the icon; you can see a better example of both in Figure 13-11.) Figure 13-1. Borders in Swing![]() You can place a border around any Swing component that extends JComponent. The JComponent class contains a border property that is inherited by all Swing components. (Top-level components that don't inherit from JComponent, like JFrame and JDialog, can't have borders.) By default, the border property is null (no border), but you can access and modify it. Once you've set a component's border, the component paints itself using that border from that point on, and the insets of the border replace the component's default insets. Here's how to set a component's border: JLabel label = new JLabel("A Border"); mylabel.setBorder(new BevelBorder(BevelBorder.LOWERED)); Borders are grouped into a separate package within the Swing hierarchy, javax.swing.border. Figure 13-2 shows the classes within this package. The borders included with Swing directly or indirectly extend the AbstractBorder class, which in turn implements the fundamental Border interface and provides a number of helpful housekeeping methods that any implementation can use. (This is an example of the useful skeletal implementation pattern for working with interfaces described in Joshua Bloch's outstanding Effective Java Programming Language Guide [Addison-Wesley].) You'll almost certainly want to use the same technique if you develop your own border. Figure 13-2. Border class diagram![]() Borders can be combined to form more elaborate compound borders. The lower-right corner of Figure 13-1 shows an example of a compound border. We combined an etched border (on the inside) with a raised bevel border (on the outside). Swing allows you to mix any number of border styles into a single border object. This gives Swing borders a useful compositional feature not often found in other graphical toolkits. 13.1.1 The Border InterfaceThe Border interface contains three methods. 13.1.1.1 Methods
Figure 13-3. A border is allowed to paint itself only within the insets it declares![]()
|
I l@ve RuBoard |
![]() ![]() |