Keele University Programming II (P2) Lecture 3 - Java Layout Managers PDF
Document Details
Uploaded by Deleted User
Keele University
Dr Charles Day
Tags
Summary
This Keele University lecture covers Java GUI programming, specifically layout managers. It discusses different types of layout managers and how to use them to structure graphical user interfaces, along with events related to mouse clicks. The lecture materials are likely beneficial to students enrolled in computer science or related programs focusing on GUI programming.
Full Transcript
Keele University School of Computing & Mathematics Programming II (P2): Data Structures & Algorithms Dr Charles Day Lecture 3 – Layout Managers (reprise) & GUI Events to update Object State The Java Layout Managers The follow...
Keele University School of Computing & Mathematics Programming II (P2): Data Structures & Algorithms Dr Charles Day Lecture 3 – Layout Managers (reprise) & GUI Events to update Object State The Java Layout Managers The following Layout Manager classes are available in Java: FlowLayout BorderLayout GridLayout BoxLayout CardLayout GridBagLayout GroupLayout SpringLayout “Nesting” Layout Management So far we have seen examples of a single layout manager assigned to a window (JFrame) object. Complex layouts in a window can be achieved by nesting different layout managers in panels (JPanel) within the window. More than one level of nesting is possible but not common practice. Layout Managers in GUIs The following page is an excellent resource to help you understand and use the Java layout managers: http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html Changing the Size of Components Almost all GUI components have a setPreferredSize method that can be used to change their size.... JButton aButton = new JButton(“Exit”); aButton.setPreferredSize(new Dimension(50, 30)); myWindow.add(aButton);... Note however that the layout manager has the final say on the size of the components. Some (or sometimes) layout managers will completely ignore your “preferred size”. GUI events End-user interaction with Java programs via a Graphical User Interface (GUI) can take many forms: button clicks mouse-drags slider-dragging typing into text-fields window resizing etc. etc. (depending upon precisely which GUI-components a program makes available of course) For each of these modes of interaction the developers of the Java system have identified a complex, but well defined, set of events Each of these events can result in a structured transition of the GUI component from its current state to a new state GUI events Not all Java programs are GUI based, so if we want to have access to the structured set of events that have been defined for GUI components we must import the correct package of classes (actually sub-packages of the Java AWT and Swing packages) into our Java program e.g. import java.awt.event.*; import javax.swing.event.*; For a full account of the many different forms of event provided by this package consult the Java documentation. For example, general mouse interactions result in the generation of MouseEvents - which can then be determined as being from one of the following detailed mouse-operations: mousePressed, mouseReleased, mouseEntered, mouseExited, mouseClicked GUI event listeners – step 1 Importing the AWT/Swing event sub-packages gives our programs access to the broad family of possible GUI events If our program has, say, check-boxes in its GUI then we need our program to be sensitive to ChangeEvents from checkBox-clicks The way we tell the JRE exactly which kinds of events our program is expecting to handle is by declaring that it will listen to any events of the specified type sent to it by the JRE. One of the neatest ways of providing this event-listening facility is by declaring an inner class that implements each of the pre- defined transitions that need to be dealt with (e.g. from events such as MouseEvents etc) as specified by that listener’s interface GUI event listeners – step 2 Carrying out step 1, previous slide, provides a suitable inner-class to implement a particular listener interface Now we need to attach one of these listener object instances to the GUI component that generates those events this is done by adding the listener object to the particular GUI component. For example for a JCheckBox on the GUI we might say an anonymous object instance... fineCheckBox.addChangeListener( new GridCheckBoxesListener() ); reference variable Constructor for the inner class that for the GUI has been written as the listener: able component that is to process the events it receives the source of the events Mouse Events The mouse causes a MouseEvent whenever: It is moved over a Java GUI component. It is dragged (i.e. a move with a mouse button held down). It is clicked or double-clicked. One of its buttons is pressed or released. etc. Once again, you can specify that a particular GUI object is to listen for any of the above events in order to do something about it. The mouse event (which is a Java object itself) will contain information about the state of the mouse when the event occurred. It is very likely that you will need this information when you process the event – you can see what information is available by looking at a MouseEvent’s collection of ‘getter’ methods in the Java API documentation. Mouse Event Listeners In order to capture mouse events (just as with any other events) we use a listener interface. Common event listeners related to mouse events (with the operations listed in their interface) are: MouseListener mouseClicked(MouseEvent) mouseEntered(MouseEvent) Precise set of operations is defined mousePressed(MouseEvent) by the specific Listener’s interface mouseReleased(MouseEvent) MouseWheelListener mouseWheelMoved(MouseWheelEvent) MouseMotionListener mouseDragged(MouseEvent) mouseMoved(MouseEvent) Encapsulation – Inner Classes The DrawingApplication program that we will be working on throughout P2 will make extensive use of inner classes. Inner classes: can be declared inside the scope (but outside of any methods) of an enclosing class (look at the MyMouseMotionListenerClass in class DrawingApplication, see later slides) objects of such a class can then be used by any methods of the enclosing class Or, can be declared inside the scope of a particular method (this is a very important technique for GUI-based Java programs). objects of such a class can then access local variables (declared as final) from the enclosing method in our DrawingApplication we will often use ‘anonymous’ object instances of such inner classes to listen for GUI events. Inner Classes & Encapsulation The code written inside an inner class will have access to the local variables of the surrounding scope providing that those variables have been declared as final. This ensures a good OO design Capitalise on the benefits of encapsulation without being compromised by the need to have GUI components that are sensitive to state-changing end-user interactions. Next week’s practicals will get you started with listeners for MouseEvents, and ChangeEvents (i.e. the type of events that arise from mouse clicks on JCheckBox objects – display the different drawing grids in our DrawingApplication) DrawingApplication class – the big picture The overall structure of the DrawingApplication class that is recommended is as follows: public class DrawingApplication extends JFrame All the Java statements that specify { // constants declared around here... the structure of our GUI and the class Canvas extends JPanel layout of its components: menus, { } // end class Canvas control-panel, drawing-canvas, error- message area etc. // instance variables declared around here... public DrawingApplication() { Also has additional statements to } // end constructor create object-instances of relevant listener classes and then hook-them- up-to the GUI component that they private void draw(Graphics g) should listen to { } // end draw method This is a good region of the DrawingApplication in which to write public static void main(String args[]) { the source code for the various } // end main method listener classes that we want to use to } // end class DrawingApplication respond to GUI events