GUI Properties in Java - PowerPoint PPT Presentation

About This Presentation
Title:

GUI Properties in Java

Description:

text3 = new JTextField( 'Uneditable text field', 20 ); text3.setEditable( false ); c. ... application listens to its own mouse events. addMouseListener( this ) ... – PowerPoint PPT presentation

Number of Views:101
Avg rating:3.0/5.0
Slides: 33
Provided by: LeeMcC
Category:
Tags: gui | java | properties

less

Transcript and Presenter's Notes

Title: GUI Properties in Java


1
GUI Properties in Java
2
Java Basic GUI Components
  • Swing component overview
  • Event handling
  • Inner classes and anonymous inner classes
  • Examples and various components
  • Layouts
  • Panels

3
Swing Components
  • Lightweight vs. heavyweight components
  • Lightweight platform independent
  • Heavyweight tied to platform windowing system
  • Peer object object responsible for interactions
    between heavyweight object and local system
  • Swing components are lightweight

4
The JFrame Class
5
Example
  • import java.awt.
  • import java.awt.event.
  • import javax.swing.
  • public class FrameDemo
  • public static void main(String s)
  • JFrame frame new JFrame("FrameDemo")
  • frame.addWindowListener(new WindowAdapter()
  • public void windowClosing(WindowEvent e)
  • System.exit(0)
  • )
  • JLabel emptyLabel new JLabel("")
  • emptyLabel.setPreferredSize(new Dimension(175,
    100))
  • frame.getContentPane().add(emptyLabel,
    BorderLayout.CENTER)
  • frame.pack()
  • frame.setVisible(true) // show() is
    deprecated

6
The Event Handing Model
  • GUI components are event-driven
  • Programmer must
  • register events
  • implement event handlers
  • Event registration add listeners
  • Event implementation define listener methods

7
Example Registering Events
  • public class TextFieldTest extends JFrame
  • private JTextField text1, text2, text3
  • private JPasswordField password
  • public TextFieldTest()
  • super( "Testing JTextField and JPasswordField"
    )
  • Container c getContentPane()
  • c.setLayout( new FlowLayout() )
  • // construct textfield with default sizing
  • text1 new JTextField( 10 )
  • c.add( text1 )
  • // construct textfield with default text
  • text2 new JTextField( "Enter text here" )
  • c.add( text2 )

8
Example (cont.)
  • // construct textfield with default text and
  • // 20 visible elements and no event handler
  • text3 new JTextField( "Uneditable text
    field", 20 )
  • text3.setEditable( false )
  • c.add( text3 )
  • // construct textfield with default text
  • password new JPasswordField( "Hidden text" )
  • c.add( password )
  • TextFieldHandler handler new
    TextFieldHandler()
  • text1.addActionListener( handler )
  • text2.addActionListener( handler )
  • text3.addActionListener( handler )
  • password.addActionListener( handler )
  • setSize( 325, 100 )
  • show()

9
Listeners for Event Types
  • ActionListener
  • MouseListener
  • MouseMotionListener
  • KeyListener
  • ButtonChangeListener
  • AncestorListener
  • PropertyChangeListener
  • ...

10
ActionListener Interface
11
actionPerformed method
12
Example Handling Events
  • // inner class for event handling
  • private class TextFieldHandler implements
    ActionListener
  • public void actionPerformed( ActionEvent e )
  • String s ""
  • if ( e.getSource() text1 )
  • s "text1 " e.getActionCommand()
  • else if ( e.getSource() text2 )
  • s "text2 " e.getActionCommand()
  • else if ( e.getSource() text3 )
  • s "text3 " e.getActionCommand()
  • else if ( e.getSource() password )
  • JPasswordField pwd (JPasswordField)
    e.getSource()
  • s "password " new String(
    pwd.getPassword() )
  • JOptionPane.showMessageDialog( null, s )

13
Event Handling and Inner Classes
  • Event handler classes are usually private
  • Often event handlers are anonymous inner classes
    defined purely to implement the handing method

14
Driver for Example
  • public static void main( String args )
  • TextFieldTest app new TextFieldTest()
  • app.addWindowListener(new WindowAdapter()
  • public void windowClosing(WindowEvent e)
  • System.exit( 0 )
  • )

15
GUI Components
  • JTextField and JPasswordField
  • JButton
  • JCheckBox and JRadioButton
  • JComboBox
  • JList and Multiple Selection Lists

16
SwingSet Demo
17
Mouse Interaction
  • // Fig. 12.17 MouseTracker.java
  • // Demonstrating mouse events.
  • import java.awt.
  • import java.awt.event.
  • import javax.swing.
  • public class MouseTracker extends JFrame
  • implements MouseListener, MouseMotionListener
  • private JLabel statusBar
  • public MouseTracker()
  • super( "Demonstrating Mouse Events" )
  • statusBar new JLabel()
  • getContentPane().add( statusBar,
    BorderLayout.SOUTH )
  • // application listens to its own mouse events
  • addMouseListener( this )
  • addMouseMotionListener( this )
  • setSize( 275, 100 )
  • show()

18
Mouse Interaction (cont.)
  • // MouseListener event handlers
  • public void mouseClicked( MouseEvent e )
  • statusBar.setText( "Clicked at " e.getX()
    ", " e.getY() "" )
  • public void mousePressed( MouseEvent e )
  • statusBar.setText( "Pressed at " e.getX()
    ", " e.getY() "" )
  • public void mouseReleased( MouseEvent e )
  • statusBar.setText( "Released at " e.getX()
    ", " e.getY() "" )
  • public void mouseEntered( MouseEvent e )
  • statusBar.setText( "Mouse in window" )
  • public void mouseExited( MouseEvent e )

19
Mouse Interaction (cont.)
  • // MouseMotionListener event handlers
  • public void mouseDragged( MouseEvent e )
  • statusBar.setText( "Dragged at " e.getX()
    ", " e.getY() "" )
  • public void mouseMoved( MouseEvent e )
  • statusBar.setText( "Moved at " e.getX() ",
    " e.getY() "" )
  • public static void main( String args )
  • MouseTracker app new MouseTracker()
  • app.addWindowListener(new WindowAdapter()
  • public void windowClosing( WindowEvent e )
  • System.exit( 0 )
  • )

20
Adapter Classes
  • Interfaces with many methods to implement can be
    cumbersome
  • The adapter class provides a default
    implementation of all interface methods
  • Application can over-ride interface methods that
    are of interest

21
Java-Based GUI Components
  • Layout managers
  • Panels
  • Customized panels
  • Menus
  • More layout

22
Simple Layout Managers
  • Automatic control over component placement
    according to layout scheme
  • API for particular layout scheme provides tunable
    options

23
BorderLayout
  • Default layout manager for the content pane
  • Implements the LayoutManager2 interface
  • Lays out components based on a compass scheme
  • Rules for expansion/filling when components
    are left out of the compass positions

24
FlowLayout
  • Adds components to the panel in the first open
    spot as they are added
  • Nice for quick (sometimes ugly) layout of button
    banks or text boxes

25
GridLayout
  • Arranges components into rows and columns
  • Must specify rows and columns
  • Nice for an organized look for lists of elements
    (like a spreadsheet)

26
Using Panels
  • Panels can have their own layout
  • Multiple panels can be added to the container

27
Menus and JFrame
  • JMenuBar, JMenuItem, JMenu
  • Objects of classes that provide setJMenuBar()
    are able to support menus JFrame, JApplet
  • Customization/added functionality
  • JCheckBoxMenuItem,
  • JRadioButtonMenuItem

28
Menu Example
  • // Fig. 13.7 MenuTest.java
  • // Demonstrating menus
  • import javax.swing.
  • import java.awt.event.
  • import java.awt.
  • public class MenuTest extends JFrame
  • private Color colorValues Color.black,
    Color.blue, Color.red, Color.green
  • private JRadioButtonMenuItem colorItems,
    fonts
  • private JCheckBoxMenuItem styleItems
  • private JLabel display
  • private ButtonGroup fontGroup, colorGroup
  • private int style
  • public MenuTest()
  • super( "Using JMenus" )
  • JMenuBar bar new JMenuBar() // create
    menubar
  • setJMenuBar( bar ) // set the menubar for the
    JFrame

29
Menu Example (cont.)
  • // create File menu and Exit menu item
  • JMenu fileMenu new JMenu( "File" )
  • fileMenu.setMnemonic( 'F' )
  • JMenuItem aboutItem new JMenuItem( "About..."
    )
  • aboutItem.setMnemonic( 'A' )
  • aboutItem.addActionListener(new
    ActionListener()
  • public void actionPerformed( ActionEvent e )
  • JOptionPane.showMessageDialog( MenuTest.th
    is,
  • "This is an example\n of using
    menus",
  • "About", JOptionPane.PLAIN_MESSAGE)
  • )
  • fileMenu.add( aboutItem )

30
Menu Example (cont.)
  • JMenuItem exitItem new JMenuItem( "Exit" )
  • exitItem.setMnemonic( 'x' )
  • exitItem.addActionListener(
  • new ActionListener()
  • public void actionPerformed( ActionEvent e )
  • System.exit( 0 )
  • )
  • fileMenu.add( exitItem )
  • bar.add( fileMenu ) // add File menu
  • // create the Format menu, its submenus and
    menu items
  • JMenu formatMenu new JMenu( "Format" )
  • formatMenu.setMnemonic( 'r' )
  • // create Color submenu
  • String colors "Black", "Blue", "Red",
    "Green"
  • JMenu colorMenu new JMenu( "Color" )
  • colorMenu.setMnemonic( 'C' )
  • colorItems new JRadioButtonMenuItem
    colors.length

31
Menu Example Run
32
Snazzy and Fun
  • Pop-up menus
  • Internal frames
  • Look and feel options
Write a Comment
User Comments (0)
About PowerShow.com