1.24k likes | 1.59k Vues
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.
E N D
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 • Menus • Making GUIs Pretty and More Functional • More Layout Managers • Inner Classes • More on Events and Listeners • The Swing Class Hierarchy Reconsidered
Menus: Outline • Programming Example: A GUI with a Menu • Menu Bars, Menus, and Menu Items • Nested Menus
Programming Example: A GUI with a Menu • class MemoGUI
Programming Example: A GUI with a Menu, cont. • class MemoGUI, cont.
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.
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);
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.
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);
Menu Bars, cont. • Alternatively, a menu bar can be added to the content pane of a JFrame or other container.
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);
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.
Making GUIs Pretty and More Functional: Outline • Adding Icons • The JScrollPane Class for Scroll Bars • Adding Borders
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.
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.
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.
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);
Placing an Icon and a String on a Label (or Button), cont. class IconDemo
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)
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)
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)
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))
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))
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))
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))
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
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
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.
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)
Classes Dimension and Inset, cont. • examples aButton.setMargin(new Insets (10, 20, 10, 20)); aLabel.setPreferredSize (new Dimension (20, 50));
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.
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.
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.
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);
The JScrollPane Class for Scroll Bars, cont. • class ScrollBarDemo
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.
Some Methods and Constants in Class JScrollBar • to create a new JScrollPane for the objectToBeScrolled public JScrollPane(Component objectToBeScrolled)
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
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
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
Adding Borders, cont. • to use the border classes import javax.swing.border.* • to provide a border JComponent.setBorder(Border_Object);
Adding Borders, cont. • class BorderDemo
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));
Some Border Classes • to create a BevelBorder object public BevelBorder(int bevelType) where bevelType is one of BevelBorder.RAISED BevelBorder.LOWERED