Using Swing Components(1)

using top-level containers (JFrame,JDialog,JApplet)
 !these facts should be kept in mind:
   !! To appear onscreen,every GUI component must be part of a containment hierarchy.A containment hierarchy is a tree of components that has a top-level container as its root.
   !! Each GUI component can be contained only once.
   !! you can optionally add a menu bar to a top-level container(positioned within the top-level container,but outside the content pane).
!top-level containers and Containment Hierachies
   !!As a rule,a standalone application with a Swing-based GUI has at least one containment hierarchy with a JFrame as its root.
   !!A Swing-based applet has at least one containment hierarchy,exactly one of which is rooted by a JApplet object.
  !Adding Compoents to the Content Pane(why the content pane???)
   !!the default content pane is a simple intermediate container that inherits from JComponent,and that uses a BorderLayout as its layout manager.
   !!customizing the content pane :setting the layout manager and adding a border.
   !!gotcha:
     !!! getcontentPane method: returns a Container object,not a JComponent object.
     !!! take advantage of the content pane's JComponent features:
          !!!!typecast the return value
          !!!!create your own component to be the content pane.(our examples generally take the second approach,since it's a little cleaner.): make sure it's opaque.an opaque JPanel object makes a good content pane.(layout manager!)
          !!!!add a customized component to the content pane,covering the content pane completely.
               use the top-level container's setContentPane method.for example:
                //Create a panel and add components to it.
                  JPanel contentPane = new JPanel(new BorderLayout());
                    contentPane.setBorder(someBorder);
                     contentPane.add(someComponent, BorderLayout.CENTER);
                   contentPane.add(anotherComponent, BorderLayout.PAGE_END);

                //Make it the content pane.
                  contentPane.setOpaque(true);
                   topLevelContainer.setContentPane(contentPane);
           NOTE:In most look and feels,JPanels are opaque by default.however,JPanels in the GTK+ look and feel are not initially opaque.to be safe, we invoke setOpaque on all JPanels used as content panes.
!Adding a Menu Bar (importation:all top-level containers can, in theory,have a menu bar.In practice,menu bars usually appear only in frames and perhaps in applets.)
   to add a menu bar to a top-level container,you create a JMenuBar object,populate it with menus,and then call setJMenuBar.
!the root pane(manages the content pane and the manu bar,along with a couple of other containers.if you need to intercept mouse clicks or paint over multiple components)
 a glimpse at the components that a root pane provides to a frame:
   !!content pane
   !!the optional menu bar
   !!layered pane(directly contains the menu bar and content pane,and enables Z-ordering of other components you might add.)
   !!glass pane(used to intercept input events occuring over the top-level container,and can also be used to paint over multiple components)
Using Models(be owned by most noncontainer Swing components,and stored the the component's state)
  !!benifits:
   !!!flexibility in determining how data is stored and retrieved:example,spreadsheet
   !!!they mean that data isn't copied between a program's data structures and those of the Swing components.
   !!!automatically propagate changes to all intersted listeners,making if easy for the GUI to stay in sync with the data.
  !!an example:converter
  !!kinds of models:
   !!!BoundedRangeModel
   !!!table data models
   !!!color chooser's selection model
   !!!DefaultTreeModel
   !!!DefaultListModel
   !!!SharedDataModel
   !!!DefaultListModel
   !!!spinner models
   !!!slider models
 
  
 

你可能感兴趣的:(Using Swing Components(1))