Title: Programming in Java
1Programming in Java
- April 3, 2007
- David Goldschmidt, Ph.D.
- Rensselaer Polytechnic Institute
- goldschd_at_strose.edu
2Week 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
3Portability
- 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
4GUI Class Hierarchy (Swing)
5Container Classes
6GUI Helper Classes
7Swing Components
8Core Swing Components
9AWT Classes
10Frames
- A frame is a window not contained inside another
window
11Content 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))
12Frames 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
13Color (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.
14Color (ii)
- Most GUI components are colorable
- setBackground(Color c)
- setForeground(Color c)
15Fonts (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)
16Fonts (ii)
- Set a GUI components font
- okButton.setFont(new Font(etc.))
- Find available font names
- GraphicsEnvironment env
- GraphicsEnvironment.getLocalGraphicsEnvironmen
t() - String fontNames
- e.getAvailableFontFamilyNames()
17Borders
- 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))
18Event-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.
19Event-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
20Adapters
- 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") -
- )
21Event-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
22Inner 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
23Inner Classes (ii)
24Inner 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
25Mouse Events (i)
26Mouse Events (ii)
27Keyboard 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
28Timer Events
- Use timers (the java.awt.Timer class) to control
animations, trigger other events, etc.