Source of DesktopFrame.java


  1: // Fig. 22.11: DesktopFrame.java
  2: // Demonstrating JDesktopPane.
  3: import java.awt.BorderLayout;
  4: import java.awt.Dimension;
  5: import java.awt.Graphics;
  6: import java.awt.event.ActionListener;
  7: import java.awt.event.ActionEvent;
  8: import java.util.Random;
  9: import javax.swing.JFrame;
 10: import javax.swing.JDesktopPane;
 11: import javax.swing.JMenuBar;
 12: import javax.swing.JMenu;
 13: import javax.swing.JMenuItem;
 14: import javax.swing.JInternalFrame;
 15: import javax.swing.JPanel;
 16: import javax.swing.ImageIcon;
 17: 
 18: public class DesktopFrame extends JFrame 
 19: {
 20:    private JDesktopPane theDesktop;
 21: 
 22:    // set up GUI
 23:    public DesktopFrame()
 24:    {
 25:       super( "Using a JDesktopPane" );
 26: 
 27:       JMenuBar bar = new JMenuBar(); // create menu bar
 28:       JMenu addMenu = new JMenu( "Add" ); // create Add menu
 29:       JMenuItem newFrame = new JMenuItem( "Internal Frame" );
 30: 
 31:       addMenu.add( newFrame ); // add new frame item to Add menu
 32:       bar.add( addMenu ); // add Add menu to menu bar
 33:       setJMenuBar( bar ); // set menu bar for this application
 34: 
 35:       theDesktop = new JDesktopPane(); // create desktop pane
 36:       add( theDesktop ); // add desktop pane to frame
 37:     
 38:       // set up listener for newFrame menu item
 39:       newFrame.addActionListener(
 40: 
 41:          new ActionListener() // anonymous inner class
 42:          {  
 43:             // display new internal window
 44:             public void actionPerformed( ActionEvent event ) 
 45:             {
 46:                // create internal frame
 47:                JInternalFrame frame = new JInternalFrame( 
 48:                   "Internal Frame", true, true, true, true );
 49: 
 50:                MyJPanel panel = new MyJPanel(); // create new panel
 51:                frame.add( panel, BorderLayout.CENTER ); // add panel
 52:                frame.pack(); // set internal frame to size of contents
 53: 
 54:                theDesktop.add( frame ); // attach internal frame
 55:                frame.setVisible( true ); // show internal frame
 56:             } // end method actionPerformed
 57:          } // end anonymous inner class
 58:       ); // end call to addActionListener
 59:    } // end constructor DesktopFrame
 60: } // end class DesktopFrame
 61: 
 62: // class to display an ImageIcon on a panel
 63: class MyJPanel extends JPanel 
 64: {
 65:    private static Random generator = new Random();
 66:    private ImageIcon picture; // image to be displayed
 67:    private String[] images = { "yellowflowers.png", "purpleflowers.png",
 68:       "redflowers.png", "redflowers2.png", "lavenderflowers.png" };
 69: 
 70:    // load image
 71:    public MyJPanel()
 72:    {
 73:       int randomNumber = generator.nextInt( 5 );
 74:       picture = new ImageIcon( images[ randomNumber ] ); // set icon
 75:    } // end MyJPanel constructor
 76: 
 77:    // display imageIcon on panel
 78:    public void paintComponent( Graphics g )
 79:    {
 80:       super.paintComponent( g );
 81:       picture.paintIcon( this, g, 0, 0 ); // display icon
 82:    } // end method paintComponent
 83: 
 84:    // return image dimensions
 85:    public Dimension getPreferredSize()
 86:    {
 87:       return new Dimension( picture.getIconWidth(), 
 88:          picture.getIconHeight() );  
 89:    } // end method getPreferredSize
 90: } // end class MyJPanel
 91: 
 92: /**************************************************************************
 93:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 94:  * Pearson Education, Inc. All Rights Reserved.                           *
 95:  *                                                                        *
 96:  * DISCLAIMER: The authors and publisher of this book have used their     *
 97:  * best efforts in preparing the book. These efforts include the          *
 98:  * development, research, and testing of the theories and programs        *
 99:  * to determine their effectiveness. The authors and publisher make       *
100:  * no warranty of any kind, expressed or implied, with regard to these    *
101:  * programs or to the documentation contained in these books. The authors *
102:  * and publisher shall not be liable in any event for incidental or       *
103:  * consequential damages in connection with, or arising out of, the       *
104:  * furnishing, performance, or use of these programs.                     *
105:  *************************************************************************/