1 30 31 package rcm.awt; 32 33 import java.awt.*; 34 import java.awt.event.*; 35 import java.beans.*; 36 37 public class OkCancelDialog extends Dialog { 38 39 Panel controlsPane; 40 Button okButton; 41 Button cancelButton; 42 43 public OkCancelDialog (Frame owner, String title) { 44 super (owner, title, true); 45 46 add (controlsPane = new Panel (), BorderLayout.CENTER); 47 48 Panel panel; 49 add (panel = new Panel (), BorderLayout.SOUTH); 50 panel.setLayout (new FlowLayout ()); 51 52 Panel subpanel; 53 panel.add (subpanel = new Panel ()); 54 subpanel.setLayout (new GridLayout (1, 0, 4, 0)); 55 56 subpanel.add (okButton = new Button ("OK")); 57 okButton.addActionListener (new ActionListener () { 58 public void actionPerformed (ActionEvent event) { 59 ok (); 60 } 61 }); 62 63 subpanel.add (cancelButton = new Button ("Cancel")); 64 cancelButton.addActionListener (new ActionListener () { 65 public void actionPerformed (ActionEvent event) { 66 cancel (); 67 } 68 }); 69 70 addWindowListener (new WindowAdapter () { 71 public void windowClosing (WindowEvent event) { 72 cancel (); 73 } 74 }); 75 76 } 77 78 public static void centerWindow (Window window, Component ref) { 79 Dimension size = window.getSize(); 80 Dimension refSize = (ref != null) ? ref.getSize() : Toolkit.getDefaultToolkit().getScreenSize(); 81 Point origin = (ref != null) ? ref.getLocationOnScreen () : new Point (0, 0); 82 83 if (refSize != null) { 84 int x = Math.max (0, origin.x + (refSize.width - size.width) / 2); 85 int y = Math.max (0, origin.y + (refSize.height - size.height) / 2); 86 window.setLocation (x, y); 87 } 88 } 89 90 public void show () { 91 Container parent = getParent (); 92 if (parent != null) 93 centerWindow (this, parent); 94 95 registerKeystrokes (); 96 97 super.show (); 98 } 99 100 public Panel getControlsPane () { 101 return controlsPane; 102 } 103 104 public void registerKeystrokes () { 105 } 107 108 public void ok () { 109 dispose (); 110 } 111 112 public void cancel () { 113 dispose (); 114 } 115 } 116 | Popular Tags |