Date post: | 04-Jan-2016 |
Category: |
Documents |
Upload: | monica-terry |
View: | 224 times |
Download: | 1 times |
Swing
SwingDifferences between Swing and AWT
Naming Conventions
All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
Most Swing Components are “Lightweight”
•Use Java code rather than native code to draw in the underlying window.
•The Exceptions are JFrame, JApplet, JWindow, and JDialog
Use Content Pane for adding Components to “Heavyweight” Containers
To add Components to a JApplet (in method init( )) use
Container cp = new getContentPane( );
cp.add(new JButton(“Start”);
Swing
Differences between Swing and AWT Components
Use of paintComponent for Drawing
public void paintComponent(Graphics g) {
super.paintComponent(g);
//always call super.paintComponent( ) before performing custom drawing
// draw Shape on the Swing Component
}
Double Buffering
In Swing a JPanel is used instead of a Canvas to be the target for drawing graphics objects. When drawing to a JPanel using paintComponent double buffering is automatically employed. There is an option to perform drawing directly to the screen using the paint method by using panel.getGraphics( );
SwingAdditional features of Swing Components
All of the “lightweight” Swing classes inherit from JComponent. The four “heavyweight” components – JFrame, JApplet, JWindow, and JDialog – inherit from their AWT counterparts.
Unlike their AWT counterparts, Swing uses setXXX( ) and getXXX( ) to access private attributes. This designation is a bit easier for users to remember, and lends itself to the documentation conventions used by Java Beans and class Introspector( ) to display information about a bean.
The default layout manager for a JApplet is BorderLayout.
Swing
In many ways Swing is an improvement upon the original Java design and the AWT…
BUT
Many browsers (none before Netscape 6.0) cannot run the most current versions of the JRE (Java Runtime Environment) and will not run applets using swing components. If you want your applet to be viewable by the widest audience use AWT components.
SwingThe Java Plug-In
A plug-in is available from Sun that, once installed, allows browsers to run the most current version of the JRE. This allows you to deliver applets that use the more recent JRE classes (like Swing and 2D-graphics) that can run over Netscape and IE browsers
This capability comes at a price!
The plug-in is 5 MB, and downloading the plug-in is not a viable solution for an internet client.
The plug-in may be obtained at:
http://java.sun.com/products/plugin/
SwingExample 1 – an applet with 3 buttons and 2 text fields
Step1 – Design the interface
Read Write Clear
Enter text
Correct Entry?
Applet
The JApplet will consist of 2 JPanels –
The top one containing labels and text fields
The bottom one containing three JButtonsThe browser will add a title bar when the applet is running
Swing
Read Write Clear
Enter text
Correct Entry?
Applet
The top Panel will use a GridLayout with 2 rows and 2 columns
(with a separation of 10 pixels)
Swing
//(global) attributes of the class
private MyJTextField source = new MyJTextField(25);
private MyJTextField target = new MyJTextField(25);
private JLabel edit = new JLabel("Enter text");
private JLabel verify = new JLabel("Correct Entry?");
private String inStr = new String("");
//method to construct the top panel
private JPanel makeJTextPanel( ) {
JPanel theText = new JPanel( );
theText.setLayout(new GridLayout(2,2, 10, 10));
theText.add(edit);
theText.add(source);
theText.add(verify);
theText.add(target);
return theText;
}
Construct the top panel – write a method makeJTextPanel( )
Add components to the grid – left to right, top to bottom
Swing
Read Write Clear
Enter text
Correct Entry?
Applet
Construct the bottom Panel – Use FlowLayout (center adjusted)
Top JPanel
Bottom JPanel
SwingConstruct the bottom panel – with a method
private MyJButton read;
private MyJButton write;
private MyJButton clear;
//build the bottom panel with method makeJButtonPanel( )
private JPanel makeJButtonPanel() {
read = new MyJButton ("Read", source);
write = new MyJButton("Write", target);
clear = new MyJButton("Clear", target);
JPanel theButtons = new JPanel( );
theButtons.setLayout(new FlowLayout(FlowLayout.CENTER));
theButtons.add(read);
theButtons.add(write);
theButtons.add(clear);
theButtons.setBackground(Color.blue);
return theButtons;
}
Create the bottom panel
Set its layout and color, and add the buttons to it (left to right)
SwingPlace the panels into the applet in method init( )
public void init( ) {
Container cp = getContentPane( );
theButtons = makeJButtonPanel( );
cp.add(BorderLayout.SOUTH, theButtons);
theText = makeJTextPanel( );
cp.add(BorderLayout.CENTER, theText);
}
For heavyweight component like JApplet components must be added to a ContentPane
//add theButtons (a JPanel) to the ContentPane
//the default LayoutManager of a JApplet is BorderLayout
SwingDesign of the System
A button event will cause a read, write, or clear of a text field.
JPanel
JApplet
MyJTextField
MyJTextField(int size)
doButtonAct(MyJButton)
String response
MyJButtonJLabel
MyJButton(String,
MyJTextField)
actionPerformed(ActionEvent)
MyJTextField target
ActionListener
activates
The applet is an aggregate of 2 JPanels
2
32
describes
JPanels contain JLabels and MyJTextField objects OR MyJButtons
MyJButton objects interact with MyJTextField objects
Swing
class MyJButton extends JButton implements ActionListener {
private MyJTextField target;
public MyJButton(String name, MyJTextField targ) {
Font buttonFont = new Font ("Times Roman", Font.BOLD, 12);
setFont(buttonFont);
setText(name);
setBackground(Color.cyan);
target = targ;
addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
target.doButtonAct(this);
}
}
class MyJButton
Constructor receives handle to text field
Hold handle to target text field
Event causes message to be sent to the target to perform a read or write
Swing
class MyJTextField extends JTextField {
private String response = new String( );
public MyJTextField(int size) {
super(size);
}
public void doButtonAct(MyJButton source) {
response = source.getText( );
if (response.equals("Read") )
inStr = super.getText( );
else if (response.equals("Write") )
setText(inStr);
else if (response.equals("Clear") )
setText("");
}
}
A MyJButton object notifies a MyJTextField of a button event and elicits an action
Swing
The Applet JButtons -- implementation of this design JButtons.html
SwingAdding scroll bars to a component such as a JTextArea
Just wrap a JTextArea object inside a JScrollPane.
JTextArea ta = new JTextArea(10, 25);
JScrollPane sp = new JScrollPane(ta);
The programmer can control which scrollbars are allowed – vertical, horizontal, both, or neither
JScrollPane sp = new JScrollPane(ta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Swing
Example 2. Drawing Shapes on a JPanel
Swing
ArcLine Ellipse Rectangle Rounded Rectangle Circle
RGB Colors
R G B
JApplet MainPanel
Add a JButtonPanel to the NorthAdd a JPanel to the East that….Contains a JLabel Panel and…A JScrollBar PanelFinally add a JPanel to the Center (whatever is left with no West and South) to serve as a canvas on which you will draw the shapes.Remember! You must get a ContentPane from the JApplet on which you will add these panels.
Swing
The applet will contain the following classes:
MyCanvas – a JPanel that will be the object where the shapes will be placed. This class will need to hold the shape objects in a container, implement the method paintComponent( ) for painting the contents of this container, and discover when and which JButton and JScrollBar events occur and handle the repainting commands that they require.
MyJScrollBar – Three scroll bars with adjustment listeners handle adjustment events and notify the MyCanvas object to change its background color.
MyJButton – Butttons with action listeners respond to an event by notifying the MyCanvas object to add a new shape to its collection and indicate the specific shape that has been requested.
A class Shape that is extended by such classes as Line, Ellipse, Rectangle, etc. – These classes hold color and size information and provide a draw method for locating and painting the shape on the canvas.
Swing
Class Shape uses a BoundsBox object, that is located and adjusted by pressing and dragging a mouse on the MyCanvas object, to determine the location and size parameters of each shape that is drawn.
The class MyCanvas object must be notified of and respond to button and scroll bar events. It must also implement the MouseListener and MouseMotionListener interfaces to respond to mousePressed( ) and mouseDragged( ) events that determine the location and extent of the BoundsBox object.
Classes MyJButton and MyJScrollBar must maintain a permanent handle to the MyCanvas object that they must notify when one of their events occur.
Swing
Here is the applet that implements the previous design
Draw Shapes