Outline 0024 Spring 2010 17 :: 2 More information about Swing - - PowerPoint PPT Presentation
Outline 0024 Spring 2010 17 :: 2 More information about Swing - - PowerPoint PPT Presentation
Outline 0024 Spring 2010 17 :: 2 More information about Swing 0024 Spring 2010 17 :: 3 Java Foundation Classes import java.awt.*; import java.awt.event.*; import javax.swing.*; import
– 17 :: 2 – 0024 Spring 2010
Outline
– 17 :: 3 – 0024 Spring 2010
More information about Swing
– 17 :: 4 – 0024 Spring 2010
Java Foundation Classes
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*;
– 17 :: 5 – 0024 Spring 2010
Swing vs. AWT
(c) OʼReilly 1999
– 17 :: 6 – 0024 Spring 2010
Swing
– 17 :: 7 – 0024 Spring 2010
Top-level containers
http://java.sun.com/docs/books/tutorial/ui/features/components.html
– 17 :: 8 – 0024 Spring 2010
Containers
containers
– 17 :: 9 – 0024 Spring 2010
General-purpose containers
– 17 :: 10 – 0024 Spring 2010
Basic controls
– 17 :: 11 – 0024 Spring 2010
Essential Swing components
AWT Swing
– 17 :: 12 – 0024 Spring 2010
Example 1
pack() causes a window to be sized to fit the preferred size and layouts of its sub- components
– 17 :: 13 – 0024 Spring 2010
Example 2
In this example how a custom frame is created
– 17 :: 14 – 0024 Spring 2010
Build from bottom up
JPanel JButton JFrame JLabel
– 17 :: 15 – 0024 Spring 2010
Layout managers
Left to right, Top to bottom c n s e w FlowLayout GridLayout BorderLayout none, programmer sets x,y,w,h null One at a time CardLayout GridBagLayout JButton Organizing Layout of components in a container
– 17 :: 16 – 0024 Spring 2010
Combinations
JButton JButton JTextArea
– 17 :: 17 – 0024 Spring 2010
Combinations
n JPanel: BorderLayout c JFrame JPanel: FlowLayout JButton JButton JTextArea
– 17 :: 18 – 0024 Spring 2010
Code: null layout
press me
– 17 :: 19 – 0024 Spring 2010
Code: FlowLayout
press me then me
– 17 :: 20 – 0024 Spring 2010
import java.awt.*; import javax.swing.*; public class Main { public static void main(String[] args) { JFrame f = new JFrame("title"); JPanel p = new JPanel( ); FlowLayout L = new FlowLayout( ); JButton b1 = new JButton("press me"); JButton b2 = new JButton("then me"); p.setLayout(L); p.add(b1); p.add(b2); f.setContentPane(p); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setVisible(true); } }
– 17 :: 21 – 0024 Spring 2010
Box Layout
Component.RIGHT_ALIGNMENT Component.CENTER_ALIGNMENT Component.LEFT_ALIGNMENT
myPane.setLayout(new BoxLayout(myPane, BoxLayout.PAGE_AXIS));
– 17 :: 22 – 0024 Spring 2010
A simple Swing program - Events
- Objects communicate by “firing” and “handling” events
- Events are sent from a single source object to one or
more registered listener objects
– 17 :: 23 – 0024 Spring 2010
Types of event listeners
Act that results in event Listener type
User clicks a button, presses Return while typing in a text field, or chooses a menu item ActionListener User closes a frame (main window) WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener Component becomes visible ComponentListener Component gets the keyboard focus FocusListener Table or list selection changes ListSelectionListener
– 17 :: 24 – 0024 Spring 2010
Handling events I
JPanel JButton Listener
- JFrame
JLabel
– 17 :: 25 – 0024 Spring 2010
Event handling II
– 17 :: 26 – 0024 Spring 2010
Example using local class
– 17 :: 27 – 0024 Spring 2010
JDialog
– 17 :: 28 – 0024 Spring 2010
Dialog Example
The JOptionPane class can be used to create simple modal dialogs (icons, title, text and buttons can be customized).
– 17 :: 29 – 0024 Spring 2010
Swing and Threads
– 17 :: 30 – 0024 Spring 2010
Safe Swing code
Safe to update state of Swing components Unsafe to update state of Swing components
– 17 :: 31 – 0024 Spring 2010
Event Dispatching
javax.swing.SwingUtilities.invokeLater( new Runnable(){ public void run(){ // Access to components } } );
– 17 :: 32 – 0024 Spring 2010
Handle a job in the background
– 17 :: 33 – 0024 Spring 2010
Model/View/Controller [MVC]
http://www.itu.dk/courses/VOP/E2005/VOP2005E/8_mvc_krasner_and_pope.pdf
– 17 :: 34 – 0024 Spring 2010
Model/View/Controller
Model
complete, self-contained representation of
- bject managed by the application
e.g., spreadsheet document
provides a number of services to
manipulate the data e.g., recalculate, save
computation and persistence issues
Try to separate the model and its services
so that it is Swing-free
– 17 :: 35 – 0024 Spring 2010
Model/View/Controller
View
tracks what is needed for a particular
perspective of the data e.g., bar chart view
presentation issues
Controller
gets input from the user, and uses appropriate
information from the view to modify the model e.g., get slider value, trigger chart modify
interaction issues
In practice, views and controllers are
implemented with Swing components and listeners
– 17 :: 36 – 0024 Spring 2010
Model/View/Controller
– 17 :: 37 – 0024 Spring 2010
MVC
– 17 :: 38 – 0024 Spring 2010
Pluggable “Look and Feel”
– 17 :: 39 – 0024 Spring 2010
Pluggable Look-and-Feel
http://java.sun.com/docs/books/tutorial/ui/overview/demo.html
– 17 :: 40 – 0024 Spring 2010
Extra part
– 17 :: 41 – 0024 Spring 2010
Applets
JApplet contentPane JButton
– 17 :: 42 – 0024 Spring 2010
Applet Methods
– 17 :: 43 – 0024 Spring 2010
Application + Applet
JApplet contentPane JPanel JFrame JButton
- r
Browser Command line
– 17 :: 44 – 0024 Spring 2010
Applet Security
– 17 :: 45 – 0024 Spring 2010
Java 2D API
– 17 :: 46 – 0024 Spring 2010
Graphics
JButton
– 17 :: 47 – 0024 Spring 2010
Coordinate System
(0,0) (width,0) (0,height) (width, height)
– 17 :: 48 – 0024 Spring 2010
Painting Components
JButton JPanel
– 17 :: 49 – 0024 Spring 2010
Painting in Java
– 17 :: 50 – 0024 Spring 2010
Graphics Primitives
label
http://java.sun.com/docs/books/tutorial/2d/geometry/primitives.html
– 17 :: 51 – 0024 Spring 2010
Graphics Attributes
– 17 :: 52 – 0024 Spring 2010
Code
Hello World
To paint the inside of a component,
- verride the paintComponent
– 17 :: 53 – 0024 Spring 2010
Creating and Drawing to an Image
http://java.sun.com/docs/books/tutorial/2d/images/drawonimage.html
– 17 :: 54 – 0024 Spring 2010
Reading/Loading Image
– 17 :: 55 – 0024 Spring 2010