/* * Example1.java * * Created on Mon Aug 12 2002 */ import com.brunchboy.util.swing.relativelayout.*; import javax.swing.*; import java.util.*; import java.io.*; import java.awt.event.*; /** * Example of building an "about box" with the RelativeLayout layout manager. * This version uses normal Java constructors to create all the constraints. *
* * @author James Elliott, jim@brunchboy.com * @version $Id: Example1.java,v 1.5 2002/08/19 01:23:24 jim Exp $ **/ public class Example1 { /** * Provides access to the CVS version of this class. **/ public static final String VERSION = "$Id: Example1.java,v 1.5 2002/08/19 01:23:24 jim Exp $"; /** * Display the application's "about box".
* * @return the window in which the information is displayed. **/ public static JFrame showAboutBox() { // Create the about box and assign it a RelativeLayout. final JFrame aboutBox = new JFrame("About Example 1"); aboutBox.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); RelativeLayout ourLayout = new RelativeLayout(); aboutBox.getContentPane().setLayout(ourLayout); // Add the application title, with a font size of twenty points. JLabel title = new JLabel("Example 1"); title.setFont(title.getFont().deriveFont(20.0f)); aboutBox.getContentPane().add(title, "title"); // Position the title as specified by the UI sketch: Ten pixels // below the top of the window, centered horizontally. ourLayout.addConstraint("title", AttributeType.TOP, new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.TOP, 10)); ourLayout.addConstraint("title", AttributeType.HORIZONTAL_CENTER, new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.HORIZONTAL_CENTER)); // Add the version number, positioned eight pixels below the title, // and five pixels from the left edge of the window. aboutBox.getContentPane().add(new JLabel("Version 2.0"), "version"); ourLayout.addConstraint("version", AttributeType.TOP, new AttributeConstraint("title", AttributeType.BOTTOM, 8)); ourLayout.addConstraint("version", AttributeType.LEFT, new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.LEFT, 5)); // Add the date, at the same height as the version, five pixels from // the right edge of the window. aboutBox.getContentPane().add(new JLabel("December, 2002"), "date"); ourLayout.addConstraint("date", AttributeType.TOP, new AttributeConstraint("version", AttributeType.TOP)); ourLayout.addConstraint("date", AttributeType.RIGHT, new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.RIGHT, -5)); // Create the scrolling details area, and fill it with enough // "information" to scroll. JTextArea details = new JTextArea("This is where the details go...\n"); details.setEditable(false); details.setLineWrap(true); details.setWrapStyleWord(true); for (int i = 1; i < 10; i++) { details.append("Filler line " + i + '\n'); } aboutBox.getContentPane().add(new JScrollPane(details), "details"); // Position it as in the sketch, with left and right edges flush with // the two text areas above, and top and bottom boundaries relative to // them and the "OK" button. Notice that we can set up the constraint // to the button even though that's not yet been added. ourLayout.addConstraint("details", AttributeType.LEFT, new AttributeConstraint("version", AttributeType.LEFT)); ourLayout.addConstraint("details", AttributeType.RIGHT, new AttributeConstraint("date", AttributeType.RIGHT)); ourLayout.addConstraint("details", AttributeType.TOP, new AttributeConstraint("version", AttributeType.BOTTOM, 4)); ourLayout.addConstraint("details", AttributeType.BOTTOM, new AttributeConstraint("ok", AttributeType.TOP, -4)); // Finally, add the "OK" button, demonstrating a different way to // center a component in the window. JButton okButton = new JButton("OK"); aboutBox.getContentPane().add(okButton, "ok"); ourLayout.addConstraint("ok", AttributeType.HORIZONTAL_CENTER, new AxisConstraint(DependencyManager.ROOT_NAME, AttributeAxis.HORIZONTAL, 0.5)); ourLayout.addConstraint("ok", AttributeType.BOTTOM, new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.BOTTOM, -10)); // Arrange it so that clicking the "OK" button closes the window. okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { aboutBox.setVisible(false); } }); // Set the initial size of the frame and show it on the screen. aboutBox.setSize(300, 200); aboutBox.setVisible(true); return aboutBox; } /** * Displays the about box when invoked from the command line. **/ public static void main(String args[]) { // Show the about box. JFrame aboutBox = showAboutBox(); // Wait for the about box to be closed, and then exit. (In a real app, // we wouldn't care about the window closing, but for this example, // be nice and exit the demo once it closes.) while(aboutBox.isVisible()) { try { Thread.sleep(500); } catch (InterruptedException ie) { ie.printStackTrace(); System.exit(1); } } System.exit(0); } }