| |
"Working with Swing"
Vol. 5, Issue 10, p. 108
Listing 1
public void createSplashScreen()
{
// Create a JLabel for the title. Ensure that the text is
// centered within the JLabel and that the JLabel is cen-
// tered within the window.
JLabel _title = new JLabel("SplashScreen Test v3.1", JLa-
bel.CENTER);
_title.setAlignmentX(Component.CENTER_ALIGNMENT);
// Set the color of the text in the title. The background
// will be that of the window itself.
_title.setForeground(Color.green);
// ditto for the progress indicator
progressLabel = new JLabel("Connecting to server...", JLa-
bel.CENTER);
progressLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
progressLabel.setForeground(Color.green);
// Create the SplashScreen
splashScreen = new SplashScreen(
this, "net/jools/test/SplashTest.jpg", _title, progressLabel);
// Set the background color of the SplashScreen
splashScreen.getContentPane().setBackground(Color.white);
// Show the SplashScreen
splashScreen.setVisible(true);
}
Listing 2
public SplashScreen(...)
{
JComponent pane = (JComponent)getContentPane();
// Use a vertical BoxLayout to arrange the components
// top to bottom.
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
i = new ResourceImageIcon(splashName);
// To ensure that the image is centered horizontally, cen-
// ter it in the JLabel and center the JLabel in the window.
image = new JLabel(i, JLabel.CENTER);
image.setAlignmentX(Component.CENTER_ALIGNMENT);
// Add a border to the window
pane.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createRaisedBevelBorder(),
BorderFactory.createLoweredBevelBorder()));
// The order in which the components are now added to the
// window is important. First the title, then the image,
// then the footer.
// To ensure that the title component spans the full width
// of the window set its maximum sizes. The caller is
// responsible for its alignment
if (title != null) {
title.setMaximumSize(
new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
pane.add(title);
}
pane.add(image);
if (footer != null) {
footer.setMaximumSize(
new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
pane.add(footer);
}
...
}
|
|