1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52:
53: import ;
54: import ;
55:
56:
68: public class JFrame extends Frame
69: implements WindowConstants, RootPaneContainer, Accessible
70: {
71:
74: protected class AccessibleJFrame extends Frame.AccessibleAWTFrame
75: {
76:
79: public AccessibleJFrame()
80: {
81: super();
82:
83: }
84: }
85:
86:
92: public static final int EXIT_ON_CLOSE = 3;
93:
94: private static final long serialVersionUID = -3362141868504252139L;
95: private static boolean defaultLookAndFeelDecorated;
96: private int close_action = HIDE_ON_CLOSE;
97: protected AccessibleContext accessibleContext;
98: protected JRootPane rootPane;
99:
100:
103: protected boolean rootPaneCheckingEnabled = false;
104:
105:
110: private boolean initStageDone = false;
111:
112: public JFrame()
113: {
114: super("JFrame");
115: frameInit();
116: }
117:
118: public JFrame(String title)
119: {
120: super(title);
121: frameInit();
122: }
123:
124:
133: public JFrame(GraphicsConfiguration gc)
134: {
135: super(gc);
136: frameInit();
137: }
138:
139:
149: public JFrame(String title, GraphicsConfiguration gc)
150: {
151: super(title, gc);
152: frameInit();
153: }
154:
155: protected void frameInit()
156: {
157: super.setLayout(new BorderLayout(1, 1));
158: enableEvents(AWTEvent.WINDOW_EVENT_MASK);
159: getRootPane();
160:
161: initStageDone = true;
162: }
163:
164: public Dimension getPreferredSize()
165: {
166: return super.getPreferredSize();
167: }
168:
169: public JMenuBar getJMenuBar()
170: {
171: return getRootPane().getJMenuBar();
172: }
173:
174: public void setJMenuBar(JMenuBar menubar)
175: {
176: getRootPane().setJMenuBar(menubar);
177: }
178:
179: public void setLayout(LayoutManager manager)
180: {
181:
182:
183: if (initStageDone)
184: {
185: if (isRootPaneCheckingEnabled())
186: throw new Error("Cannot set layout. Use getContentPane().setLayout()"
187: + " instead.");
188: getContentPane().setLayout(manager);
189: }
190: else
191: super.setLayout(manager);
192: }
193:
194: public void setLayeredPane(JLayeredPane layeredPane)
195: {
196: getRootPane().setLayeredPane(layeredPane);
197: }
198:
199: public JLayeredPane getLayeredPane()
200: {
201: return getRootPane().getLayeredPane();
202: }
203:
204: public JRootPane getRootPane()
205: {
206: if (rootPane == null)
207: setRootPane(createRootPane());
208: return rootPane;
209: }
210:
211: protected void setRootPane(JRootPane root)
212: {
213: if (rootPane != null)
214: remove(rootPane);
215:
216: rootPane = root;
217: add(rootPane, BorderLayout.CENTER);
218: }
219:
220: protected JRootPane createRootPane()
221: {
222: return new JRootPane();
223: }
224:
225: public Container getContentPane()
226: {
227: return getRootPane().getContentPane();
228: }
229:
230: public void setContentPane(Container contentPane)
231: {
232: getRootPane().setContentPane(contentPane);
233: }
234:
235: public Component getGlassPane()
236: {
237: return getRootPane().getGlassPane();
238: }
239:
240: public void setGlassPane(Component glassPane)
241: {
242: getRootPane().setGlassPane(glassPane);
243: }
244:
245: protected void addImpl(Component comp, Object constraints, int index)
246: {
247:
248:
249: if (!initStageDone)
250: super.addImpl(comp, constraints, index);
251: else
252: {
253: if (isRootPaneCheckingEnabled())
254: throw new Error("rootPaneChecking is enabled - adding components "
255: + "disallowed.");
256: getContentPane().add(comp,constraints,index);
257: }
258: }
259:
260: public void remove(Component comp)
261: {
262:
263:
264: if (comp==rootPane)
265: super.remove(rootPane);
266: else
267: getContentPane().remove(comp);
268: }
269:
270: protected boolean isRootPaneCheckingEnabled()
271: {
272: return rootPaneCheckingEnabled;
273: }
274:
275: protected void setRootPaneCheckingEnabled(boolean enabled)
276: {
277: rootPaneCheckingEnabled = enabled;
278: }
279:
280: public void update(Graphics g)
281: {
282: paint(g);
283: }
284:
285: protected void processKeyEvent(KeyEvent e)
286: {
287: super.processKeyEvent(e);
288: }
289:
290: public static void setDefaultLookAndFeelDecorated(boolean decorated)
291: {
292: defaultLookAndFeelDecorated = decorated;
293: }
294:
295: public static boolean isDefaultLookAndFeelDecorated()
296: {
297: return defaultLookAndFeelDecorated;
298: }
299:
300: public AccessibleContext getAccessibleContext()
301: {
302: if (accessibleContext == null)
303: accessibleContext = new AccessibleJFrame();
304: return accessibleContext;
305: }
306:
307: public int getDefaultCloseOperation()
308: {
309: return close_action;
310: }
311:
312: protected String paramString()
313: {
314: return "JFrame";
315: }
316:
317: protected void processWindowEvent(WindowEvent e)
318: {
319: super.processWindowEvent(e);
320: switch (e.getID())
321: {
322: case WindowEvent.WINDOW_CLOSING:
323: {
324: switch (close_action)
325: {
326: case EXIT_ON_CLOSE:
327: {
328: System.exit(0);
329: break;
330: }
331: case DISPOSE_ON_CLOSE:
332: {
333: dispose();
334: break;
335: }
336: case HIDE_ON_CLOSE:
337: {
338: setVisible(false);
339: break;
340: }
341: case DO_NOTHING_ON_CLOSE:
342: break;
343: }
344: break;
345: }
346: case WindowEvent.WINDOW_CLOSED:
347: case WindowEvent.WINDOW_OPENED:
348: case WindowEvent.WINDOW_ICONIFIED:
349: case WindowEvent.WINDOW_DEICONIFIED:
350: case WindowEvent.WINDOW_ACTIVATED:
351: case WindowEvent.WINDOW_DEACTIVATED:
352: break;
353: }
354: }
355:
356:
369: public void setDefaultCloseOperation(int operation)
370: {
371: SecurityManager sm = System.getSecurityManager();
372: if (sm != null && operation == EXIT_ON_CLOSE)
373: sm.checkExit(0);
374:
375: if (operation != EXIT_ON_CLOSE && operation != DISPOSE_ON_CLOSE
376: && operation != HIDE_ON_CLOSE && operation != DO_NOTHING_ON_CLOSE)
377: throw new IllegalArgumentException("defaultCloseOperation must be EXIT_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or DO_NOTHING_ON_CLOSE");
378:
379: close_action = operation;
380: }
381: }