Programming in Java - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Programming in Java

Description:

Frames as Containers. All JFrame objects act as container objects ... Borders. Create a border for any instances of the JComponent class ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 29
Provided by: ETS38
Category:

less

Transcript and Presenter's Notes

Title: Programming in Java


1
Programming in Java
  • April 3, 2007
  • David Goldschmidt, Ph.D.
  • Rensselaer Polytechnic Institute
  • goldschd_at_strose.edu

2
Week 12 Coverage
  • Remaining Assignments
  • Project 2 due April 19
  • Homework 3 due April 24
  • Project 3 due May 2
  • Exam 2 on May 1
  • Chapter 12 GUI Programming
  • Chapter 14 Event-Driven Programming

3
Portability
  • The java.awt library contains heavyweight classes
  • Implemented via the Java Virtual Machine
  • Dependent on target platform
  • The javax.swing library contains lightweight
    classes
  • Implemented in Java (except for subclasses of
    java.awt.Window and java.awt.Panel)
  • Independent of target platform

4
GUI Class Hierarchy (Swing)
5
Container Classes
6
GUI Helper Classes
7
Swing Components
8
Core Swing Components
9
AWT Classes
10
Frames
  • A frame is a window not contained inside another
    window

11
Content Frame Delegation
  • In Java 1.4, required to add GUI elements to
    content panes
  • frame.getContentPane().add(new JButton(OK))
  • In Java 1.5, frame delegates automatically to the
    content pane
  • frame.add(new JButton(OK))

12
Frames as Containers
  • All JFrame objects act as container objects
  • GUI objects are added (much like an ArrayList),
    then displayed when requested
  • A layout manager describes how GUI objects are to
    be displayed
  • FlowLayout
  • GridLayout
  • BorderLayout
  • GridBagLayout

The order in which objects are added matters
to FlowLayout and GridLayout layout managers
13
Color (i)
  • The java.awt.Color class represents colors within
    GUI elements and graphics
  • Colors are made up of red, blue, green
    components
  • java.awt.Color c new Color(255, 0, 128)
  • Standard colors are predefined constants in the
    java.awt.Color class
  • BLACK, RED, BLUE, YELLOW, GRAY, etc.

14
Color (ii)
  • Most GUI components are colorable
  • setBackground(Color c)
  • setForeground(Color c)

15
Fonts (i)
  • Use the java.awt.Font class to work with fonts
  • Font font new Font(name, style, pointSize)
  • Examples
  • Font f1 new Font(Serif, Font.PLAIN, 12)
  • Font f2 new Font(SansSerif, Font.BOLD, 16)
  • Font f3 new Font(Monospaced,
  • Font.BOLD Font.ITALIC,
    16)
  • Font f4 new Font(Times New Roman,
    Font.PLAIN, 16)

16
Fonts (ii)
  • Set a GUI components font
  • okButton.setFont(new Font(etc.))
  • Find available font names
  • GraphicsEnvironment env
  • GraphicsEnvironment.getLocalGraphicsEnvironmen
    t()
  • String fontNames
  • e.getAvailableFontFamilyNames()

17
Borders
  • Create a border for any instances of the
    JComponent class
  • LineBorder b new LineBorder(Color.RED, 4)
  • panel1.setBorder(b)
  • panel2.setBorder(new TitleBorder(Student
    Data))

18
Event-Driven Programming (i)
  • In event-driven programming, code is executed
    upon activation of events
  • Events include mouse movements, mouse clicks,
    mouse drags, keystrokes, timer expirations,
    operating system events, etc.

19
Event-Driven Programming (ii)
Source Event TypeUser Action Object Generated C
lick a button JButton ActionEvent Click a check
box JCheckBox ItemEvent, ActionEvent Click a
radio button JRadioButton ItemEvent,
ActionEvent Press return on a text
field JTextField ActionEvent Select a new
item JComboBox ItemEvent, ActionEvent Window
opened, closed, etc. Window WindowEvent Mouse
pressed, released, etc. Component MouseEvent Key
released, pressed, etc. Component KeyEvent
20
Adapters
  • Listeners are typically interfaces, therefore
    requiring all methods be implemented
  • Use an adapter class to provide convenience
    implementations of unused methods
  • addWindowListener(new WindowAdapter()
  • public void windowActivated(WindowEvent
    event)
  • System.out.println("Window
    activated")
  • )

21
Event-Driven Programming (iii)
  • Listener classes respond to actions
  • e.g. override the java.awt.event.actionPerformed()
    method
  • Listener classes must be registered with the GUI
    objects they are listening for
  • e.g. via the okButton.addActionListener() method

22
Inner Classes (i)
  • An inner class is a class defined within a class
  • Visible only to the surrounding outer class
  • Can access outer class data members and methods
  • Useful when a specific helper class is required
    (e.g. to implement a listener in
    GUI programming)
  • Inner classes may be declared public, private, or
    protected and follow the same visibility rules as
    data members and methods

23
Inner Classes (ii)
24
Inner Classes (iii)
  • An inner class defined as static may be accessed
    outside the outer class
  • A static inner class cannot access non-static
    data members or methods of its outer class

25
Mouse Events (i)
26
Mouse Events (ii)
27
Keyboard Events
  • Capture keyboard events via the KeyListener
    interface
  • keyPressed(KeyEvent e)
  • keyReleased(KeyEvent e)
  • keyTyped(KeyEvent e)

Home VK_HOME Page Up VK_PGUP Page Down VK_PGDN
Arrow Keys VK_UP VK_DOWN VK_RIGHT VK_LEFT
28
Timer Events
  • Use timers (the java.awt.Timer class) to control
    animations, trigger other events, etc.
Write a Comment
User Comments (0)
About PowerShow.com