1 7 package javax.swing; 8 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.beans.PropertyChangeListener ; 12 import java.util.Locale ; 13 import java.util.Vector ; 14 import java.io.Serializable ; 15 16 import javax.accessibility.*; 17 18 19 92 public class JFrame extends Frame implements WindowConstants , Accessible, RootPaneContainer 93 { 94 102 public static final int EXIT_ON_CLOSE = 3; 103 104 108 private static final Object defaultLookAndFeelDecoratedKey = 109 new StringBuffer ("JFrame.defaultLookAndFeelDecorated"); 110 111 private int defaultCloseOperation = HIDE_ON_CLOSE; 112 113 122 protected JRootPane rootPane; 123 124 133 protected boolean rootPaneCheckingEnabled = false; 134 135 136 149 public JFrame() throws HeadlessException { 150 super(); 151 frameInit(); 152 } 153 154 173 public JFrame(GraphicsConfiguration gc) { 174 super(gc); 175 frameInit(); 176 } 177 178 193 public JFrame(String title) throws HeadlessException { 194 super(title); 195 frameInit(); 196 } 197 198 219 public JFrame(String title, GraphicsConfiguration gc) { 220 super(title, gc); 221 frameInit(); 222 } 223 224 225 protected void frameInit() { 226 enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK); 227 setLocale( JComponent.getDefaultLocale() ); 228 setRootPane(createRootPane()); 229 setBackground(UIManager.getColor("control")); 230 setRootPaneCheckingEnabled(true); 231 if (JFrame.isDefaultLookAndFeelDecorated()) { 232 boolean supportsWindowDecorations = 233 UIManager.getLookAndFeel().getSupportsWindowDecorations(); 234 if (supportsWindowDecorations) { 235 setUndecorated(true); 236 getRootPane().setWindowDecorationStyle(JRootPane.FRAME); 237 } 238 } 239 sun.awt.SunToolkit.checkAndSetPolicy(this, true); 240 } 241 242 246 protected JRootPane createRootPane() { 247 JRootPane rp = new JRootPane (); 248 rp.setOpaque(true); 253 return rp; 254 } 255 256 265 protected void processWindowEvent(WindowEvent e) { 266 super.processWindowEvent(e); 267 268 if (e.getID() == WindowEvent.WINDOW_CLOSING) { 269 switch(defaultCloseOperation) { 270 case HIDE_ON_CLOSE: 271 setVisible(false); 272 break; 273 case DISPOSE_ON_CLOSE: 274 setVisible(false); 275 dispose(); 276 break; 277 case DO_NOTHING_ON_CLOSE: 278 default: 279 break; 280 case EXIT_ON_CLOSE: 281 System.exit(0); 284 break; 285 } 286 } 287 } 288 289 293 352 public void setDefaultCloseOperation(int operation) { 353 if (operation != DO_NOTHING_ON_CLOSE && 354 operation != HIDE_ON_CLOSE && 355 operation != DISPOSE_ON_CLOSE && 356 operation != EXIT_ON_CLOSE) { 357 throw new IllegalArgumentException ("defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or EXIT_ON_CLOSE"); 358 } 359 if (this.defaultCloseOperation != operation) { 360 if (operation == EXIT_ON_CLOSE) { 361 SecurityManager security = System.getSecurityManager(); 362 if (security != null) { 363 security.checkExit(0); 364 } 365 } 366 int oldValue = this.defaultCloseOperation; 367 this.defaultCloseOperation = operation; 368 firePropertyChange("defaultCloseOperation", oldValue, operation); 369 } 370 } 371 372 373 380 public int getDefaultCloseOperation() { 381 return defaultCloseOperation; 382 } 383 384 385 391 public void update(Graphics g) { 392 paint(g); 393 } 394 395 405 public void setJMenuBar(JMenuBar menubar) { 406 getRootPane().setMenuBar(menubar); 407 } 408 409 415 public JMenuBar getJMenuBar() { 416 return getRootPane().getMenuBar(); 417 } 418 419 431 protected boolean isRootPaneCheckingEnabled() { 432 return rootPaneCheckingEnabled; 433 } 434 435 436 452 protected void setRootPaneCheckingEnabled(boolean enabled) { 453 rootPaneCheckingEnabled = enabled; 454 } 455 456 457 476 protected void addImpl(Component comp, Object constraints, int index) 477 { 478 if(isRootPaneCheckingEnabled()) { 479 getContentPane().add(comp, constraints, index); 480 } 481 else { 482 super.addImpl(comp, constraints, index); 483 } 484 } 485 486 498 public void remove(Component comp) { 499 if (comp == rootPane) { 500 super.remove(comp); 501 } else { 502 getContentPane().remove(comp); 503 } 504 } 505 506 507 518 public void setLayout(LayoutManager manager) { 519 if(isRootPaneCheckingEnabled()) { 520 getContentPane().setLayout(manager); 521 } 522 else { 523 super.setLayout(manager); 524 } 525 } 526 527 528 535 public JRootPane getRootPane() { 536 return rootPane; 537 } 538 539 540 551 protected void setRootPane(JRootPane root) 552 { 553 if(rootPane != null) { 554 remove(rootPane); 555 } 556 rootPane = root; 557 if(rootPane != null) { 558 boolean checkingEnabled = isRootPaneCheckingEnabled(); 559 try { 560 setRootPaneCheckingEnabled(false); 561 add(rootPane, BorderLayout.CENTER); 562 } 563 finally { 564 setRootPaneCheckingEnabled(checkingEnabled); 565 } 566 } 567 } 568 569 public void setIconImage(Image image) { 570 Image oldImage = getIconImage(); 571 super.setIconImage(image); 572 firePropertyChange("iconImage", oldImage, image); 573 } 574 575 582 public Container getContentPane() { 583 return getRootPane().getContentPane(); 584 } 585 586 608 public void setContentPane(Container contentPane) { 609 getRootPane().setContentPane(contentPane); 610 } 611 612 619 public JLayeredPane getLayeredPane() { 620 return getRootPane().getLayeredPane(); 621 } 622 623 637 public void setLayeredPane(JLayeredPane layeredPane) { 638 getRootPane().setLayeredPane(layeredPane); 639 } 640 641 648 public Component getGlassPane() { 649 return getRootPane().getGlassPane(); 650 } 651 652 664 public void setGlassPane(Component glassPane) { 665 getRootPane().setGlassPane(glassPane); 666 } 667 668 692 public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) { 693 if (defaultLookAndFeelDecorated) { 694 SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.TRUE); 695 } else { 696 SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.FALSE); 697 } 698 } 699 700 701 709 public static boolean isDefaultLookAndFeelDecorated() { 710 Boolean defaultLookAndFeelDecorated = 711 (Boolean ) SwingUtilities.appContextGet(defaultLookAndFeelDecoratedKey); 712 if (defaultLookAndFeelDecorated == null) { 713 defaultLookAndFeelDecorated = Boolean.FALSE; 714 } 715 return defaultLookAndFeelDecorated.booleanValue(); 716 } 717 718 728 protected String paramString() { 729 String defaultCloseOperationString; 730 if (defaultCloseOperation == HIDE_ON_CLOSE) { 731 defaultCloseOperationString = "HIDE_ON_CLOSE"; 732 } else if (defaultCloseOperation == DISPOSE_ON_CLOSE) { 733 defaultCloseOperationString = "DISPOSE_ON_CLOSE"; 734 } else if (defaultCloseOperation == DO_NOTHING_ON_CLOSE) { 735 defaultCloseOperationString = "DO_NOTHING_ON_CLOSE"; 736 } else if (defaultCloseOperation == 3) { 737 defaultCloseOperationString = "EXIT_ON_CLOSE"; 738 } else defaultCloseOperationString = ""; 739 String rootPaneString = (rootPane != null ? 740 rootPane.toString() : ""); 741 String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ? 742 "true" : "false"); 743 744 return super.paramString() + 745 ",defaultCloseOperation=" + defaultCloseOperationString + 746 ",rootPane=" + rootPaneString + 747 ",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString; 748 } 749 750 751 752 756 757 protected AccessibleContext accessibleContext = null; 758 759 768 public AccessibleContext getAccessibleContext() { 769 if (accessibleContext == null) { 770 accessibleContext = new AccessibleJFrame(); 771 } 772 return accessibleContext; 773 } 774 775 781 protected class AccessibleJFrame extends AccessibleAWTFrame { 782 783 790 public String getAccessibleName() { 791 if (accessibleName != null) { 792 return accessibleName; 793 } else { 794 if (getTitle() == null) { 795 return super.getAccessibleName(); 796 } else { 797 return getTitle(); 798 } 799 } 800 } 801 802 809 public AccessibleStateSet getAccessibleStateSet() { 810 AccessibleStateSet states = super.getAccessibleStateSet(); 811 812 if (isResizable()) { 813 states.add(AccessibleState.RESIZABLE); 814 } 815 if (getFocusOwner() != null) { 816 states.add(AccessibleState.ACTIVE); 817 } 818 return states; 821 } 822 } } 824 | Popular Tags |