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 / directX / d3drm / Viewer / MessageBox.java < prev    next >
Encoding:
Java Source  |  1996-10-10  |  3.1 KB  |  138 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.             x_caption = 100;
  54.             height=12;
  55.         }
  56.         reshape(0, 0, x_caption + XSLOP*5, height*3+YSLOP*2);
  57.  
  58.         // panel components
  59.         //
  60.         Button ok     = new Button(szOK);
  61.         Button cancel = new Button(szCancel);
  62.         Label caption = new Label(cap, Label.CENTER);
  63.  
  64.         caption.setFont(fontBox);
  65.         ok.setFont(fontBox);
  66.         cancel.setFont(fontBox);
  67.  
  68.         // panel layout
  69.         //
  70.         setLayout(new GridLayout(2, 1, 0, YSLOP));
  71.         Panel pnCaption = new Panel();
  72.         Panel pnButtons = new Panel();
  73.         add(pnCaption);
  74.         add(pnButtons);
  75.  
  76.         pnCaption.setLayout(null);
  77.         pnCaption.add(caption);
  78.         
  79.         pnCaption.setLayout(new GridLayout(1, 2, XSLOP, 0));
  80.         pnButtons.add(ok);
  81.         pnButtons.add(cancel);
  82.  
  83.         setResizable(false);
  84.         show();
  85.     }
  86.  
  87.     //////////////////////////////////////////////////////////////////////////
  88.     
  89.     public boolean action(Event e, Object o)
  90.     {
  91.         if( o instanceof String )
  92.         {
  93.             if( ((String)o).equals(szOK) )
  94.                 onButton(1);
  95.             else if( ((String)o).equals(szCancel) )
  96.                 onButton(2);
  97.         }
  98.         return false;
  99.     }
  100.     
  101.     //////////////////////////////////////////////////////////////////////////
  102.  
  103.     public void onButton(int result)
  104.     {
  105.         dispose();
  106.     }
  107.     
  108.     //////////////////////////////////////////////////////////////////////////
  109. /*
  110.     public boolean handleEvent(Event evt) 
  111.        {
  112.         switch(evt.id)
  113.         {
  114. //            case Event.MOUSE_DOWN:
  115. //            case Event.MOUSE_UP:
  116. //                for (Enumeration e = keys.elements(); e.hasMoreElements() ;)
  117. //                {
  118. //                    Key k = (Key)e.nextElement();
  119. //                    if(k.inside(evt.x, evt.y))
  120. //                    {
  121. //                        downKey = k;
  122. //                        k.startPlay();
  123. //                        break;
  124. //                    }
  125. //                }
  126. //                break;
  127.  
  128.             case Event.KEY_PRESS:
  129.                 dispose();
  130.              
  131.             default:
  132.                 break;
  133.         }                
  134.     return false;
  135.     }
  136. */
  137. }    
  138.