Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Swing Chapter 14 Menus Making GUIs Pretty (and More Functional)

Similar presentations


Presentation on theme: "More Swing Chapter 14 Menus Making GUIs Pretty (and More Functional)"— Presentation transcript:

1 More Swing Chapter 14 Menus Making GUIs Pretty (and More Functional)
Box Containers and Box Layout Managers More on Events and Listeners Another Look at the Swing Class Hierarchy

2 Menus Three Swing classes used to put a menu in a program: JMenuBar
JMenuItem Menu items behave in the same way as buttons AbstractButton JButton JMenu JMenuItem

3 A GUI with a Menu JMenu memoMenu = new JMenu("Memos"); JMenuItem m;
m = new JMenuItem("Save Memo 1"); m.addActionListener(this); memoMenu.add(m); m = new JMenuItem("Save Memo 2"); . . . JMenuBar mBar = new JMenuBar(); mBar.add(memoMenu); setJMenuBar(mBar); Create a menu Create a menu item A menu item uses an action listener the same way a button does.

4 A GUI with a Menu JMenu memoMenu = new JMenu("Memos"); JMenuItem m;
m = new JMenuItem("Save Memo 1"); m.addActionListener(this); memoMenu.add(m); m = new JMenuItem("Save Memo 2"); . . . JMenuBar mBar = new JMenuBar(); mBar.add(memoMenu); setJMenuBar(mBar); Each menu item is added to the menu. The menu is added to the menu bar. One way to add a menu bar to a JFrame

5 Nested Menus JMenu is a descendant of JMenuItem
Every JMenu object is also a JMenuItem A JMenu can be a menu item in another menu This allows nested menus Clicking on a nested menu shows the items in the nested menu and allows them to be selected. AbstractButton JButton JMenu JMenuItem

6 Making GUIs Pretty (and More Functional)
Adding Icons The JScrollPane Class for Scroll Bars Adding Borders Changing the Look and Feel

7 Using Icons Icons are (small) pictures
Icons may be added to labels, buttons, and menu items. The ImageIcon class can be used to convert a picture to an icon: ImageIcon smileyFaceIcon = new ImageIcon(“smiley.gif”); The setIcon method can be used to add an icon to a component: JLabel helloLabel = new JLabel(“Hello”); ImageIcon dukeWavingIcon = new ImageIcon(“duke_waving.gif”); helloLabel.setIcon(dukeWavingIcon);

8 The JScrollPane Class for Scroll Bars
A view port is used when not all information can be displayed on screen at once. Scroll bars move a view port around to show different parts of the information. JScrollPane is a class that can provide a view port with scroll bars. An example using JScrollPane with a JTextArea called theText and a JPanel called textPanel: JScrollPane scrolledText = new JScrollPane(theText); textPanel.add(scrolledText);

9 Adding Borders A border is an area that frames a component.
Swing provides several different types of borders: BevelBorder—makes component look raised or lowered EtchedBorder—similar to BevelBorder but can’t set size EmptyBorder—extra space around the component LineBorder—colored border of a given thickness MatteBorder—similar to LineBorder but can adjust thickness on each side of the component An example of adding a bevel border to a button: testButton.setBorder(new BevelBorder(BevelBorder.LOWERED));

10 Box Layout Manager Useful for a single column or single row of components Specify X_AXIS (horizontal) or Y_AXIS (vertical) layout as second parameter to constructor for layout manager Provides a means of separating components in a row or column Strut—allocates a fixed amount of space between two components Glue—allocates a variable amount of space between two components A Box container is a container that is automatically given a BoxLayout manager.

11 Box Layout Versus Other Layouts
Horizontal box layout is similar to flow layout. Vertical box layout is similar to grid layout with only one column. Big advantage of box layout is control over spacing using struts and glue. Note that struts and glue should not be used with other layout managers.

12 Box Layout Demo Program
Specifies a horizontal layout JPanel horizontalPanel = new JPanel(); horizontalPanel.setLayout( new BoxLayout(horizontalPanel, BoxLayout.X_AXIS)); Component horizontalStrut = Box.createHorizontalStrut(HORIZONTAL_STRUT_SIZE); horizontalPanel.add(horizontalStrut); JButton hStopButton = new JButton("Red"); hStopButton.addActionListener(this); horizontalPanel.add(hStopButton); Static method in Box class used to create a strut of a particular size for spacing

13 Struts and Glue invisible components used to add space between visible components horizontal strut: programmer specifies width, which layout manager does not change vertical strut: programmer specifies height, which layout manager does not change glue: no specified size can be added to layout to specify where extra space should go when container grows

14 Setting the Spacing Between Components
To control spacing with layouts other than the Box layout, use setHgap and setVgap: public void setHgap(int hgap) sets the horizontal gap between components argument is size of gap in pixels public void setVgap(int vgap) sets the vertical gap between components Can also use EmptyBorder in any layout manager which will add space as part of a component Most layout managers also have a constructor with hgap and vgap parameters

15 The Box Container Class
A Box object works like a panel with a BoxLayout manager. Created using static methods: Box horizontalBox = Box.createHorizontalBox(); Box verticalBox = Box.createVerticalBox(); Automatically given a BoxLayout manager when created You should not use setLayout method with a box object.

16 The CardLayout Manager
Allows a set of views (components) to choose among Only one view is visible at a time. Can go through views in order or jump to any view. Often the components added to a CardLayout will be panels. Each component added to a CardLayout has a string associated with it that works like a name: deckPanel.add("start", startCardPanel); The string can be used later to display that component (or view): dealer.show(deckPanel, "start"); Need reference to layout manager to change views, so do not use anonymous object: deckPanel.setLayout(new CardLayout()); legal but useless

17 CardLayoutDemo Only one of these three panels will be visible at a time. deckPanel = new JPanel(); dealer = new CardLayout(); deckPanel.setLayout(dealer); ... deckPanel.add("start", startCardPanel); deckPanel.add("green", greenCardPanel); deckPanel.add("red", redCardPanel); dealer.show(deckPanel, "red"); dealer.next(deckPanel); will show redCardPanel if redCardPanel is currently displayed, will show startCardPanel

18 Inner Classes An inner class is a class defined within another class.
Advantages: They make the outer class more self contained. If WindowDestroyer is used, must make sure that class is available. If InnerDestroyer (inner class version of WindowDestroyer) is used, it will always be available. Inner class has access to all instance variables and methods of outer class, including private ones. Avoid name conflicts. You could have another (outer) class called InnerDestroyer and there would not be a conflict.

19 The WindowListener Interface
For a class to be a listener for window events, it must implement the WindowListener interface. By implementing the WindowListener interface, a window can be its own listener. The advantage of making a window its own listener is that it is easy to call methods from the listener since they are in the same object.

20 The WindowListener Interface
Implementation of the WindowListener interface requires these seven methods to be defined: public void windowOpened(WindowEvent e) public void windowClosing(WindowEvent e) public void windowClosed(WindowEvent e) public void windowIconified(WindowEvent e) public void windowDeiconified(WindowEvent e) public void windowActivated(WindowEvent e) public void windowDeactivated(WindowEvent e) If a method will be not be used, it should be defined with an empty body WindowAdapter is a class that implements all seven methods of the WindowListener with empty bodies.

21 Programming the Close-Window Button
The WindowListener interface can be used to program the close-window button. If the close-window button is not programmed, by default it will close the window but not exit the program. For a window that does not close when the close-window button is clicked, use a method call like this: setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE); The CloseWindowDemo uses this method call When the close-window button is clicked, the program displays a confirmation dialog instead of closing the window.

22 CloseWindowDemo Program
Prevents window from closing so that user can confirm or cancel before window is closed. public CloseWindowDemo() { setSize(WIDTH, HEIGHT); setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE); addWindowListener(new InnerDestroyer()); setTitle("Close Window Demo"); Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); . . . } Defined on next slide Constructor for the CloseWindowDemo class, which inherits from JFrame.

23 CloseWindowDemo Program
Definition of inner class used as listener for the CloseWindowDemo class. Inherits from WindowAdapter so it does not have to define all seven window event methods. private class InnerDestroyer extends WindowAdapter { public void windowClosing(WindowEvent e) ConfirmWindow askWindow = new ConfirmWindow(); askWindow.setVisible(true); } ConfirmWindow closes window and exits program if user confirms close.

24 CloseWindowDemo Program
actionPerformed method from the ConfirmWindow inner class The main window will only be closed if the user clicks the “Yes” button in the ConfirmWindow public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Yes")) System.exit(0); else if (e.getActionCommand().equals("No")) dispose(); //Destroys only the ConfirmWindow. else System.out.println("Error in Confirm Window."); }

25 Changing Components A program can add or remove components after a GUI has been displayed, but that is beyond the scope of the book. Making components visible or not visible gives a similar effect. The setVisible method is used in the VisibleDemo program to make only one of the red and green labels visible at a time. (code on next slide)

26 Changing Components The actionPerformed method from the VisibleDemo program public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals(“Red”)) colorPanel.setBackground(Color.red); stopLabel.setVisible(false); goLabel.setVisible(true); validate(); } . . . There is similar code for when the Green button is pressed, which turns the background green and hides the go label. Visibility changes won’t occur until the validate method is called.

27 Another Look at the Swing Class Hierarchy
JComponent JLabel JMenuBar AbstractButton JButton JMenu JMenuItem Swing Class Abstract Class All of the basic properties of JButton and JMenuItem are inherited from AbstractButton. JButton and JMenuItem are similar because they are derived from the same abstract class. Since AbstractButton is an abstract class, no objects of that class can be made. The purpose of the AbstractButton class is to provide a place for code that is common to JButton and JMenuItem and avoid repeated code.

28 Another Look at the Swing Class Hierarchy
JComponent JLabel JMenuBar AbstractButton JButton JMenu JMenuItem Swing Class Abstract Class JLabel and JButton inherit from a common ancestor, namely JComponent, so they have some similarities. Notice, however, that JLabel and JButton are not derived from the same class, even though they have a common ancestor. The hierarchy reflects the fact that JButton and JMenuItem are more similar than JLabel and JButton. Also notice that JMenu inherits from JMenuItem, so it can be used anywhere a JMenuItem can. This allows nested menus.

29 Summary You can add icons to JButtons, JLabels, and JMenuItems.
A JMenuBar can be added to a JFrame with the method setJMenuBar or with the usual add method. Both buttons and menu items fire action events and so should have an ActionListener registered with them. You can use the class JScrollPane to add scroll bars to a text area. You can define a window listener class by having it implement the WindowListener interface. If you want a close-button to do something other than close the window, you must use SetDefaultCloseOperation. If you change the visibility of a component you should use the validate method to update the GUI.


Download ppt "More Swing Chapter 14 Menus Making GUIs Pretty (and More Functional)"

Similar presentations


Ads by Google