Advanced Applets(Powerpoint)

Download Report

Transcript Advanced Applets(Powerpoint)

INFSY 547: WEB-BASED
TECHNOLOGIES
Gayle J Yaverbaum, PhD
Professor of Information Systems
Penn State Harrisburg
Adding More Applets
objects to XML
Content
<?xml version="1.0"?>
<!DOCTYPE panelslayoutapplet SYSTEM "PanelLayoutsApplet.dtd">
<jclass>
<app class="panellayoutsapplet.PanelLayoutsApplet“
width="300" height="250">
</app>
<app class="firstapplet.FirstApplet" width="300" height="250">
</app>
<app class="urlapplet.URLApplet" width="300" height="250">
</app>
</jclass>
xsl Code
<TR>
<xsl:for-each select="app">
<TD width=“30%">
<applet>
<xsl:attribute name="code">
<xsl:value-of select="@class"/>
</xsl:attribute>
<xsl:attribute name="width">
<xsl:value-of select="@width"/>
</xsl:attribute>
<xsl:attribute name="height">
<xsl:value-of select="@height"/>
</xsl:attribute>
</applet>
</TD>
</xsl:for-each>
</TR>
Run Saxon
Processor
Applet Considerations
 Javax.swing supplies best classes
 setSize is unnecessary but aids in
debugging
 applet size is set in html
package guisampleapplet;
import javax.swing.JApplet;
import java.awt.*;
public class GUISampleApplet extends JApplet
{
PanelClass p;
public void init ()
{
p = new PanelClass();
}
}
Graphical User Example
package guisampleapplet;
•
Extending our code!
import javax.swing.JApplet;
import java.awt.*;
public class GUISampleApplet extends JApplet
{
PanelClass p;
public void init()
{
p = new PanelClass();
this.setSize(300, 40);
this.add("North", p);
}
}
BorderLayout
package guisampleapplet;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
public class PanelClass extends JPanel {
JButton _button1, _button2, _button3;
JPanel _p;
public PanelClass() {
super();
_p = this;
designPanel();
}
private void designPanel() {
_p.setBackground(Color.blue);
_p.setLayout(new FlowLayout());
_button1 = new CreateAButton(_p, _button1, "Button 1 ",Color.orange);
_button2 = new CreateAButton(_p, _button2, "Button 2 ", Color.GREEN);
_button3 = new CreateAButton(_p, _button3, "Button 3 ", Color.YELLOW);
}
}
package guisampleapplet;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JPanel;
public class CreateAButton extends JButton
{
public CreateAButton (JPanel p, JButton b, String str, Color c)
{
super (str);
this.setBackground(c);
p.add (this);
}
}
Layout
Managers
 In JAVA
• layout is set in setLayout method
 Indicate layout of choice or accept
default
 Add () and remove () methods are
common to all layouts.
Popular Layout Managers
 FlowLayout
 GridLayout
 BorderLayout
Panels
 A layout manager may apply to a panel or
an applet as a whole
 An applet may be divided into panels
which may themselves be divided into
subpanels and so on.
Panels
 Labels, buttons and more may be
added to containers (panels)
 Subsequently, panels are added to
the applet
 Complete all files to run your applet in
a browser
 xml
 dtd
 xsl
 css
 bat
 Run the applet in the browser
Add a class to create a label
 Add labels between each button
 Remove button labels
 Change all button colors
 Line up each button and label –
one/line using GridLayout
(see next slide and page 285 of text)
package gridlayoutexample;
import javax.swing.JApplet;
import javax.swing.JPanel;
import java.awt.*;
public class GridLayoutClass extends JApplet
{
CreateAButton b;
JPanel j;
public void init ()
{
this.setBackground (Color.white);
j = new JPanel ();
j.setLayout(new GridLayout(5, 2));
for (int i=1; i<=10; i++)
{
b= new CreateAButton (j, i, Color.red, Color.white);
j.add (b);
}
this.add(j);
}
}
package gridlayoutexample;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JPanel;
public class CreateAButton extends JButton
{
public CreateAButton (JPanel p, int x, Color c, Color f)
{
super ("Button " + x);
this.setBackground(c);
this.setForeground (f);
}
}
•Code that is called by the system when an
event occurs
•An event signifies an occurrence
•The event generator notifies event handlers
OS Interrupt
Screen
Press Button
Button
Object
(Buttons generate events)
Event Notification
JAVA AWT
Environment:
Messages are sent
between JAVA
Objects
Event
Handling
Object
Text field
Object
Update Text Field
Event Handling and
Associated Objects
 Interface Events Flow
 User clicks a button
 User types text
 User uses a mouse
 When an event occurs, applet is
notified
 Program may take action
Add implements action listener to the class
public class PanelClass extends JPanel implements ActionListener
1. Add to the action code to:
_button1.addActionListener(this);
for designPanel method
2. Do this for all of your buttons
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == _button1)
{
_button1.setBackground (Color.red);
_button2.setBackground (Color.blue);
_button2.setBackground (Color.yellow);
}
}
3. Change all of the colors for each button clicked
to a different color pattern
o Divides the container (either a panel or whole
applet)
o Five areas
- North
-South
-East
-West
-Center
•May also use the horizontal and vertical gaps as
in FlowLayout
North
East
West
Center
South
public void PlacePanels ()
{
setLayout(new BorderLayout(5,10));
.
.
}
Note: 1. may simply be (new BorderLayout ()
2. the 5,10 indicates border space in pixels
•Pressing a mouse button triggers a mouse event
•Rather than implementing ActionListener, you will
implement MouseListener (note that more than one listener
may be implemented)
•When a mouse is pressed, the location is stored in x,y
coordinates and location may be retrieved with getx ()
and gety ().
• MouseEvent handlers are invoked
Example on next slides
demonstrate:




Mouse enter
Mouse exit
Left mouse click
Right mouse click
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class mousedemo extends Applet implements MouseListener
{ //note that this bracket will having matching end bracket next screen
TextField mousetext;
public void init(){
mousetext = new TextField(25);
add(mousetext);
addMouseListener(this); }
public void mousePressed(MouseEvent e){
if((e.getModifiers()& InputEvent.BUTTON1_MASK)
== InputEvent.BUTTON1_MASK){
mousetext.setText("Left mouse button down at "
+ e.getX() + "," + e.getY());
}
else{
mousetext.setText("Right mouse button down at "
+ e.getX() + "," + e.getY());
}
}
public void mouseClicked(MouseEvent e){
mousetext.setText("You clicked the mouse at " +
e.getX() + "," + e.getY());
}
public void mouseReleased(MouseEvent e){
mousetext.setText("The mouse button is up");
}
public void mouseEntered(MouseEvent e){
mousetext.setText("The mouse is in the applet");
}
public void mouseExited(MouseEvent e){
mousetext.setText("The mouse is out of the applet");
}
} //note that this bracket ends the applet from previous page