Java methods - PowerPoint PPT Presentation

About This Presentation
Title:

Java methods

Description:

??? s?st? at??? e?sa???? to basic swing components, their ... The idiom for working with boxes is slightly different: Box box1 = Box.createHorizontalBox ... – PowerPoint PPT presentation

Number of Views:108
Avg rating:3.0/5.0
Slides: 28
Provided by: marial169
Category:
Tags: idiom | java | methods

less

Transcript and Presenter's Notes

Title: Java methods


1
Java Methods
TM
An Introduction to Object-Oriented Programming
GUI Components and Events
2
?e??e??µe?a
  • ??? s?st?µat??? e?sa???? to basic swing
    components, their methods, and events they
    generate.
  • ?a Components p?? ?a s???t?????
  • JLabel
  • JButton
  • JToggleButton
  • JCheckBox
  • JComboBox
  • JSlider
  • JTextField
  • JPasswordField
  • JTextArea

3
GUI Components
  • ?a Components d?µ????????ta? ap? constructors
  • G?a ?a ???e? ???s?µ? ??a component p??pe? ?a
    p??ste?e? st? applications content pane ? se
    ??a ???? component

JLabel guest new JLabel ("Guest)
JPanel scorePanel new JPanel()
scorePanel.add (guest)
4
GUI Events
  • Components (except JLabel) µp??e? ?a d?µ??????se?
    events.
  • Events are captured and processed by listeners
    objects equipped ??a ?a d?a?e??????ta? ??a
    s???e???µ??? t?p? ap? events.
  • ??af??et???? t?p?? events epe?e??????ta? ap?
    d?af??et????? t?p??? of listeners.

5
Interfaces
  • ??a interface ????e? µ?a ? pe??ss?te?e? public
    methods.
  • ??a interface e??a? pa??µ??? µe µ?a abstract
    class a??? ?aµ?a ap? t?? µe??d??? t?? µp??e? ?a
    ??e? code.
  • µ?a class p?? ????eta? ?a ???p??e? ??a interface
    p??pe? ?a ???p???se? ??e? t?? µe??d??? t??
    interface.
  • µ?a class µp??e? ?a ???e? implement d??f??a
    interfaces.

6
Interfaces (contd)
  • ??a object e??? class p?? ???e? implements ??a
    interface ??e? ??a secondary data type a?t? t??
    interface (ep?p??s?eta st?? primary data type of
    the class).
  • ???f???? t?p?? ap? listeners e??a? interfaces.
    ?? ?d?? object can serve as different types of
    listeners (as long as its class implements all
    the corresponding interfaces).

7
Listeners
Objects of this class are GoHandlers but also
ActionListeners
public class GoHandler implements
ActionListener ... public void
actionPerformed (ActionEvent e) ...

This method is called automatically when the
button is clicked
... JButton go new JButton (Go)
go.addActionListener (new GoHandler ())
This method expects an ActionListener a
GoHandler object qualifies.
8
Listeners (contd)
  • ?ta? ???p??e?ta? ??a event listener, ??
    programmers s???? ???s?µ?p????? µ?a private
    embedded class p?? ??e? p??sßas? se ??a ta fields
    of the surrounding public class.
  • a?t??, ???µ????ta? inner classes ?a? µp????? ?a
    ????? constructors ?a? private fields, ?p?? ??e?
    ?? ???e? class.
  • Inner classes e??a? p??sßas?µe? ???? ap? t??
    outer class t???.

9
Listeners (contd)
public class MyPanel extends JPanel private
JButton go ... go new JButton
(Go) go.addActionListener (new GoHandler
()) ... private class GoHandler
implements ActionListener public void
actionPerformed (ActionEvent e)
go.setText(Stop) ...

go is accessible in constructors and methods of
the inner class
10
Listeners (contd)
  • ?e? e??a? a?a??ast??? ?a capture all events.
  • ?? de? ?e? ?a as?????e?? µe events ap? ??a
    component, ap?? µ?? ep?s????e?? ??a listener se
    a?t?.
  • ?? ?e? ?a capture events a??? ?e??se?? ?a ???e??
    add a listener, t?te ?a???a events will be
    captured (????? pa??????).

11
Listeners (contd)
  • Listener methods pa?????? a?t?st?????? type of
    event sa? argument.
  • Event objects ????? ???s?µe? methods. G?a
    pa??de??µa, getSource returns t? object p??
    pa???a?e t? event.
  • A MouseEvent ??e? t?? methods getX, getY.

12
Menus
  • ?p??e?? ?a p??s??se?? ??a JMenuBar object se
    JFrame ? JApplet.
  • ?p??e?? ?a p??s??se?? JMenu objects se JMenuBar.
  • ?p??e?? ?a p??s??se?? ?a? ???a JMenus,
    JMenuItems, JCheckBoxMenuItems,
    JRadioButtonMenuItems, etc. to a JMenu.

13
JLabel
Constructors JLabel(String text) JLabel(ImageI
con icon) JLabel(String text, ImageIcon icon,
SwingConstants.LEFT) // or CENTER,
RIGHT, LEADING, TRAILING. Methods void
setText(String text) void setIcon(ImageIcon
icon) Events None
14
JButton
Constructors JButton(String text) JButton(Imag
eIcon picture) JButton(String text, ImageIcon
picture) Methods void addActionListener(Action
Listener object) void setText(String text) void
setActionCommand(String cmd) void
setIcon(ImageIcon icon) void requestFocus() Eve
nts class ... implements ActionListener
public void actionPerformed(ActionEvent e)
JButton b (JButton)e.getSource() String s
e.getActionCommand()
15
JCheckBox
Constructors JCheckBox(String text, boolean
checked) JCheckBox(ImageIcon icon, boolean
checked) JCheckBox(String text, ImageIcon icon,
boolean checked) Methods void
addActionListener(ActionListener object) boolean
isSelected() void setSelected(boolean
checked) void setText(String text) void
setIcon(ImageIcon icon) Events class ...
implements ActionListener public void
actionPerformed(ActionEvent e) JCheckBox
b (JCheckBox)e.getSource() if (b
checkBox1 b.isSelected()) ...
16
Layouts
  • ??a? layout manager e??a? ??a e?d??
    st?at?????? (strategy) ??a t?p???t?s?
    components on the content pane or another
    component (usually a panel).
  • In Java, the content pane and any GUI component
    is a Container.
  • A layout is chosen by calling the containers
    setLayout method.

17
Layouts (contd)
  • Layouts are used to achieve some degree of
    platform independence and scalability.
  • AWT/Swing support several layout managers. Here
    we consider four FlowLayout, GridLayout,
    BorderLayout, BoxLayout.
  • These classes implement the java.awt.LayoutManager
    interface.

18
FlowLayout
  • t?p??ete? components se µ?a ??aµµ? ?s? ?????e,
    ?pe?ta ?e???? ??a ??aµµ?.
  • ???s?µ?p??e? best judgement ??a t? spacing
    components. Centers by default.
  • Lets each component assume its natural
    (preferred) size.
  • ???s?µ?p??e?ta? s???? ??a t?p???t?s? buttons on
    panels.

19
FlowLayout (contd)
Container c getContentPane()
c.setLayout (new FlowLayout() ) c.add (new
JButton ("Next Slide")) c.add (new JButton
("Previous Slide")) c.add (new JButton
("Back to Start")) c.add (new JButton
("Exit"))
20
GridLayout
  • ?????e? t? panel into a grid ?ta? d??eta? ?
    a???µ?? t?? ??aµµ?? (rows) ?a? t?? st????
    (columns).
  • ta components t?p??et???ta? sta ?e??? (cells) t??
    grid.
  • ??a????e? t? µ??e??? ???e component ?a ?a??pte?
    ???????? t? cell.
  • ???s??te? ep?p???? spacing a??µesa sta cells.

21
GridLayout (contd)
Container c getContentPane()
c.setLayout (new GridLayout(3, 2, 10, 20 ))
c.add (new JButton ("Next Slide")) c.add
(new JButton ("Previous Slide")) c.add (new
JButton ("Back to Start")) c.add (new
JButton (Last Slide")) c.add (new JButton
("Exit"))
Extra space between the cells
22
BorderLayout
  • Divides the area into five regions and adds a
    component to the specified region.
  • Forces the size of each component to occupy the
    whole region.

23
BorderLayout (contd)
Container c getContentPane()
c.setLayout(new BorderLayout() ) c.add (new
JButton ("Next Slide"), BorderLayout.NORTH)
c.add (new JButton ("Previous Slide"),
BorderLayout.SOUTH) c.add (new JButton
("Back to Start"), BorderLayout.EAST) c.add
(new JButton ("Last Slide"), BorderLayout.WEST)
c.add (new JButton ("Exit"),
BorderLayout.CENTER)
24
BoxLayout
  • Se ??a horizontal box, ta components
    t?p??et???ta? horizontally, ap? a??ste?? p??? ta
    de???.
  • Se ??a vertical box, ta components t?p??et???ta?
    vertically, ap? p??? p??? ta ??t?.

Horizontal or vertical has nothing to do with
the shape of the box itself.
25
BoxLayout (contd)
  • BoxLayout e??a? ? default type of layout ??a ??a
    Box container.
  • The idiom for working with boxes is slightly
    different

Box box1 Box.createHorizontalBox() box1.
add (...) // add a spacer, 60 pixels
box1.add(Box.createHorizontalStrut (60) Box
box2 Box.createVerticalBox() ...
26
BoxLayout (contd)
Container c getContentPane()
c.setLayout(new FlowLayout() ) Box box
Box.createVerticalBox() box.add (new JButton
("Next Slide")) box.add (new JButton
("Previous Slide")) box.add
(Box.createVerticalStrut (20) ) box.add (new
JButton ("Exit")) c.add (box)
27
Default Layouts
  • Each component has a default layout manager,
    which remains in effect until the components
    setLayout method is called.
  • Some of the defaults are
  • Content pane BorderLayout
  • JPanel FlowLayout
  • Box BoxLayout
Write a Comment
User Comments (0)
About PowerShow.com