Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Go (programming language) wikipedia , lookup
Reactive programming wikipedia , lookup
Falcon (programming language) wikipedia , lookup
Abstraction (computer science) wikipedia , lookup
Class (computer programming) wikipedia , lookup
Name mangling wikipedia , lookup
C Sharp syntax wikipedia , lookup
Java (programming language) wikipedia , lookup
Object-oriented programming wikipedia , lookup
Structured programming wikipedia , lookup
Chapter 14 More Swing Objects 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 Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Menus Three Swing classes used to put a menu in a program: AbstractButton » JMenuBar » JMenu JMenuItem » JMenuItem JButton JMenu Chapter 14 Menu items behave in the same way as buttons Java: an Introduction to Computer Science & Programming - Walter Savitch 2 A GUI with a Menu JMenu memoMenu = new JMenu("Memos"); JMenuItem m; Create a menu m = new JMenuItem("Save Memo 1"); m.addActionListener(this); memoMenu.add(m); Create a menu item m = new JMenuItem("Save Memo 2"); m.addActionListener(this); A menu item uses an memoMenu.add(m); action listener the same . . . JMenuBar mBar = new JMenuBar(); way a button does. mBar.add(memoMenu); setJMenuBar(mBar); Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch 3 A GUI with a Menu JMenu memoMenu = new JMenu("Memos"); JMenuItem m; m = new JMenuItem("Save Memo 1"); Each menu item is added m.addActionListener(this); to the menu. memoMenu.add(m); m = new JMenuItem("Save Memo 2"); m.addActionListener(this); The menu is added to the memoMenu.add(m); menu bar. . . . JMenuBar mBar = new JMenuBar(); mBar.add(memoMenu); setJMenuBar(mBar); One way to add a menu bar to a JFrame Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch 4 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 JMenuItem JButton JMenu Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch 5 Making GUIs Pretty (and More Functional) Chapter 14 Adding Icons The JScrollPane Class for Scroll Bars Adding Borders Changing the Look and Feel Java: an Introduction to Computer Science & Programming - Walter Savitch 6 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); Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch 7 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); Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch 8 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)); Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch 9 Changing the Look and Feel Chapter 14 Look and feel refers to the general appearance of the GUI, including: » Shape and exact placement of buttons » Default colors Three standard choices for look and feel: » Metal—considered the standard Java look and feel » Motif—often considered the standard Unix look and feel » Windows—looks like the windows you get with the Windows operating system A Macintosh look and feel is also available Java: an Introduction to Computer Science & Programming - Walter Savitch 10 An Example of Changing the Look and Feel of a GUI try Fully qualified class name— { includes directory path UIManager.setLookAndFeel( “com.sun.java.swing.plaf.motif.MotifLookAndFeel”); SwingUtilities.updateComponentTreeUI(this); } Try and catch necessary catch (Exception e) because any one of four { exceptions could be thrown System.out.println( “Could not load the Motif look and feel”); } Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch 11 Box Layout Manager Chapter 14 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. Java: an Introduction to Computer Science & Programming - Walter Savitch 12 Box Layout Versus Other Layouts Chapter 14 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. Java: an Introduction to Computer Science & Programming - Walter Savitch 13 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 Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch 14 The WindowListener Interface Chapter 14 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. Java: an Introduction to Computer Science & Programming - Walter Savitch 15 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. Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch 16 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); Chapter 14 The CloseWindowDemo uses this method call When the close-window button is clicked, the program displays a confirmation dialog instead of closing the window. Java: an Introduction to Computer Science & Programming - Walter Savitch 17 CloseWindowDemo Program Prevents window from closing so public CloseWindowDemo() that user can confirm or cancel { before window is closed. setSize(WIDTH, HEIGHT); setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE); addWindowListener(new InnerDestroyer()); setTitle("Close Window Demo"); Defined Container contentPane = getContentPane(); on next contentPane.setLayout(new BorderLayout()); slide . . . } Constructor for the CloseWindowDemo class, which inherits from JFrame. Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch 18 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. Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch 19 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."); } Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch 20 Changing Components Chapter 14 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) Java: an Introduction to Computer Science & Programming - Walter Savitch 21 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); There is similar code for validate(); when the Green button is Visibility changes won’t pressed, which turns the } occur until the validate background green and . . . method is called. hides the go label. } Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch 22 Another Look at the Swing Class Hierarchy JComponent Swing AbstractButton JLabel JMenuBar JMenuItem Abstract Class Class JButton JMenu 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. Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch 23 Another Look at the Swing Class Hierarchy JComponent Swing AbstractButton JLabel JMenuBar JMenuItem Abstract Class Class JButton JMenu 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. Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch 24 Summary Chapter 14 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. When you define a GUI using Swing you can specify the look and feel for the GUI. 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. Java: an Introduction to Computer Science & Programming - Walter Savitch 25