Download presentation
Presentation is loading. Please wait.
Published byGavin Blake Modified over 9 years ago
1
CS1054: Lecture 21 - Graphical User Interface
2
Graphical User Interfaces vs. Text User Interface
3
Three steps to GUI Programming What kind of elements can we show on screen? Components How do we arrange those elements? Layout How do we react to user input? Event Handlers
4
AWT vs SWING Java has two GUI libraries – AWT and Swing Swing later and much improved than AWT Equivalent classes in Swing can be identified by a J at the start of class name.
5
Top level Window In control of Operating System’s window management Java word – Frames Swing calls - JFrame
6
JFrame Windows Title bar Menu bar Content pane
7
Adding Simple Components JLabel frame.pack( ); frame.setVisible (true);
8
Adding Menus JMenuBar JMenu JMenuItem
9
Creating Menubars – 3 steps JMenuBar menubar = new JMenuBar ( ); frame.setJMenuBar ( menubar ); JMenu fileMenu = new JMenu ( “File”); menubar.add (fileMenu); JMenuItem openItem = new JMenuItem (“Open”); fileMenu.add (openItem); JMenuItem quitItem = new JMenuItem (“Quit”); fileMenu.add (quitItem);
10
Event Handling ActionEvent MouseEvent WindowEvent
11
To receive events 1. import java.awt.event.*; 2. To class header ActionListener interface 3. public void actionPerformed (ActionEvent event) 4. The objects must call addActionListener ( ) to register as a listener
12
Add ActionListener public class ImageViewer implements ActionListener public void actionPerformed (ActionEvent event) { System.out.println ("Item: " + event.getActionCommand ( )); } openItem.addActionListener( this); quitItem.addActionListener( this);
13
Layout Managers - Arrange components on a screen. - Each container has a Layout Manager associated with it. - The Layout Manager takes care of arranging components within that container
14
Layout Managers http://java.sun.com/docs/books/tutorial/u iswing/layout/visual.html -FlowLayout - BorderLayout - GridLayout
15
MouseListener Demo http://java.sun.com/j2se/1.4.2/docs/api /java/awt/event/MouseListener.html
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.