home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 25 / IOPROG_25.ISO / SOFT / JavaS / javastar-eval.exe / data1.cab / Program_Files / examples / namedb / gjt / Box.java < prev    next >
Encoding:
Java Source  |  1999-02-11  |  2.8 KB  |  91 lines

  1. package gjt;
  2. import java.awt.*;
  3.  
  4. /**
  5.  * A Panel containing a single component; an etched rectangle is 
  6.  * drawn around the component, and a Label is centered at the top
  7.  * of the rectangle. Of course, the single component may be
  8.  * a container, and therefore a Box may surround many components.
  9.  * <p>
  10.  * 
  11.  * Both the Component around which the box is drawn, and the 
  12.  * String drawn at the top of the box are specified at 
  13.  * construction time.<p>
  14.  *
  15.  * Etching of the box is controlled by etchedIn() and 
  16.  * etchedOut().  Default etching is etched in.<p>
  17.  *
  18.  * <em>Note: AWT 1.0.2 contains a bug which causes the 
  19.  * Label.CENTER argument of the Label created for the title
  20.  * to be ignored, under Win95.  Therefore, under Win95, the
  21.  * title will be off-center.</em><p>
  22.  *
  23.  * @version 1.0, Apr 1 1996
  24.  * @author  David Geary
  25.  * @see     EtchedRectangle
  26.  * @see     gjt.test.BoxTest
  27.  */
  28. public class Box extends Panel {
  29.     private EtchedRectangle box = new EtchedRectangle(this);
  30.     public Label           titleLabel;
  31.  
  32.     public Box(Component surrounded, String title) {
  33.         this(surrounded, new Label(title, Label.CENTER));
  34.     }
  35.     public Box(Component surrounded, Label label) {
  36.         Assert.notNull(surrounded);
  37.         Assert.notNull(label);
  38.  
  39.         titleLabel = label;
  40.  
  41.         GridBagLayout      gbl = new GridBagLayout();
  42.         GridBagConstraints gbc = new GridBagConstraints();
  43.  
  44.         setLayout(gbl);
  45.         gbc.gridwidth = GridBagConstraints.REMAINDER;
  46.         gbc.anchor    = GridBagConstraints.NORTH;
  47.         gbl.setConstraints(titleLabel, gbc);
  48.     add(titleLabel);
  49.  
  50.     gbc.insets  = new Insets(0,10,10,10);
  51.         gbc.anchor  = GridBagConstraints.CENTER;
  52.         gbc.weighty = 1.0;
  53.         gbc.weightx = 1.0;
  54.         gbc.fill    = GridBagConstraints.BOTH;
  55.         gbl.setConstraints(surrounded,gbc); 
  56.         add(surrounded);
  57.     }
  58.     public void etchedIn ()           { box.etchedIn (); }
  59.     public void etchedOut()           { box.etchedOut(); }
  60.     public void paint    (Graphics g){
  61.       box.paint();     
  62.     }
  63.  
  64.   public void setText(String label) {
  65.     titleLabel.setText(label);
  66.   }
  67.   public String getText(String label) {
  68.     return titleLabel.getText();
  69.   }
  70.     public void resize(int w, int h) {
  71.         reshape(location().x, location().y, w, h);
  72.     }
  73.     public void reshape(int x, int y, int w, int h) {
  74.         super.reshape(x,y,w,h);
  75.  
  76.     if (titleLabel.getFont()==null)
  77.       return;
  78.         FontMetrics fm   = titleLabel.getFontMetrics(
  79.                                 titleLabel.getFont());
  80.         int         top  = insets().top + fm.getAscent();
  81.         Dimension   size = size();
  82.  
  83.         box.reshape(0, top, size.width-1, size.height-top-1);
  84.     }
  85.     protected String paramString() {
  86.         return super.paramString() + ",etching=" +
  87.         (box.isEtchedIn() ? Etching.IN : Etching.OUT) +
  88.         ",title=" + titleLabel;
  89.     }
  90. }
  91.