13-11-2012, 06:14 PM
Swing
swing PPT.ppt (Size: 807.5 KB / Downloads: 118)
Swing vs AWT
AWT is Java’s original set of classes for building GUIs
Uses peer components of the OS; heavyweight
Not truly portable: looks different and lays out inconsistently on different OSs
Due to OS’s underlying display management system
Swing is designed to solve AWT’s problems
99% java; lightweight components
Drawing of components is done in java
Uses 4 of AWTs components
Window, frame, dialog, ?
Lays out consistently on all OSs
Uses AWT event handling
Implementing a Swing GUI
Import javax.swing.*, java.io.*, java.awt.*
Make a specific class to do GUI functions
Specify all the GUI functions/components in the class’s constructor (or methods / classes called by the constructor)
Run the GUI by instantiating the class in the class’s main method
JFrame
Frames are the basis of any Java GUI
Frame is the actual window that encompasses your GUI objects; a GUI can have multiple frames
The “J” prefix is at the beginning of any Swing component’s name (to distinguish them from AWT components)
JFrame is a wrapper around AWT’s Frame
Panes/JPanels
The terms “pane” and “panel” are used interchangeably in Java
If a frame is a window, a pane is the glass
Panes hold a window’s GUI components
Every frame has at least one pane, the default “Content Pane”
Content Pane
When a frame is created, the content pane is created with it
To add a component to the content pane (and thus to the frame), use:
frameName.getContentPane().add(component name);
where frameName is the name of the frame
Text Areas
Specified by Java’s JTextarea class
Multiple constructors allow you to create a new text area with a specified size and/or specified text
A text area is just a white space of variable size that can hold text
If text goes out of the area’s bounds, it will exist but some of it will not be seen
Wrap the text area in a scrollable pane
Model-View-Controller
Design pattern often used in Swing objects
Breaks a GUI object down into three parts
“Model” manages the data used by the object
“View” manages the graphical/textual output of the object
“Controller” interprets user input, commanding the model and view to change as necessary
Other Layout Managers
BorderLayout
Defines five regions: North, South, East, West, and Center
Programmer specifies which objects go to which regions
GridLayout
Programmer defines matrix dimensions; objects are then put in the matrix in the order they are added, left to right, top to bottom