[ Pobierz całość w formacie PDF ]
.The more complex the layoutof your applet, the more likely you're going to want to use nestedpanels.Nested PanelsPanels, as you've already learned, are components that can beactually displayed onscreen; Panel'ssuperclass Container providesthe generic behavior for holding other components inside it.TheApplet class, from whichyour applets all inherit, is a subclass of Panel.To nest other panels inside an applet, you merely create a newpanel and add it to the applet, just as you would add any otherUI component:setLayout(new GridLayout(1, 2, 10, 10));Panel panel1 = new Panel();Panel panel2 = new Panel();add(panel1);add(panel2);You can then set up an independent layout for those subpanelsand add AWT components to them (including still more subpanels)by calling the add() methodin the appropriate panel:panel1.setLayout(new FlowLayout());panel1.add(new Button("Up"));panel1.add(new Button("Down"));Although you can do all this in a single class, it's common ingraphical applets and applications that make heavy use of subpanelsto factor out the layout and behavior of the subpanels into separateclasses and to communicate between the panels by using methods.You'll look at an extensive example of this later in today's lessonin the section "A Complete Example: RGB-to-HSB Converter."Events and Nested PanelsWhen you create applets with nested panels, those panels forma hierarchy from the outermost panel (the applet, usually) tothe innermost UI component.This hierarchy is important to howeach component in the interface interacts with other components;for example, the component hierarchy determines the order in whichthose components are painted to the screen.More importantly, however, the hierarchy also affects event handling,particularly for user-input events such as mouse and keyboardevents.Events are received by the innermost component in the componenthierarchy and passed up the chain to the applet's panel (or tothe root window in Java applications).Suppose, for example, thatyou have an applet with a subpanel that can handle mouse events(using the mouseDown() andmouseUp() methods), and thatpanel contains a button.Clicking the button means that the buttonreceives the event before the panel does; if the button isn'tinterested in that mouseDown(),the event gets passed to the panel, which can then process itor pass it further up the hierarchy.Remember the discussion about the basic event methods yesterday?You learned that the basic event methods all return boolean values.Those boolean values become important when you're talking abouthandling events or passing them on.An event-handling method, whether it is the set of basic eventmethods or the more generic handleEvent(),can do one of three things, given any random event:Ignore the event entirely, if the event doesn't match whatevercriteria the event-handling method set-for example, the mouseDownwasn't in the right area, or the action wasn't a button action.If this is the case, the event handler should return falseso the event is passed up the hierarchy until a component processesit (or it is ignored altogether).Intercept the event, process it, and return true.In this case, the event stops with that event method.Intercept the method, process it, and pass it on to another,more specific event handler-for example, as handleEventpasses events onto mouseDown().More UI ComponentsAfter you master the basic UI components and how to add them topanels, organize their layout, and manage their events, you canadd more UI components.In this section, you'll learn about textareas, scrolling lists, scrollbars, and canvases.Note that most of the components in this section do not produceactions, so you can't use the action()method to handle their behavior.Instead, you have to use a generichandleEvent() method to testfor specific events that these UI components generate.You'lllearn more about this in the next section.Text AreasText areas are like text fields, except they have more functionalityfor handling large amounts of text.Because text fields are limitedin size and don't scroll, they are better for one-line responsesand simple data entry; text areas can be any given width and heightand have scrollbars by default, so you can deal with larger amountsof text more easily.Text areas are larger, scrollable text-entry components.Whereas text fields only provide one line of text, text areascan hold any amount of editable text.To create a text area, use one of the following constructors:TextArea() creates anempty text area 0 rows long and 0 characters wide (the text areawill be automatically resized based on the layout manager).TextArea(int, int) createsan empty text area with the given number of rows and columns (characters)
[ Pobierz całość w formacie PDF ]