/* * Example2.java * * Created on Sat May 11 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, * using XML to specify constraints. * * @author James Elliott, jim@brunchboy.com * @version $Id: Example2.java,v 1.3 2002/08/19 01:23:24 jim Exp $ **/ public class Example2 { /** * Provides access to the CVS version of this class. **/ public static final String VERSION = "$Id: Example2.java,v 1.3 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 2"); 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 2"); title.setFont(title.getFont().deriveFont(20.0f)); aboutBox.getContentPane().add(title, "title"); // Add the version number. aboutBox.getContentPane().add(new JLabel("Version 2.0"), "version"); // Add the date. aboutBox.getContentPane().add(new JLabel("December, 2002"), "date"); // 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"); // Finally, add the "OK" button. JButton okButton = new JButton("OK"); aboutBox.getContentPane().add(okButton, "ok"); // Set up the constraints for all components. XmlConstraintBuilder builder = new XmlConstraintBuilder(); try { builder.addConstraints(new File("example2.xml"), ourLayout); } catch (Exception e) { e.printStackTrace(); } // 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); } }