home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-10-10 | 3.1 KB | 138 lines |
- import java.awt.*;
-
-
- //////////////////////////////////////////////////////////////////////////
-
- public class MessageBox extends Dialog
- {
- private String szOK = "OK";
- private String szCancel = "Cancel";
-
- public static int MB_MODAL = 1;
-
- //////////////////////////////////////////////////////////////////////////
-
- public MessageBox( Frame f, int flags, String cap )
- {
- this( f, flags, "MS Java", cap );
- }
-
- //////////////////////////////////////////////////////////////////////////
-
- public Insets insets()
- {
- return new Insets(10, 10, 10, 10);
- }
-
- //////////////////////////////////////////////////////////////////////////
-
- public MessageBox( Frame fmParent, int flags, String title, String cap )
- {
- super(fmParent, title, ((flags & MB_MODAL)!=0) );
-
- // panel font
- //
- Font fontBox = new Font("Dialog", Font.PLAIN, 10);
- FontMetrics fm = getFontMetrics(fontBox);
-
- int XSLOP = 8; // slop around text so not shoved onto border of item
- int YSLOP = 8;
- int height = fm.getHeight() + YSLOP*2;
- int x_caption = fm.stringWidth(cap);
- int x_ok = fm.stringWidth(szOK);
- int x_cancel = fm.stringWidth(szCancel);
- int x_button = x_ok;
- if( x_ok < x_cancel)
- x_button = x_cancel;
-
- if( (x_button*2+XSLOP) > x_caption )
- x_caption = x_button*2+XSLOP;
-
- if( x_caption == 0 )
- {
- x_caption = 100;
- height=12;
- }
- reshape(0, 0, x_caption + XSLOP*5, height*3+YSLOP*2);
-
- // panel components
- //
- Button ok = new Button(szOK);
- Button cancel = new Button(szCancel);
- Label caption = new Label(cap, Label.CENTER);
-
- caption.setFont(fontBox);
- ok.setFont(fontBox);
- cancel.setFont(fontBox);
-
- // panel layout
- //
- setLayout(new GridLayout(2, 1, 0, YSLOP));
- Panel pnCaption = new Panel();
- Panel pnButtons = new Panel();
- add(pnCaption);
- add(pnButtons);
-
- pnCaption.setLayout(null);
- pnCaption.add(caption);
-
- pnCaption.setLayout(new GridLayout(1, 2, XSLOP, 0));
- pnButtons.add(ok);
- pnButtons.add(cancel);
-
- setResizable(false);
- show();
- }
-
- //////////////////////////////////////////////////////////////////////////
-
- public boolean action(Event e, Object o)
- {
- if( o instanceof String )
- {
- if( ((String)o).equals(szOK) )
- onButton(1);
- else if( ((String)o).equals(szCancel) )
- onButton(2);
- }
- return false;
- }
-
- //////////////////////////////////////////////////////////////////////////
-
- public void onButton(int result)
- {
- dispose();
- }
-
- //////////////////////////////////////////////////////////////////////////
- /*
- public boolean handleEvent(Event evt)
- {
- switch(evt.id)
- {
- // case Event.MOUSE_DOWN:
- // case Event.MOUSE_UP:
- // for (Enumeration e = keys.elements(); e.hasMoreElements() ;)
- // {
- // Key k = (Key)e.nextElement();
- // if(k.inside(evt.x, evt.y))
- // {
- // downKey = k;
- // k.startPlay();
- // break;
- // }
- // }
- // break;
-
- case Event.KEY_PRESS:
- dispose();
-
- default:
- break;
- }
- return false;
- }
- */
- }
-