Download presentation
Presentation is loading. Please wait.
Published byLucinda Dawson Modified over 8 years ago
1
Timer class and inner classes
2
Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate an action event periodically When a timer event takes place, timer calls actionPerformed(ActionEvent event) part of the ActionListener interface (java.awt.event) Example: TimerTester.java
3
Inner classes Inner class Any class defined inside another is available to all the methods of the enclosing class Can access the members of the enclosing class Can reference the outer class using ( Outerclass.this )
4
Anonymous inner classes Anonymous classes Often encountered within a method Combines the creation of an object with the definition Requires the inclusion of a semi-colon marking the end of expression containing the anonymous class Objects defined inside the method but outside of class Must be final before used inside anonymous class
5
GUI-based Applications
6
Graphical Applications Java apps having graphical components Called GUI components GUI component An object representing a screen element Some are containers used to hold other components GUI related classes Defined in java.awt and javax.swing packages
7
GUI Containers Frame Used to display GUI-based Java application Displayed as a window with a title bar A heavyweight container Managed by the underlying operating system Panel Container that is not stand-alone Must be added to another container to be displayed Used to organize other components Lightweight container managed by the java program itself
8
Example: Authority Label GUI component displaying a line of text Used to display information or to identify other components Let us look at a program Organizing two labels in a panel And displaying panel in a frame
9
Layout Management An object That determines the way components Are arranged in a container Several predefined managers Are provided by Java standard class library Three useful layout managers: Flow Layout Border Layout Grid Layout
10
Border Layout This manager groups container Into five areas: center, north, west, south and east Default manager for a frame To use it in a panel: panel.setLayout(new BorderLayout()); When adding a component: panel.add(component, BorderLayout.NORTH);
11
Grid Layout This manager groups components into a grid with a fixed number of rows and columns Adds the components, row by row, left to right JPanel numberPanel = new JPanel(); numberPanel.setLayout(new GridLayout(4, 3)); numberPanel.add(button7); numberPanel.add(button8); numberPanel.add(button9); numberPanel.add(button4);...
12
GUI Interfaces GUI revolves around Components Events Listeners Event Represents some activity to which we may want to respond May be generated by a graphical component Listener Defines what happens when an event occurs
13
© 2004 Pearson Addison-Wesley. All rights reserved 4-13 Events and Listeners Component A component object may generate an event Listener A corresponding listener object is designed to respond to the event Event When the event occurs, the component calls the appropriate method of the listener, passing an object that describes the event
14
Example: PushCounter A push button Allows the user to initiate an action By pressing a graphical button using the mouse is defined by JButton class The pushcounter example Displays a push button Increments a counter each time a button is pushed
15
Determining Event Sources One listener Can be used to listen to two different components In this case, The source of the event can be determined by Using the getSource method of the event received by listener Example label + two buttons With label indicating which of the 2 buttons is pressed
16
Example: TextFields A text field Allows the user to enter one line of input Generates an event when the enter key is pressed Fahrenheit.java Instantiates a listener and adds it to text field When user enters temperature in Fahrenheit The conversion into Celsius is performed
17
Simple GUI-based Input/Output with JOptionPane Most applications use windows or dialog boxes To interact with the user JOptionPane provides prebuilt dialog boxes for input/output, displayed via static methods: showInputDialog Displays an input dialog that collects input data from user showMessageDialog Displays an output message dialog to the user showConfirmDialog Displays a dialog box with a yes/no question to the user
18
Icon Types
19
Image Icons
20
Images Images are often used in a program with a graphical interface As we have seen, a JLabel object can be used To display a line of text It can also be used to display an image That is, a label can be composed of text, and image Or both at the same time
21
Images (Cont’d) The ImageIcon class Can be used to represent an image that is stored in a label The position of the text relative to the image Can be set explicitly The alignment of text and image within label Can be set as well Refer to ImageIconDemo project
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.