home *** CD-ROM | disk | FTP | other *** search
/ Chip Special: HTML & Java / Chip-Special_1997-01_HTML-a-Java.bin / javasdk / sdk-java.exe / SDKJava.cab / Samples / AWT / menux / MessageBox.java < prev    next >
Encoding:
Java Source  |  1996-10-10  |  2.7 KB  |  111 lines

  1. import java.awt.*;
  2.  
  3.  
  4.     //////////////////////////////////////////////////////////////////////////
  5.  
  6.     public class MessageBox extends Dialog
  7.     {
  8.         private String szOK = "OK";
  9.         private String szCancel = "Cancel";
  10.  
  11.     public static int MB_MODAL = 1;
  12.  
  13.     //////////////////////////////////////////////////////////////////////////
  14.     
  15.     public MessageBox( Frame f, int flags, String cap )
  16.     {
  17.         this( f, flags, "MS Java", cap );
  18.     }
  19.     
  20.     //////////////////////////////////////////////////////////////////////////
  21.  
  22.     public Insets insets()
  23.     {
  24.                 return new Insets(10, 10, 10, 10);
  25.     }
  26.     
  27.     //////////////////////////////////////////////////////////////////////////
  28.  
  29.         public MessageBox( Frame fmParent, int flags, String title, String cap )
  30.     {
  31.         super(fmParent, title, ((flags & MB_MODAL)!=0) );
  32.         
  33.         // panel font
  34.         //
  35.         Font        fontBox = new Font("Dialog", Font.PLAIN, 10);
  36.         FontMetrics fm      = getFontMetrics(fontBox);
  37.  
  38.         int XSLOP = 8;        // slop around text so not shoved onto border of item
  39.         int YSLOP = 8;
  40.         int height    = fm.getHeight() + YSLOP*2;
  41.         int x_caption = fm.stringWidth(cap);
  42.         int x_ok      = fm.stringWidth(szOK);
  43.         int x_cancel  = fm.stringWidth(szCancel);
  44.         int x_button  = x_ok;
  45.         if( x_ok < x_cancel)
  46.             x_button = x_cancel;
  47.         
  48.         if( (x_button*2+XSLOP) > x_caption )
  49.             x_caption = x_button*2+XSLOP;
  50.  
  51.         if( x_caption == 0 )
  52.         {
  53.             System.out.println("bugger: x_caption == 0");
  54.             x_caption = 100;
  55.             height=12;
  56.         }
  57.         reshape(0, 0, x_caption + XSLOP*5, height*3+YSLOP*2);
  58.  
  59.         // panel components
  60.         //
  61.         Button ok     = new Button(szOK);
  62.         Button cancel = new Button(szCancel);
  63.         Label caption = new Label(cap, Label.CENTER);
  64.  
  65.         caption.setFont(fontBox);
  66.         ok.setFont(fontBox);
  67.         cancel.setFont(fontBox);
  68.  
  69.         // panel layout
  70.         //
  71.         setLayout(new GridLayout(2, 1, 0, YSLOP));
  72.         Panel pnCaption = new Panel();
  73.         Panel pnButtons = new Panel();
  74.         add(pnCaption);
  75.         add(pnButtons);
  76.  
  77.         pnCaption.setLayout(null);
  78.         pnCaption.add(caption);
  79.         
  80.         pnCaption.setLayout(new GridLayout(1, 2, XSLOP, 0));
  81.         pnButtons.add(ok);
  82.         pnButtons.add(cancel);
  83.  
  84.         setResizable(false);
  85.         show();
  86.     }
  87.  
  88.     //////////////////////////////////////////////////////////////////////////
  89.     
  90.     public boolean action(Event e, Object o)
  91.     {
  92.         if( o instanceof String )
  93.         {
  94.             if( ((String)o).equals(szOK) )
  95.                 onButton(1);
  96.             else if( ((String)o).equals(szCancel) )
  97.                 onButton(2);
  98.         }
  99.         return false;
  100.     }
  101.     
  102.     //////////////////////////////////////////////////////////////////////////
  103.  
  104.     public void onButton(int result)
  105.     {
  106.         dispose();
  107.     }
  108.     
  109.     //////////////////////////////////////////////////////////////////////////
  110. }    
  111.