java/jdk/sdk/…!!!bpg/teaching/it82/handouts/js_2up.pdf · 2 spring 2003 © university of stirling...
TRANSCRIPT
1
Spring 2003© University of Stirling
IT82: Multimedia
Spring 2003© University of Stirling
IT82: Multimedia
Java/JDK/SDK/…!!!Java language Development Tools
Java 1.0 JDK 1.0.*
Java 1.1 JDK 1.1.*
Java 2 JDK/SDK 1.2
SDK 1.3
SDK 1.4
2
Spring 2003© University of Stirling
IT82: Multimedia
JFC and SwingJava Foundation Classes (JFC)
• Accessibility API
• Java2D API
• Drag and Drop API
Swing:
• Swing components
• Pluggable Look and Feel
only available
in Java 2
Spring 2003© University of Stirling
IT82: Multimedia
JFC
Accessibility API• allows assistive technologies to be used with Java
components– e.g. screen readers, screen magnifiers, speech recognition
Java2D API• more advanced graphics drawing facilities
– e.g. more sophisticated geometry, transformations, paintingstyles, image processing
Drag-and-Drop API• allows dragging and dropping between Java and non-
Java applications
3
Spring 2003© University of Stirling
IT82: Multimedia
JFC
Swing• interface components written in Java• non-window-system-specific• customizable
Pluggable Look-and-Feel• switch the look of a suite of applications without
having to re-jig the components of each one• a number of standard looks and feels provided
Spring 2003© University of Stirling
IT82: Multimedia
4
Spring 2003© University of Stirling
IT82: Multimedia
Spring 2003© University of Stirling
IT82: Multimedia
5
Spring 2003© University of Stirling
IT82: Multimedia
Swing and AWT• Swing has versions of all the components supplied by
the Abstract Windowing Toolkit (AWT)• …plus lots more!• Swing components are typically more specialized
versions of (are subclasses of) AWT components,e.g.
Spring 2003© University of Stirling
IT82: Multimedia
AWT• One big difference is in the hierarchies of containers:
• …and you can do (eg) add(myBigRedButton)
6
Spring 2003© University of Stirling
IT82: Multimedia
Swing
getContentPane().add(myBigRedButton);
Spring 2003© University of Stirling
IT82: Multimedia
Visualizing the Panes
7
Spring 2003© University of Stirling
IT82: Multimedia
Swing Components
Containers• Top-Level• General-Purpose• Special-Purpose
Atomic Components• Basic Controls• Information Displays
– Uneditable– Editable
Spring 2003© University of Stirling
IT82: Multimedia
Top-level ContainersAt the top of any containment hierarchy, you will findone of these:
Applet Dialogue Frame
JApplet JDialog JFrameJOptionPane
8
Spring 2003© University of Stirling
IT82: Multimedia
General-purpose Containers(Intermediate containers of many uses)
Toolbar Panel
JToolBar JPanel
Split Pane Scroll Pane Tabbed Pane
JSplitPane JScrollPane JTabbedPane
Spring 2003© University of Stirling
IT82: Multimedia
Special-purpose Containers(Intermediate containers with specific roles)
Internal Frame Layered Pane
JInternalFrame JLayeredPaneRoot Pane
JRootPane
9
Spring 2003© University of Stirling
IT82: Multimedia
Controls
Jbutton JList JComboBoxMenu Slider Text field
JMenu JSlider JTextFieldJMenuItem
ButtonsList
Combo Box
Spring 2003© University of Stirling
IT82: Multimedia
Information Displays(uneditable, solely to give information)
Label Progress Bar ToolTip
JLabel JProgressBar JToolTip
10
Spring 2003© University of Stirling
IT82: Multimedia
InformationDisplays
(editable, formatted)
JFileChooser JColorChooserTable Text Tree
JTable JTextArea JTree
Colour Chooser
File Chooser
Spring 2003© University of Stirling
IT82: Multimedia
Layout Managers(standard ones)
Managers available:• Flow• Card• Border• Box• Grid• GridBag (complicated!)• customized
11
Spring 2003© University of Stirling
IT82: Multimedia
BoxLayout BorderLayout
GridBagLayoutGridLayout
Spring 2003© University of Stirling
IT82: Multimedia
A Whole Lot of Listeners
HyperlinkListener InternalFrameListenerMenuListener MouseListener ItemListenerKeyListener MouseInputListenerListSelectionListener TreeModelListenerListDataListener MenuKeyListenerMenuDragMouseListener MouseMotionListenerTableColumnModelListener PopupMenuListenerTableModelListener TreeExpansionListenerTreeSelectionListener UndoableEditListenerTreeWillExpandListener WindowListener
ActionListener AncestorListener
CaretListener
CellEditorListener
ChangeListener
ComponentListener
ContainerListener
DocumentListener
FocusListener
12
Spring 2003© University of Stirling
IT82: Multimedia
Example SwingApplication
Spring 2003© University of Stirling
IT82: Multimedia
import javax.swing.*; import java.awt.*;import java.awt.event.*;
public class SwingApplication extends JFrame implements ActionListener {
private String labelPrefix = "Number of button clicks: "; JLabel label; private int numClicks = 0;
public SwingApplication() { label = new JLabel(labelPrefix + "0 ", JLabel.CENTER); JButton button = new JButton("I'm a Swing button!", new ImageIcon("duke.gif"));
button.addActionListener(this); JPanel panel = new JPanel(); // adds a space of 30 pixels around the edge of the panel panel.setBorder(BorderFactory. createEmptyBorder(40,40,40,40)); panel.setLayout(new GridLayout(2,1)); panel.add(button); panel.add(label); getContentPane().add(panel, BorderLayout.CENTER); }
13
Spring 2003© University of Stirling
IT82: Multimedia
public void actionPerformed(ActionEvent e) { numClicks++; label.setText(labelPrefix + numClicks); }
public static void main(String[] args) { // Create the top-level frame JFrame frame = new JFrame("SwingApplication"); SwingApplication app = new SwingApplication();
// Make sure the frame can exit app.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); app.setSize(600,400); app.setVisible(true); }}
Spring 2003© University of Stirling
IT82: Multimedia
Practical Swing Conversion Tips
• Don’t forget the import javax.swing.*;• Remember about separate content panes forJFrame
• Don’t call the paint method directly - usepaintComponent
• Stick to either AWT or Swing - in particular,– Don’t mix heavy and light components
14
Spring 2003© University of Stirling
IT82: Multimedia
Heavy and Light
• “Heavy” components have their own screenresources, “light” components borrow them– AWT components are heavy– Swing components are light
• Mixing them causes problems, because of:– lightweights can be transparent, heavies are opaque– heavies always appear rectangular
– mouse events on a lightweight component fall through to theparent of the component - not so with heavies
– heavy components are always on top
Spring 2003© University of Stirling
IT82: Multimedia
Heavy and Light
15
Spring 2003© University of Stirling
IT82: Multimedia
Why use Swing?(in case you weren’t already converted)
• Big range of snazzy (usable too!) features:– e.g. icons on buttons, toolbars, tabbed panes, sliders, file
choosers,…..
• Pluggable Look and Feel gives a range of choice forinterface styles non-window-system-dependent
• Built-in accessibility support
Spring 2003© University of Stirling
IT82: Multimedia
More Swing(Information for if you want to go further into Swing)Many Java books cover some Swing
• One book is by Mary Campione
and Kathy Walrath:
“The JFC Swing tutorial”
• This book is the portable version
• of the online Swing tutorial, athttp://java.sun.com/docs/books/tutorial/uiswing/
• More information than you could ever possibly wantat the sun site, java.sun.com
16
Spring 2003© University of Stirling
IT82: Multimedia
Plug-in Look and Feel SamplesJava MacOS
Windows Motif
Spring 2003© University of Stirling
IT82: Multimedia
Java Look and Feel Guidelines
• Very comprehensive, a valuable resource• Book form, as well as online at
java.sun.com/products/jlf/
• Covers structural design, as well as visual/layoutguidelines
• Detailed guidelines on various topics, including– Interface components– Mouse behaviour– Using graphics
17
Spring 2003© University of Stirling
IT82: Multimedia
Look and Feel Graphics
Spring 2003© University of Stirling
IT82: Multimedia
Extract from Graphics Guidelines
18
Spring 2003© University of Stirling
IT82: Multimedia
End Of Lecture
• Next lecture is on Java2D• Remember: File formats tutorials next week
– NOT the week after, as in original timetable