Java Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Java Programming

Description:

Java's method of providing a. constant. final. You can't change the. value ... (One of the major challenges in Java .. Getting your components to reveal themselves. ... – PowerPoint PPT presentation

Number of Views:77
Avg rating:3.0/5.0
Slides: 64
Provided by: jm158
Category:

less

Transcript and Presenter's Notes

Title: Java Programming


1
Java Programming
Introduction to Swing Class 7
2
Swing
  • Graphical User Interface
  • Provides
  • Windows
  • Menus
  • Buttons
  • Labels
  • Textboxes
  • In the beginning, there was AWT .
  • Abstract Windowing Toolkit
  • Relied on peers
  • Heavyweight Native Code interfaces
  • Written for each target
  • Enabled original AWT to be written in 6 weeks!
  • Portability problems

3
A Swing Program
  • import java.awt.
  • import javax.swing.
  • public class JWTest
  • static final int n_label 10
  • public static void main( String args )
  • JLabel lab1
  • Jwindow w new JWindow()
  • Container content_pane w.getContentPane()
  • w.setVisible( true )
  • content_pane.setLayout( new BorderLayout( ) )
  • lab1 new JLabeln_label
  • for(int k0kltn_labelk)
  • lab1k new JLabel("Label " k)
  • content_pane.add( lab1k )
  • w.pack()
  • w.show()
  • System.exit(0)

4
A Swing Program
  • import java.awt.
  • import javax.swing.
  • public class JWTest
  • static final int n_label 10
  • public static void main( String args )
  • JLabel lab1
  • Jwindow w new JWindow()
  • Container content_pane w.getContentPane()
  • w.setVisible( true )
  • content_pane.setLayout( new BorderLayout( ) )
  • lab1 new JLabeln_label
  • for(int k0kltn_labelk)
  • lab1k new JLabel("Label " k)
  • content_pane.add( lab1k )
  • w.pack()
  • w.show()
  • System.exit(0)

5
A Swing Program
  • import java.awt.
  • import javax.swing.
  • public class JWTest
  • static final int n_label 10
  • public static void main( String args )
  • JLabel lab1
  • Jwindow w new JWindow()
  • Container content_pane w.getContentPane()
  • w.setVisible( true )
  • content_pane.setLayout( new BorderLayout( ) )
  • lab1 new JLabeln_label
  • for(int k0kltn_labelk)
  • lab1k new JLabel("Label " k)
  • content_pane.add( lab1k )
  • w.pack()
  • w.show()
  • System.exit(0)

6
Constants
  • import java.awt.
  • import javax.swing.
  • public class JWTest
  • static final int n_label 10
  • public static void main( String args )
  • JLabel lab1
  • Jwindow w new JWindow()
  • Container content_pane w.getContentPane()
  • w.setVisible( true )
  • content_pane.setLayout( new BorderLayout( ) )
  • lab1 new JLabeln_label
  • for(int k0kltn_labelk)
  • lab1k new JLabel("Label " k)
  • content_pane.add( lab1k )
  • w.pack()
  • w.show()
  • System.exit(0)

7
Arrays
  • import java.awt.
  • import javax.swing.
  • public class JWTest
  • static final int n_label 10
  • public static void main( String args )
  • JLabel lab1
  • JWindow w new JWindow()
  • Container content_pane w.getContentPane()
  • w.setVisible( true )
  • content_pane.setLayout( new BorderLayout( ) )
  • lab1 new JLabeln_label
  • for(int k0kltn_labelk)
  • lab1k new JLabel("Label " k)
  • content_pane.add( lab1k )
  • w.pack()
  • w.show()
  • System.exit(0)

8
Construct a window
  • import java.awt.
  • import javax.swing.
  • public class JWTest
  • static final int n_label 10
  • public static void main( String args )
  • JLabel lab1
  • JWindow w new JWindow()
  • Container content_pane w.getContentPane()
  • w.setVisible( true )
  • content_pane.setLayout( new BorderLayout( ) )
  • lab1 new JLabeln_label
  • for(int k0kltn_labelk)
  • lab1k new JLabel("Label " k)
  • content_pane.add( lab1k )
  • w.pack()
  • w.show()
  • System.exit(0)

9
Where can I draw things?
  • import java.awt.
  • import javax.swing.
  • public class JWTest
  • static final int n_label 10
  • public static void main( String args )
  • JLabel lab1
  • JWindow w new JWindow()
  • Container content_pane w.getContentPane()
  • w.setVisible( true )
  • content_pane.setLayout( new BorderLayout( ) )
  • lab1 new JLabeln_label
  • for(int k0kltn_labelk)
  • lab1k new JLabel("Label " k)
  • content_pane.add( lab1k )
  • w.pack()
  • w.show()
  • System.exit(0)

10
Panes?
  • There are 3 of them
  • contentPane
  • JPanel
  • Resides in layeredPane
  • Contains components
  • glassPane
  • JPanel
  • Floats above others
  • Traps mouse events first
  • layeredPane
  • JLayeredPane
  • Contains contentPane and menuBar
  • menuBar
  • JMenuBar

11
Visibility and Layout
  • import java.awt.
  • import javax.swing.
  • public class JWTest
  • static final int n_label 10
  • public static void main( String args )
  • JLabel lab1
  • JWindow w new JWindow()
  • Container content_pane w.getContentPane()
  • w.setVisible( true )
  • content_pane.setLayout( new BorderLayout( ) )
  • lab1 new JLabeln_label
  • for(int k0kltn_labelk)
  • lab1k new JLabel("Label " k)
  • content_pane.add( lab1k )
  • w.pack()
  • w.show()
  • System.exit(0)

12
Adding things to a window
  • import java.awt.
  • import javax.swing.
  • public class JWTest
  • static final int n_label 10
  • public static void main( String args )
  • JLabel lab1
  • Jwindow w new JWindow()
  • Container content_pane w.getContentPane()
  • w.setVisible( true )
  • content_pane.setLayout( new BorderLayout( ) )
  • lab1 new JLabeln_label
  • for(int k0kltn_labelk)
  • lab1k new JLabel("Label " k)
  • content_pane.add( lab1k )
  • w.pack()
  • w.show()
  • System.exit(0)

13
Some Java features
  • import java.awt.
  • import javax.swing.
  • public class JWTest
  • static final int n_label 10
  • public static void main( String args )
  • JLabel lab1
  • Jwindow w new JWindow()
  • Container content_pane w.getContentPane()
  • w.setVisible( true )
  • content_pane.setLayout( new BorderLayout( ) )
  • lab1 new JLabeln_label
  • for(int k0kltn_labelk)
  • lab1k new JLabel("Label " k)
  • content_pane.add( lab1k )
  • w.pack()
  • w.show()
  • System.exit(0)

14
Almost there ...
  • import java.awt.
  • import javax.swing.
  • public class JWTest
  • static final int n_label 10
  • public static void main( String args )
  • JLabel lab1
  • Jwindow w new JWindow()
  • Container content_pane w.getContentPane()
  • w.setVisible( true )
  • content_pane.setLayout( new BorderLayout( ) )
  • lab1 new JLabeln_label
  • for(int k0kltn_labelk)
  • lab1k new JLabel("Label " k)
  • content_pane.add( lab1k )
  • w.pack()
  • w.show()
  • System.exit(0)

15
Those labels are boring ...
  • Changing them
  • Swing API
  • Documentation cached at ..
  • http//www.javasoft.com
  • Observe the hierarchy

Object
Component
Container
JComponent
JLabel
16
Those labels are boring ...
  • Changing them
  • Note the specification

public class JLabel extends
JComponent implements
SwingConstants, Accessible
17
Those labels are boring ...
  • Look for methods that operate on JLabel
  • Note that most of them dont have arguments
  • They operate on a JLabel

Icon getIcon() int getHorizontalAlignment() int
getHorizontalTextPosition() String
getText() void setText( String text ) etc
JLabel label new JLabel(Test label) Icon img
label.getIcon()
18
But I want to change the text font!
  • Look for methods on the superclasses
  • On JComponent
  • Since a JLabel is also a Jcomponent ..
  • works fine!

void setFont( Font font )
Jlabel label new Jlabel(Test
label) label.setFont( newfont )
19
and I want to change the location ..
  • Look for methods on the superclasses
  • On JComponent
  • ?? Nothing likely
  • Try the next ancestor ..
  • On Container
  • ?? Nothing likely
  • On Component
  • looks promising ...
  • But theres two???
  • works fine!

setLocation
20
Polymorphism in practice ...
  • But theres two .. !!
  • Choose the most convenient
  • the compiler determines which one to use by
    matching the signature ..

void setLocation( int x, int y )void
setLocation( Point p )
label.setLocation( X_POS, Y_POS )
void setLocation( int, int )
21
Polymorphism in practice ...
  • Multiple Constructors
  • Common situation
  • Again choose the most convenient
  • Other attribute values will assume default
    values, eg
  • No icon, centre alignment

JLabel() JLabel( Icon image ) JLabel( Icon
image, int horAlign )JLabel( String text
)JLabel( String text, Icon image,
int horAlogin JLabel( String text, int horAlign )
22
Other common components
  • Buttons
  • JButton
  • Standard way for initiating some action from a
    screen
  • Used with an ActionListener (next lecture!)
  • JToggleButton
  • Two states selected / de-selected
  • Query with
  • boolean isSelected()
  • ButtonGroup
  • Found in the parent AbstractButton!!
  • Allows groups of buttons
  • Select one
  • Automatic de-select of remainder

23
Other common components
  • Buttons
  • JCheckBox
  • Checked box ..
  • Selected / Not selected
  • Its a specialization of JToggleButton!
  • JRadioButton
  • Similar to JCheckBox

24
Frames Applets
  • JFrame
  • Specialization of Frame
  • Adds
  • Border
  • Menubar
  • Close box
  • JApplet
  • Similar to JFrame
  • Both have
  • ContentPane
  • GlassPane

25
Panels
  • JPanel
  • Useful as a container for other elements

26
Panels
  • JPanel
  • Useful as a container for other elements

JWindow w new JWindow()JPanel panel1 new
JPanel() JPanel panel2 new JPanel() JButton
b1 new JButton() panel1.add( b1 ) w.add(
panel1 ) panel2.add( b2 ) w.add( panel2 )
27
Panels
  • JPanel
  • Make the panels classes!

28
Text - Edit for user input
  • JTextField
  • Single line of editable text
  • public class TextExample extends JPanel
    JTextField tf
  • public TextExample( String s )
  • tf new JTextField( s ) add( tf )
  • public String getText() return
    tf.getText()

29
Text - Edit for user input
  • JTextArea
  • Multiple lines of editable text
  • public class TextExample extends JPanel
    JTextArea ta
  • static final int rows 10
  • static final int cols 20
  • public TextExample( String s )
  • ta new JTextArea( s, rows, cols ) add(
    ta )
  • public String getText( int row )
  • int offset ta.getLineStartOffset( row )
    return tf.getText( offset, cols )

30
Menus
  • Menus are attached to a JMenuBar
  • A Menu(JMenu)contains items (JMenuItem)
  • Recipe
  • Create a frame
  • Create a menubar
  • Create a menu
  • Create some menu items
  • Add an ActionListener (next lecture!)
  • Add them to the menu
  • Add the menu to the menubar
  • Set the menubar

Repeat as needed
31
Menus
  • Example
  • Create a frame
  • Create a menubar
  • Create a menu
  • Create some menu items
  • Trap events
  • Add them to the menu
  • Add the menuto the menubar
  • Set the menubar

32
Swing Example
  • Jwindow
  • Simple usage example
  • import javax.swing.public class windowexample
    public static void main(String args)
    JWindow w JLabel label int n w
    new JWindow() label new JLabel(Example
    label) w.add( label ) n
    w.getComponentCount() System.out.println(Wi
    ndow has n
    components) w.show()

33
Java ProgrammingSwing (continued)
10 mins break (not more)
34
Menus
  • Menus are attached to a JMenuBar
  • A Menu(JMenu)contains items (JMenuItem)
  • Recipe
  • Create a frame
  • Create a menubar
  • Create a menu
  • Create some menu items
  • Add an ActionListener (next lecture!)
  • Add them to the menu
  • Add the menu to the menubar
  • Set the menubar

Repeat as needed
35
Menus
  • Example
  • Create a frame
  • Create a menubar
  • Create a menu
  • Create some menu items
  • Trap events
  • Add them to the menu
  • Add the menuto the menubar
  • Set the menubar

36
Thats all very pretty but ...
  • Our GUI needs some action!
  • Add Listeners to Components
  • Component fire events,
  • Listeners trap them
  • Various types of listeners ..
  • ActionListener
  • MouseListener
  • MenuListener
  • and some more ...
  • Listeners are interfaces
  • No code!
  • Remember just a specification for methods you
    must supply

37
Making a Listener - Method 1
  • Define a class implementing ActionListener

// Event Handler import java.awt. import
java.awt.event. import javax.swing. public
class EventHandler implements ActionListener
JPanel parent JComponent src public
EventHandler( JPanel m ) // Establish a link
to my parent parent m //
ActionListener interface public void
actionPerformed( ActionEvent e )
System.out.println("aP event " e ) src
(JComponent)(e.getSource())
38
Making a Listener - Method 1
  • Define a class implementing ActionListener

// Event Handler import java.awt. import
java.awt.event. import javax.swing. public
class EventHandler implements ActionListener
JPanel parent JComponent src public
EventHandler( JPanel m ) // Establish a link
to my parent parent m //
ActionListener interface public void
actionPerformed( ActionEvent e )
System.out.println("aP event " e ) src
(JComponent)(e.getSource())
39
Making a Listener - Method 1
  • Define a class implementing ActionListener

// Event Handler import java.awt. import
java.awt.event. import javax.swing. public
class EventHandler implements ActionListener
JPanel parent JComponent src public
EventHandler( JPanel m ) // Establish a link
to my parent parent m //
ActionListener interface public void
actionPerformed( ActionEvent e )
System.out.println("aP event " e ) src
(JComponent)(e.getSource())
40
Making a Listener - Method 1
  • Define a class implementing ActionListener

// Event Handler import java.awt. import
java.awt.event. import javax.swing. public
class EventHandler implements ActionListener
JPanel parent JComponent src public
EventHandler( JPanel m ) // Establish a link
to my parent parent m //
ActionListener interface public void
actionPerformed( ActionEvent e )
System.out.println("aP event " e ) src
(JComponent)(e.getSource())
41
Making a Listener - Method 1
  • Define a class implementing ActionListener

// Event Handler import java.awt. import
java.awt.event. import javax.swing. public
class EventHandler implements ActionListener
JPanel parent JComponent src public
EventHandler( JPanel m ) // Establish a link
to my parent parent m //
ActionListener interface public void
actionPerformed( ActionEvent e )
System.out.println("aP event " e ) src
(JComponent)(e.getSource())
42
Making a Listener - Method 1
  • Define a class implementing ActionListener

// Event Handler import java.awt. import
java.awt.event. import javax.swing. public
class EventHandler implements ActionListener
JPanel parent JComponent src public
EventHandler( JPanel m ) // Establish a link
to my parent parent m //
ActionListener interface public void
actionPerformed( ActionEvent e )
System.out.println("aP event " e ) src
(JComponent)(e.getSource())
43
Making a Listener - Method 1
  • Define a class implementing ActionListener

// Event Handler import java.awt. import
java.awt.event. import javax.swing. public
class EventHandler implements ActionListener
JPanel parent JComponent src public
EventHandler( JPanel m ) // Establish a link
to my parent parent m //
ActionListener interface public void
actionPerformed( ActionEvent e )
System.out.println("aP event " e ) src
(JComponent)(e.getSource())
44
wed better attach the listener to something!
  • An Action button
  • import javax.swing.public class windowexample
    public static void main(String args)
    JWindow w new JWindow() JButton b new
    JButton(Action!) EventHandler h new
    EventHandler( this ) Container cp
    getContentPane() cp.add( b )
    b.addActionListener( h ) w.pack()
    w.show()

45
How does the listener talk to us??
  • Various possibilities
  • Call a method on the parent

46
How does the listener talk to us??
  • Various possibilities
  • Call a method in another class

47
A case for an interface!!
  • Define a class of objects which can be
    incremented
  • Must provide a void incX( int ) method

48
A case for an interface!!
  • Define a class of objects which can be
    incremented
  • Must provide a void incX( int ) method
  • The interface provides the signature for a method
  • (or methods - it can require many)
  • The class implements the method(s) in the
    interface
  • and any others that it needs!

49
A case for an interface!!
  • A class may implement several interface

50
Comparable ..
  • To make many common structures ..
  • Look up tables, search trees
  • and perform common operations
  • Sort, Match
  • We need to compare items ..
  • A Comparable interface is part of java.lang
  • It has one method
  • Class implementor decides how objects of a class
    are ordered
  • May depend on some very complex rules!!
  • So compareTo can only be implemented in specific
    classes
  • but there are many places where ordering is
    needed!!

51
Talking to the handler ...
  • The handler class can update one of its
    attributes and provide a method to access it

52
Making a Listener - Method 2
  • Define a separate class is a pain!
  • Too many files, overhead
  • No problem - make it an inner class

public class windowexample int x class
EventHandler implements ActionListener
// ActionListener interface public void
actionPerformed( ActionEvent e ) x
public static void main(String args)
JWindow w new JWindow() JButton b new
JButton(Action!) EventHandler h new
EventHandler( this ) Container cp
getContentPane() cp.add( b )
b.addActionListener( h )
53
Making a Listener - Method 2
  • Define a separate class is a pain!
  • Too many files, too much overhead
  • No problem - make it an inner class

public class windowexample int x class
EventHandler implements ActionListener
// ActionListener interface public void
actionPerformed( ActionEvent e ) x
public static void main(String args)
JWindow w new JWindow() JButton b new
JButton(Action!) EventHandler h new
EventHandler( this ) Container cp
getContentPane() cp.add( b )
b.addActionListener( h )
54
Making a Listener - Method 3
  • But thats a whole class with only one method!
  • Im lazy!
  • No problem - make it an anonymous class

public class windowexample int x public
static void main(String args) JWindow w
new JWindow() JButton b new
JButton(Action!) b.addActionListener(
new ActionListener() public
void actionPerformed( ActionEvent e )
x ) Container cp
getContentPane() cp.add( b )
55
Making a Listener - Method 3
  • But thats a whole class with only one method!
  • Im lazy!
  • No problem - make it an anonymous class

public class windowexample int x public
static void main(String args) JWindow w
new JWindow() JButton b new
JButton(Action!) b.addActionListener(
new ActionListener() public
void actionPerformed( ActionEvent e )
x ) Container cp
getContentPane() cp.add( b )
56
Making a Listener - Method 3
  • But thats a whole class with only one method!
  • Im lazy!
  • No problem - make it an anonymous class

public class windowexample int x public
static void main(String args) JWindow w
new JWindow() JButton b new
JButton(Action!) b.addActionListener(
new ActionListener() public
void actionPerformed( ActionEvent e )
x )
Container cp getContentPane() cp.add( b
)
57
Anonymous classes
  • The syntax can be confusing!

public class windowexample int x public
static void main(String args) JWindow w
new JWindow() JButton b new
JButton(Action!) b.addActionListener(
new ActionListener() public
void actionPerformed( ActionEvent e )
x )
Container cp getContentPane() cp.add( b
)
58
Anonymous classes
  • The syntax can be confusing!

public class windowexample int x public
static void main(String args) JWindow w
new JWindow() JButton b new
JButton(Action!) b.addActionListener(
new ActionListener() public
void actionPerformed( ActionEvent e )
x )
Container cp getContentPane() cp.add( b
)
59
Anonymous classes
  • The syntax can be confusing!

public class windowexample int x public
static void main(String args) JWindow w
new JWindow() JButton b new
JButton(Action!) b.addActionListener(
new ActionListener() public
void actionPerformed( ActionEvent e )
x )
Container cp getContentPane() cp.add( b
)
60
Anonymous classes
  • The syntax can be confusing!

public class windowexample int x public
static void main(String args) JWindow w
new JWindow() JButton b new
JButton(Action!) b.addActionListener(
new ActionListener() public
void actionPerformed( ActionEvent e )
x )
Container cp getContentPane() cp.add( b
)
61
MouseListener interface
  • Fine control of your GUI, but
  • 5 methods!
  • An interface must implement each one
  • Fine if you want special effects,but painful if
    you only want a click!

JButton b new JButton(Action!)b.addMouseList
ener( new MouseListener() public
void mouseClicked(MouseEvent e)
public void mouseEntered(MouseEvent e)
public void mouseExited(MouseEvent e)
public void mousePressed(MouseEvent e)
public void mouseReleased(MouseEvent e)
)
62
MouseListener interface
  • 5 methods!
  • A classic case for an adapter!
  • MouseAdapter is a class implementing
    MouseListener
  • Provides dummy implementations for all 5 required
    methods
  • You override only the one you want to use
  • Fine if you want special events,but painful if
    you only want a click!

JButton b new JButton(Action!)b.addMouseList
ener( new MouseAdapter() // Only
interested in clicks .. public void
mouseClicked(MouseEvent e) x x 1
)
63
What about the event parameter?
  • Look in the ..Event classes
  • Properties of events
  • eg Location
  • For those fancy drag-and-drop applications!
  • Now you can set the location of some other object
    ..And repaint making the object follow the
    mouse, ...

JPanel p new JPanel()p.addMouseListener(
new MouseAdapter() // Want to know where
the mouse was pressed public void
mousePressed(MouseEvent e) x
e.getX() y e.getY() // Code to
move something here repaint()
)
Write a Comment
User Comments (0)
About PowerShow.com