Java Reference
In-Depth Information
Example 14−1: MultiLineLabel.java (continued)
line_widths = new int[num_lines];
for(int i = 0; i < num_lines; i++) lines[i] = t.nextToken();
}
/**
* This internal method figures out how the font is, and how wide each
* line of the label is, and how wide the widest line is.
**/
protected synchronized void measure() {
FontMetrics fm = this.getToolkit().getFontMetrics(this.getFont());
line_height = fm.getHeight();
line_ascent = fm.getAscent();
max_width = 0;
for(int i = 0; i < num_lines; i++) {
line_widths[i] = fm.stringWidth(lines[i]);
if (line_widths[i] > max_width) max_width = line_widths[i];
}
measured = true;
}
}
The Alignment Class
MultiLineLabel uses an auxiliary class named Alignment to define three alignment
constants. The definition of this class is shown in Example 14-2. The class defines
three constants that hold three instances of itself and declares its constructor pri-
vate , so that no other instances can be created. In this way, Alignment effectively
creates an enumerated type. This is a useful technique that is not at all specific to
JavaBeans.
Example 14−2: Alignment.java
package com.davidflanagan.examples.beans;
/** This class defines an enumerated type with three values */
public class Alignment {
/** This private constructor prevents anyone from instantiating us */
private Alignment() {};
// The following three constants are the only instances of this class
public static final Alignment LEFT = new Alignment();
public static final Alignment CENTER = new Alignment();
public static final Alignment RIGHT = new Alignment();
}
Packaging a Bean
To prepare a bean for use in a beanbox, you must package it in a JAR file, along
with any other classes or resource files it requires. (JAR files are “Java archives”;
you can read about the jar tool in Java in a Nutshell .) Because a single bean can
have many auxiliary files, and because a JAR file can contain multiple beans, the
manifest of the JAR file must define which JAR file entries are beans. You create a
JAR file with the c option to the jar command. When you use the m option in con-
junction with c , it tells jar to read a partial manifest file that you specify. jar uses
the information in your partially specified manifest file when creating the complete
Search WWH ::




Custom Search