html5-img
1 / 87

Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas

C. ertificación en. J. AVA. Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas. Components in General The Visual Components The Container Components The Menu Components. 11. COMPONENTS. Objectives.

talen
Télécharger la présentation

Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C ertificación en J AVA Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas

  2. Components in General • The Visual Components • The Container Components • The Menu Components 11. COMPONENTS

  3. Objectives • Components are Java's building blocks for creating • graphical user interfaces. Some component types, • such as buttons and scroll bars, are used directly • for GUI control. Other kinds of components (those • that inherit from the abstract Container class) provide • spatial organization • GUIs are an important part of any program. Java's • Toolkit (AWT) provides extensive functionality. • This chapter reviews components

  4. Components in General Java's components are implemented by the many subclasses of the java.awt.Component and java.awt.MenuComponent superclasses There are 19 non-superclass components in all, and you should know the basics of all the component classes

  5. One way to organize this fairly large number of classes is to divide them into categories: • Visual components • Container components • Menu components

  6. There are several methods that are implemented by all the visual and container components, by virtue of inheritance from java.awt.Component (The menu components extend from java.awt.MenuComponent, so they do not inherit the same superclass functionality) getSize() returns the size of a component The return type is Dimension, which has public data members height and width

  7. setForeground() and setBackground(): set the foreground and background colors of a component Each method takes a single argument, which is an instance of java.awt.Color Chapter 12 discusses how to use the Color class. Generally the foreground color of a component is used for rendering text, and the background color is used for rendering the non-textual area of the component

  8. Example: a label blue foreground color and black background color will show up as blue text on a black background

  9. setFont(): The setFont() method determines the font that a component will use for rendering any text that it needs to display The method takes a single argument, which is an instance of java.awt.Font

  10. If you do not explicitly set a component's font, the component uses the font of its container, in the same way that the container's foreground and background colors are used if you do not explicitly call setForeground() or setBackground() Thus, if you have an applet whose font is 48-point bold Serif, and you add a check box to the applet without calling setFont() on the check box, you will get a check box whose label appears in 48-point bold Serif

  11. setEnabled(): takes a single argument of type boolean If this argument is true, then the component has its normal appearance If the argument is false, then the component is grayed out and does not respond to user input This method replaces the 1.0 methods enable() and disable(), which are deprecated

  12. setSize() and setBounds(): set a component's geometry, or rather, they attempt to set geometry. They replace the deprecated 1.0 methods resize() and reshape() setSize(): takes two int arguments: width and height an overloaded form takes a single dimension setBounds(): takes four int arguments: x, y, width, and height; an overloaded form takes a single rectangle

  13. If you have tried calling these methods, you know that it is usually futile: the size and position that you attempt to give a component is overruled by a layout manager In fact, these two methods exist mostly for the use of layout managers The major exception to this rule is the Frame class, which is not under the thumb of a layout manager and is perfectly willing to have you set its size or bounds

  14. setVisible(): takes a boolean argument that dictates whether the component is to be seen on the screen it only works for frames, unless you learn some techniques that are beyond the scope the Certification Exam

  15. The Visual Components • Button • Canvas • Checkbox • Choice • FileDialog • Label • List • ScrollPane • Scrollbar • TextArea • TextField

  16. To use one of the components in a GUI: • create an instance by calling the appropriate constructor • 2. add the component to a container

  17. Button implements a push button new Button( "Apply" ); This constructor takes a string parameter that specifies the text of the button's label When a button is pushed, it sends an Action event

  18. Applet Viewer: Visual.class Apply

  19. Canvas It is a component that has no default appearance or behavior You can subclass Canvas to create custom drawing regions, work areas, components, and so on Canvases receive input events from the mouse and the keyboard; it is up to the programmer to transform those inputs into a meaningful look and feel

  20. The default size of a canvas is uselessly small One way to deal with this problem is to use a layout manager that will resize the canvas Another way is to call setSize() on the canvas yourself; canvases are a rare case where this will actually work

  21. Canvas canv = new Canvas(); • canv.setBackground( Color.black ); • canv.setSize( 100, 50 ); Applet Viewer: Visual.class

  22. Checkbox A check box is a two-state button The two states are true (checked) and false (not checked)  Checkbox( String label ) Checkbox( String label, boolean initialState )

  23. Applet Viewer: Visual.class Use secure server 

  24. If you do not specify an initial state, the default is false Two methods support reading and setting the state of a check box: boolean getState() void setState( boolean state )

  25. CheckboxGroup cbg = new CheckboxGroup(); • add( new Checkbox( "Cinnamon", false, cbg ) ); • add( new Checkbox( "Nutmeg", false, cbg ) ); • add( new Checkbox( "All spice", true, cbg ) );  Applet Viewer: Visual.class Cinnamon Nutmeg Allspice

  26. Two methods support reading and setting the currently selected member of the group Checkbox getSelectedCheckbox() void setSelectedCheckbox (Checkbox newSelection) Check boxes send Item events when they are selected

  27. Choice A choice is a pull-down list • To create a choice: • call the constructor, • populate the choice by repeatedly calling addltem()

  28. Next figure shows two choices, both of which present the same options The choice on the left is in its normal state; the choice on the right has been mouse-clicked

  29. Choice ch = new Choice(); • ch.addltem( "Alligators" ); • ch.addltem( "Crocodiles" ); • ch.addItem( "Gila Monsters" ); • ch.addltem( "Dragons" ); Applet Viewer: Visual.class Gila Monsters 0 Alligators0 Alligators Crocodiles Gila Monsters Dragons Applet started.

  30. Choices send Item events when they are selected

  31. FileDialog The FileDialog class represents a file open or file save dialog The appearance of these dialogs varies greatly from platform to platform A file dialog is modal; this means that input from the dialog's parent frame will be directed exclusively to the dialog, as long as the dialog remains visible on the screen The dialog is automatically removed when the user specifies a file or clicks the Cancel button

  32.  The most useful FileDialog constructor   FileDialog(Frame parent, String title, int mode) The dialog's parent is the frame over which the dialog will appear The title string appears in the dialog's title bar (on most platforms) • The mode should be • FileDialog.LOAD or • FileDialog.SAVE

  33. After the user has specified a file, the name of the file or its directory can be retrieved:  String getFile() String getDirectory()

  34. FileDialog fidi = new FileDialog( f, "Choose!", FileDialog.LOAD ); • fidi.setVisible( true ); • System.out.println( fidi.getFile() );

  35. Label The simplest AWT component Labels do notrespond to user input, and they do not send out any events Constructors:  • Label() • Label( String text ) • Label( String text, int alignment )

  36. The default alignment for labels is to the left To set the alignment, use the third form of the constructor and pass in one of the following: • Label.LEFT • • Label.CENTER • • Label.RIGHT

  37.  Two methods support reading and setting the text of a label: String getText() void setText( String newText )

  38. new Label( "I'm a label, Mabel" ); Applet Viewer: Visual.class I'm a label, Mabel Applet started.

  39. List A list is a collection of text items, arranged vertically If a list contains more items than it can display, it acquires a vertical scroll bar Constructors: • List() • List( int nVisibleRows ) • List( int nVisibleRows, boolean bMultiSelectOk )

  40. The number of visible rows (parameter nVisibleRows) dictates the height of a list The first version of the constructor does not specify a number of visible rows, so presumably the height of such list will be dictated by a layout manager

  41.  If the version of the third constructor is used and multiSelectOk is true, then the list supports multiple selection If multiple selection is not enabled, then selecting a new item causes the old selected item to be deselected

  42. List list = new List( 4, true ); • list.addltem( "Augustus" ); • list. addltem( "Tiberius" ); • list.addltem( "Caligula" ); • list.addltem( "Claudius" ); • list.addltem( "Nero" ); • list.addltem( "Otho" ); • list.addItem( "Galba" ); Applet Viewer: Vi... Claudius Nero Otho Galba Applet started.

  43. The List class provides a large number of support methods • void addItem( String text ): • adds an item to the bottom of the list • void addItem( String text, int index ): • inserts an item at the specified index • String getltem( int index ): • returns the item with the specified index • int getltemCount(): • returns the number of items in the list • int getRows(): • returns the number of visible lines in the list

  44. int getSelectedlndex(): • returns the index of the currently selected item (the list should be in single-selection mode) • int[] getSelectedlndexes(): • returns an array containing the index of every currently selected item (the list should be in multiple-selection mode) • String getSelectedltem(): • returns a string that reflects the cur­rently selected item (the list should be in single-selection mode) • String[] getSelectedltems(): returns an array containing a string for every currently selected item (the list should be in multiple-selection mode)

  45. ScrollPane It was introduced in Java 1.1 A scroll pane can contain a single component, which may be taller or wider than the scroll pane itself If the contained component is larger than the scroll pane, then the default behavior of the scroll pane is to acquire horizontal and/or vertical scroll bars as needed

  46. Constructors: • ScrollPane(): • constructs a scroll pane with default scroll • bar behavior • ScrollPane( int scrollbarPolicy ): • constructs a scroll pane with the specified • scroll bar behavior

  47. If you use the second form of the constructor, then scrollbarPolicy should be one of: • ScrollPane.SCROLLBARS_AS_NEEDED • ScrollPane.SCROLLBARS_ALWAYS • ScrollPane.SCROLLBARS_NEVER 

  48. ScrollPane spane = new ScrollPane(); • Button bigButton = new Button( "What big teeth you have, Grandmother" ); • bigButton.setFont( new Font( "Serif", Font.ITALIC, 80 ) ); • spane.add( bigButton ); Applet Viewer: Visual class Applet What big teeth y Otho Applet started.

  49. Scrollbar The scroll bar component that adjusts lists and scroll panes is available as a component in its own right

  50. Constructors: 1. Scrollbar(): constructs a vertical scroll bar 2. Scrollbar( int orientation ): constructs a scroll bar with the specified orientation 3. Scrollbar( int orientation, int initialValue, int sliderSize, int minValue, int maxValue ): constructs a scroll bar with the specified parameters

More Related