1 / 122

More Swing

More Swing. Chapter 14. Objectives. learn to add menus, icons, borders, and scroll bars to GUIs learn to use the BoxLayout manager and the Box class learn about inner classes learn about the WindowListener interface learn how to change GUI components to visible or invisible. Outline.

carina
Download Presentation

More Swing

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. More Swing Chapter 14

  2. Objectives • learn to add menus, icons, borders, and scroll bars to GUIs • learn to use the BoxLayout manager and the Box class • learn about inner classes • learn about the WindowListener interface • learn how to change GUI components to visible or invisible

  3. Outline • Menus • Making GUIs Pretty and More Functional • More Layout Managers • Inner Classes • More on Events and Listeners • The Swing Class Hierarchy Reconsidered

  4. Menus: Outline • Programming Example: A GUI with a Menu • Menu Bars, Menus, and Menu Items • Nested Menus

  5. Programming Example: A GUI with a Menu • class MemoGUI

  6. Programming Example: A GUI with a Menu, cont. • class MemoGUI, cont.

  7. Programming Example: A GUI with a Menu, cont.

  8. Menu Bars, Menus, and Menu Items • You add menus using three Swing classes: • JMenuBar • JMenu • JMenuItem • JMenuItems are placed in a JMenu, and a JMenu typically is placed in a JMenuBar. • By default, an object of class JMenuItem is identified by the string that labels it.

  9. Menu Bars, Menus, and Menu Items, cont. • Using method add, you can add as many JMenuItems as you wish to a menu. • example JMenu_Name.add(JMenu_Item); • The menu lists them in the order in which they are added. • Listeners are added using JMenu_Item_Name.addActionListener (Action_Listener);

  10. Menu Bars, Menus, and Menu Items, cont. • Method actionPerformed is defined for menu items the same way it is defined for buttons. • A menu in our example includes an additional entry labeled Exit.

  11. Menu Bars • A menu bar is a container for a menu. • Typically it is placed near the top of a windowing interface. • Menus are added to the menu bar using JMenu_Bar_Name.add(JMenu_Name); • A menu bar can be added to a JFrame using setJMenuBar(JMenu_Bar_Name);

  12. Menu Bars, cont. • Alternatively, a menu bar can be added to the content pane of a JFrame or other container.

  13. Setting the Action Command for a Menu Item • If you do not wish to use the text for a JMenuItem as the default action command, you can set the action command using Menu_Item_Object.setActionCommand (Action_Command_String);

  14. Nested Menus • Class JMenu descends from class JMenuItem, so every JMenu object is also a JMenuItem object. • Thus, a JMenu can be a menu item in another menu, permitting menus to be nested.

  15. Making GUIs Pretty and More Functional: Outline • Adding Icons • The JScrollPane Class for Scroll Bars • Adding Borders

  16. Adding Icons • Typically, an icon is simply a small picture. • Labels, buttons, menu items, and other components can have icons. • A label or button can have just a string, just an icon, both, or neither. • A picture in almost any standard format can be used as the basis for an icon.

  17. Converting a Picture to a Swing Icon • You use class ImageIcon to convert a picture file to a Swing Icon. • example ImageIcon dukeWavingIcon = new ImageIcon(“duke_waving.gif”); • You can use a relative or complete path name to specify the picture file.

  18. Adding an Icon to a Label and a Button • example, cont. JLabel dukePicture = new JLabel(dukeWavingIcon); • To produce a button with just an icon on it, you use JButton dukeButton = new JButton(dukeWavingIcon); • setActionCommand should be used explicitly to give the button an action command.

  19. Placing an Icon and a String on a Label (or Button) • example JButton helloButton = new JButton(“Hello”); ImageIcon dukeWavingIcon = new ImageIcon(“dukeWaving.gif”); helloButton.setIcon(dukeWavingIcon);

  20. Placing an Icon and a String on a Label (or Button), cont. class IconDemo

  21. Placing an Icon and a String on a Label (or Button), cont.

  22. Some Methods in the Classes JButton and JLabel • to create a button or label with no text and no icon public JButton() public JLabel() • to create a button or label with text public JButton(String text) public JLabel(String text)

  23. Some Methods in the Classes JButton and JLabel, cont. • to create a button or label with text public JButton(ImageIcon Picture) public JLabel(ImageIcon Picture) • to create a button or label with both text and an icon public JButton(String text, ImageIcon Picture) public JLabel(String text, ImageIcon Picture, int horizontalAlignment)

  24. Some Methods in the Classes JButton and JLabel, cont. • to make text the only text on the button or label public void setText(String text) • to make picture the only icon on the button or label public void setIcon(ImageIcon picture)

  25. Some Methods in the Classes JButton and JLabel, cont. • to set the size of the margin around the text and icon in the button (but not the label) public void setMargin(Insets margin) or public void setMargin (new Insets( int top, int left, int bottom, int right))

  26. Some Methods in the Classes JButton and JLabel, cont. • to set the preferred size of the button of label public void setPreferredSize( Dimension(preferredSize) or public void setPreferredSize( new Dimension(int width, int height))

  27. Some Methods in the Classes JButton and JLabel, cont. • to set the maximum size of the button of label public void setMaximumSize( Dimension(maximumSize) or public void setMaximumSize( new Dimension(int width, int height))

  28. Some Methods in the Classes JButton and JLabel, cont. • to set the minimum size of the button of label public void setMinimumSize( Dimension(minimumSize) or public void setMinimumSize( new Dimension(int width, int height))

  29. Some Methods in the Classes JButton and JLabel, cont. • to set the vertical position of the text relative to the icon public void setVerticalTextPosition (int textPosition) where textPosition is one of the constants SwingConstants.TOP SwingConstants.CENTER (default) SwingContants.BOTTOM

  30. Some Methods in the Classes JButton and JLabel, cont. • to set the horizontal position of the text relative to the icon public void setHorizontalTextPosition (int textPosition) where textPosition is one of the constants SwingConstants.RIGHT SwingConstants.LEFT SwingConstants.CENTER SwingContants.LEADING SwingConstants.TRAILING

  31. Resizing Buttons • The methods for setting the preferred, maximum, and minimum sizes are only recommendations to the layout manager. • An image may be clipped if the icon is too big.

  32. Classes Dimension and Inset • Objects of classes Dimension and Inset are used with buttons, labels, and other objects. • constructors Insets (int top, int left, int bottom, int right) Dimension(int width, int height)

  33. Classes Dimension and Inset, cont. • examples aButton.setMargin(new Insets (10, 20, 10, 20)); aLabel.setPreferredSize (new Dimension (20, 50));

  34. The JScrollPane Class for Scroll Bars • When you create a text area, you specify the number of lines that are visible and the number of characters per line. • example JTextArea the Text = new JTextArea(10,40;) • It might be better not to limit the number of lines and the number of characters per line.

  35. The JScrollPane Class for Scroll Bars, cont. • This can be accommodated using scroll bars along the sides of the “window” or view port that shows only a selected portion of the text. • The view port functions as a “cut out” over an unbounded document.

  36. The JScrollPane Class for Scroll Bars, cont.

  37. The JScrollPane Class for Scroll Bars, cont. • Scroll bars can be provided using class JScrollPane. • An object of class JScrollPane is essentially a view port with scroll bars.

  38. The JScrollPane Class for Scroll Bars, cont. • The text area is provided as an argument to the JScrollPane constructor. • example JScrollPane scrolledText = new JScrollPane(theText); • A JScrollPane can be added to a container such as a JPanel or a JFrame. • example textPanel.add(scrolledText);

  39. The JScrollPane Class for Scroll Bars, cont. • class ScrollBarDemo

  40. The JScrollPane Class for Scroll Bars, cont.

  41. Scroll Bar Policy • If you omit the invocation of the methods setHorizontalScrollBarPolicy and setVerticalScrollBarPolicy, the scroll bars will be visible only when you need them.

  42. Some Methods and Constants in Class JScrollBar • to create a new JScrollPane for the objectToBeScrolled public JScrollPane(Component objectToBeScrolled)

  43. Some Methods and Constants in Class JScrollBar, cont. • To set the policy for showing the horizontal scroll bar public void setHorizontalScrollBarPolicy(int policy) wherepolicyis one of JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS JScrollPane.HORIZONTAL_SCROLLBAR_NEVER JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED

  44. Some Methods and Constants in Class JScrollBar, cont. • To set the policy for showing the vertical scroll bar public void setVerticalScrollBarPolicy(int policy) wherepolicyis one of JScrollPane.VERTICAL_SCROLLBAR_ALWAYS JScrollPane.VERTICAL_SCROLLBAR_NEVER JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED

  45. Adding Borders • A component is an area around the component that frames the component. • You can add a border to any JComponent. • A border can serve two purposes: • to make a component more attractive • to separate the component from other components

  46. Adding Borders, cont. • to use the border classes import javax.swing.border.* • to provide a border JComponent.setBorder(Border_Object);

  47. Adding Borders, cont. • class BorderDemo

  48. Adding Borders, cont.

  49. Adding Borders, cont. • You can place a border around any JComponent such as a JButton, a JLabel, a JPanel, or a JTextField. • It is common to use an anonymous argument for a border object. • example testButton.setBorder(new BevelBorder(BevelBorder.LOWERED));

  50. Some Border Classes • to create a BevelBorder object public BevelBorder(int bevelType) where bevelType is one of BevelBorder.RAISED BevelBorder.LOWERED

More Related