Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java....
-
Upload
mary-gibbs -
Category
Documents
-
view
217 -
download
1
Transcript of Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java....
Swing Hierarchy
Swing classes derived from JComponent will be lightweight,
written entirely in Java.
The top-level Swing windows are
heavyweight. They depend on the native
system.
javax.swing
Component
Container
Panel
WindowFrame
Dialog
Object java.lang
JApplet
java.awt
java.applet Applet
JDialog
JFrame
JWindow
JComponent
Class
extends
Key
Abstract Class
implements
Interface
Package
Swing Programs
• Four basic types of Top Level Window– JFrame, a top level window decorated like a native
window– JWindow, an undecorated stand-alone window (splash-
screen)– JApplet, an embeddable applet– JDialog, a popup dialog window
• Each program type is implemented as a framework class
Swing Hierarchy (Part II)
Swing components names start with ‘J’.
JButton
JMenuItem
Class
extends
Key
Abstract Class
implements
Interface
Package
Objectjava.lang
JComponent
java.awt
Component
Container
avax.swing
AbstractButton
JToggleButton
JMenu
JLabel
JTextComponent
JTextField
JTextArea
JPasswordField
JCheckbox
JRadioButton
JPopupMenu
JPanel
JMenuBar
JScrollPane
JList
JOptionPane
Event Classes
Components Events DescriptionButton, JButton ActionEvent User clicked buttonCheckBox, JCheckBox ItemEvent User toggled a checkboxCheckboxMenuItem, JCheckboxMenuItem ItemEvent User toggled a checkboxChoice, JPopupMenu ItemEvent User selected a choiceComponent, JComponent ComponentEvent Component was moved or resized FocusEvent Component acquired or lost focus KeyEvent User typed a key MouseEvent User manipulated the mouseContainer, JContainer ContainerEvent Component added/removed from containerList, JList ActionEvent User double-clicked a list item ItemEvent User clicked a list itemMenu, JMenu ActionEvent User selected menu itemScrollbar, JScrollbar AdjustmentEvent User moved scrollbarTextComponent, JTextComponent TextEvent User edited textTextField, JTextField ActionEvent User typed Enter keyWindow, JWindow WindowEvent User manipulated window
AWT events for each type of component.AWT events for each type of component.
New Swing Event Classes
Component Events DescriptionJPopupMenu PopupMenuEvent User selected a choiceJComponent AncestorEvent An event occurred in an ancestorJList ListSelectionEvent User double-clicked a list item ListDataEvent List's contents were changedJMenu MenuEvent User selected menu itemJTextComponent CaretEvent Mouse clicked in text UndoableEditEvent An undoable edit has occurredJTable TableModelEvent Items added/removed from table TableColumnModelEvent A table column was movedJTree TreeModelEvent Items added/removed from tree TreeSelectionEvent User selected a tree node TreeExpansionEvent User changed tree nodeJWindow WindowEvent User manipulated window
Newly defined Swing eventsNewly defined Swing events..
GUI Design: Choosing Basic Components Swing objects for input, output, control, Swing objects for input, output, control,
guidance:guidance: GuidanceGuidance: A : A JLabelJLabel displays a short string of displays a short string of
text or an image. It can serve as a prompt. text or an image. It can serve as a prompt. InputInput: A : A JTextFieldJTextField allows editing of a single allows editing of a single
line of text. It can get the user’s input.line of text. It can get the user’s input. OutputOutput: A : A JTextAreaJTextArea allows editing of allows editing of
multiple lines of text. We’ll use it to display multiple lines of text. We’ll use it to display results.results.
ControlControl: A : A JButtonJButton is an action control. By is an action control. By implementing the implementing the ActionListenerActionListener interface we interface we will handle the user's action eventswill handle the user's action events..
Types of Layout Managers
Manager Descriptionjava.awt.BorderLayout Arranges elements along the north, south, east, west, and in the center of the container.java.swing.BoxLayout Arranges elements in a single row or single column.java.awt.CardLayout Arranges elements like a stack of cards, with one visible at a time.java.awt.FlowLayout Arranges elements left to right across the
container.java.awt.GridBagLayout Arranges elements in a grid of variable sized cells (complicated).java.awt.GridLayout Arranges elements into a two-dimensional grid of equally sized cells.java.swing.OverlayLayout Arranges elements on top of each other.
Default Layout Managers
Container Layout ManagerJApplet BorderLayout (on its content pane)JBox BoxLayoutJDialog BorderLayout (on its content pane)JFrame BorderLayout (on its content pane)JPanel FlowLayoutJWindow BorderLayout (on its content pane)
In AWT, the default layout for applets was
FlowLayout.
Top-level windows use BorderLayout
• JTextField method setFont (inherited by JTextField indirectly from class Component) sets the font of the JTextField to a new Font (package java.awt).
• String passed to the JCheckBox constructor is the checkbox label that appears to the right of the JCheckBox by default.
• When the user clicks a JCheckBox, an ItemEvent occurs. – Handled by an ItemListener object, which must implement method
itemStateChanged. • An ItemListener is registered with method addItemListener.• JCheckBox method isSelected returns true if a JCheckBox is
selected.
(C) 2010 Pearson Education, Inc. All rights reserved.
(C) 2010 Pearson
Education, Inc. All rights reserved.
(C) 2010 Pearson
Education, Inc. All rights reserved.
(C) 2010 Pearson
Education, Inc. All rights reserved.
(C) 2010 Pearson
Education, Inc. All rights reserved.
(C) 2010 Pearson
Education, Inc. All rights reserved.
(C) 2010 Pearson
Education, Inc. All rights reserved.
(C) 2010 Pearson
Education, Inc. All rights reserved.
(C) 2010 Pearson
Education, Inc. All rights reserved.
(C) 2010 Pearson
Education, Inc. All rights reserved.
(C) 2010 Pearson
Education, Inc. All rights reserved.
(C) 2010 Pearson
Education, Inc. All rights reserved.
(C) 2010 Pearson
Education, Inc. All rights reserved.
(C) 2010 Pearson
Education, Inc. All rights reserved.
(C) 2010 Pearson
Education, Inc. All rights reserved.
(C) 2010 Pearson
Education, Inc. All rights reserved.
Painting
• When a GUI needs to change its visual appearance it performs a paint operation
• Swing components generally repaint themselves as needed
• Painting code executes on the event-dispatching thread– If painting takes a long time, no events will be
handled during that time
Example
import javax.swing.*; import java.awt.*;
public class Painting extends JPanel { public Painting() {}
public void paintComponent(Graphics g) {super.paintComponent(g);g.setColor( Color.yellow ); g.fillOval( 10,10,50,50 );g.setColor( Color.black ); g.drawOval( 10,10,50,50 );
}
public static void main( String args[] ) {JFrame win = new JFrame( "Painting" );win.setSize(100, 100);win.getContentPane().add( new Painting() );win.setvisible(true);
}}
The Graphics Object
• The Graphics object both a context for painting and methods for performing the painting.
• The graphics context consists of state such as the current painting color, the current font, and the current painting area– The color and font are initialized to the foreground color
and font of the component just before the invocation of paintComponent
The Coordinate System
• Each component has its own integer coordinate system– Ranging from (0, 0) to (width - 1, height - 1)– Each unit represents the size of one pixel
Color
• Combinations of Red, Green, Blue• Each [0, 255]
• Java: new Color(255, 150, 0)
Hokie Orange
Font
• Color and font:• g2.setColor( new Color(r,g,b) );• g2.setFont( new Font(…) );
• new font(“Serif”, Font.BOLD, Font.Italic, 18);
Re-Paint
• Screen is like a painter’s canvas• All windows paint on the same surface!• Windows don’t “remember” whats under them
• Need to re-paint when portions are newly exposed
• Receive Repaint events• Open, resize, bring to front• When other windows in front move, resize, close
MVC Paradigm
• The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller.
Users interact with a controller (e.g., buttons), and make changes to the model (e.g., data), which is then reflected in the view (e.g., graph).
A --> 25 %B --> 60 %C --> 15 %
Model
View(s)
Draw Graph
Control
MVC Relationships
Model View
Controller
MVC Relationships
The Controller has references to both the view and the model; the controller is the base of the triad.
The Model might have a reference to the view, to allow for notification of changes. The model does not know who controls it, however.
The View has a reference to the model, for gathering data. View also has reference to the Controller, to notify of successful updates. (References are minimal, perhaps to a base class, to allow swapping of controls.)
(C) 2010 Pearson Education, Inc. All rights reserved.
• Many event-listener interfaces contain multiple methods.
• An adapter class implements an interface and provides a default implementation (with an empty method body) of each method in the interface.
• You extend an adapter class to inherit the default implementation of every method and override only the method(s) you need for event handling.
(C) 2010 Pearson Education, Inc. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.
• Interface MouseWheelListener enables applications to respond to the rotation of a mouse wheel.
• Method mouseWheelMoved receives a MouseWheelEvent as its argument.
• Class MouseWheelEvent (a subclass of Mouse-Event) contains methods that enable the event handler to obtain information about the amount of wheel rotation.
(C) 2010 Pearson Education, Inc. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.
• Class Point (package java.awt) represents an x-y coordinate. – We use objects of this class to store the coordinates of each mouse
drag event.
• Class Graphics is used to draw. • MouseEvent method getPoint obtains the Point
where the event occurred. • Method repaint (inherited from Component) indicates
that a Component should be refreshed on the screen as soon as possible.
(C) 2010 Pearson Education, Inc. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.
• Graphics method fillOval draws a solid oval. – Four parameters represent a rectangular area (called the bounding
box) in which the oval is displayed. – The first two are the upper-left x-coordinate and the upper-left y-
coordinate of the rectangular area. – The last two represent the rectangular area’s width and height.
• Method fillOval draws the oval so it touches the middle of each side of the rectangular area.
(C) 2010 Pearson Education, Inc. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.