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 javax.swing.event.*; � 0024 Spring 2010 � – 17 :: 4 – �
Swing vs. AWT � 0024 Spring 2010 � – 17 :: 5 – � (c) O ʼ Reilly 1999 �
Swing � 0024 Spring 2010 � – 17 :: 6 – �
Top-level containers � 0024 Spring 2010 � – 17 :: 7 – � http://java.sun.com/docs/books/tutorial/ui/features/components.html �
Containers � containers � 0024 Spring 2010 � – 17 :: 8 – �
General-purpose containers � 0024 Spring 2010 � – 17 :: 9 – �
Basic controls � 0024 Spring 2010 � – 17 :: 10 – �
Essential Swing components � AWT � Swing � 0024 Spring 2010 � – 17 :: 11 – �
Example 1 � pack() causes a window to be sized to fit the preferred size and layouts of its sub- components � 0024 Spring 2010 � – 17 :: 12 – �
Example 2 � In this example how a custom frame is created � 0024 Spring 2010 � – 17 :: 13 – �
Build from bottom up � JButton � JLabel � JPanel � JFrame � 0024 Spring 2010 � – 17 :: 14 – �
Layout managers � Organizing Layout of components in a container � FlowLayout � GridLayout � null � none, Left to right, � programmer Top to bottom � sets x,y,w,h � GridBagLayout � BorderLayout � CardLayout � n � w � e � JButton � c � One at a time � s � 0024 Spring 2010 � – 17 :: 15 – �
Combinations � JButton � JButton � JTextArea � 0024 Spring 2010 � – 17 :: 16 – �
Combinations � JButton � JButton � JFrame � n � JPanel: FlowLayout � JPanel: BorderLayout � c � JTextArea � 0024 Spring 2010 � – 17 :: 17 – �
Code: null layout � press me 0024 Spring 2010 � – 17 :: 18 – �
Code: FlowLayout � press me then me 0024 Spring 2010 � – 17 :: 19 – �
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 ); } } 0024 Spring 2010 � – 17 :: 20 – �
Box Layout � myPane.setLayout(new BoxLayout(myPane, BoxLayout.PAGE_AXIS)); � Component.CENTER_ALIGNMENT � Component.RIGHT_ALIGNMENT � Component.LEFT_ALIGNMENT � 0024 Spring 2010 � – 17 :: 21 – �
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 � 0024 Spring 2010 � – 17 :: 22 – �
Types of event listeners � Act that results in event Listener type ActionListener User clicks a button, presses Return while typing in a text field, or chooses a menu item User closes a frame (main window) WindowListener User presses a mouse button while the cursor is over MouseListener a component User moves the mouse over a component MouseMotionListener Component becomes visible ComponentListener Component gets the keyboard focus FocusListener Table or list selection changes ListSelectionListener 0024 Spring 2010 � – 17 :: 23 – �
Handling events I � Listener � JButton � JLabel � JPanel � JFrame � 0024 Spring 2010 � – 17 :: 24 – �
Event handling II � 0024 Spring 2010 � – 17 :: 25 – �
Example using local class � 0024 Spring 2010 � – 17 :: 26 – �
JDialog � 0024 Spring 2010 � – 17 :: 27 – �
Dialog Example � The JOptionPane class can be used to create simple modal dialogs (icons, title, text and buttons can be customized). � 0024 Spring 2010 � – 17 :: 28 – �
Swing and Threads � 0024 Spring 2010 � – 17 :: 29 – �
Safe Swing code � Unsafe to update state of Swing components Safe to update state of Swing components 0024 Spring 2010 � – 17 :: 30 – �
Event Dispatching � javax.swing.SwingUtilities.invokeLater( new Runnable(){ public void run(){ // Access to components } } ); 0024 Spring 2010 � – 17 :: 31 – �
Handle a job in the background � 0024 Spring 2010 � – 17 :: 32 – �
Model/View/Controller [MVC] � http://www.itu.dk/courses/VOP/E2005/VOP2005E/8_mvc_krasner_and_pope.pdf � 0024 Spring 2010 � – 17 :: 33 – �
Model/View/Controller � Model � complete, self-contained representation of object 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 � 0024 Spring 2010 � – 17 :: 34 – �
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 � 0024 Spring 2010 � – 17 :: 35 – �
Model/View/Controller � 0024 Spring 2010 � – 17 :: 36 – �
MVC � 0024 Spring 2010 � – 17 :: 37 – �
Pluggable “Look and Feel” � 0024 Spring 2010 � – 17 :: 38 – �
Pluggable Look-and-Feel � http://java.sun.com/docs/books/tutorial/ui/overview/demo.html � 0024 Spring 2010 � – 17 :: 39 – �
Extra part � 0024 Spring 2010 � – 17 :: 40 – �
Applets � JApplet � contentPane � JButton � 0024 Spring 2010 � – 17 :: 41 – �
Applet Methods � 0024 Spring 2010 � – 17 :: 42 – �
Application + Applet � Command line � Browser � or � JFrame � JApplet � contentPane � JPanel � JButton � 0024 Spring 2010 � – 17 :: 43 – �
Applet Security � 0024 Spring 2010 � – 17 :: 44 – �
Java 2D API � 0024 Spring 2010 � – 17 :: 45 – �
Graphics � JButton � 0024 Spring 2010 � – 17 :: 46 – �
Coordinate System � (0,0) (width,0) (0,height) (width, height) 0024 Spring 2010 � – 17 :: 47 – �
Painting Components � JButton � JPanel � 0024 Spring 2010 � – 17 :: 48 – �
Painting in Java � 0024 Spring 2010 � – 17 :: 49 – �
Graphics Primitives � http://java.sun.com/docs/books/tutorial/2d/geometry/primitives.html � label 0024 Spring 2010 � – 17 :: 50 – �
Graphics Attributes � 0024 Spring 2010 � – 17 :: 51 – �
Code � Hello World � To paint the inside of a component, override the paintComponent � 0024 Spring 2010 � – 17 :: 52 – �
Creating and Drawing to an Image � http://java.sun.com/docs/books/tutorial/2d/images/drawonimage.html � 0024 Spring 2010 � – 17 :: 53 – �
Reading/Loading Image � 0024 Spring 2010 � – 17 :: 54 – �
How to proceed … � 0024 Spring 2010 � – 17 :: 55 – �
Recommend
More recommend