Download Applets

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter 18
Applets
Slides prepared by Rose Williams, Binghamton University
Icons
• An icon is a picture
– It is typically, but not always, a small picture
• An icon can be stored in a file of many different
standard formats
– Such as .gif, .tiff, or .jpg
• The class ImageIcon is used to convert a picture
file to a Swing icon
– Then it can be added as a component to any Container
class, such as JApplet
– The class ImageIcon is in the javax.swing package
ImageIcon NameOfImageIcon = new
ImageIcon("PictureFileName");
© 2004 Pearson Addison-Wesley. All rights reserved
18-2
Adding Icons to an Applet
• The easiest way to display an icon in an applet is to
place it in a JLabel
• The following three lines create a label, create an
icon, and then add the icon to the label:
JLabel aLabel=new JLabel("Welcome to my applet.");
ImageIcon dukeIcon = new
ImageIcon("duke_waving.gif");
aLabel.setIcon(dukeIcon);
• The character pictured in this icon is named Duke
– He is Sun Microsystem's mascot for the Java language
© 2004 Pearson Addison-Wesley. All rights reserved
18-3
An Applet with an Icon (Part 1 of 3)
© 2004 Pearson Addison-Wesley. All rights reserved
18-4
An Applet with an Icon (Part 2 of 3)
© 2004 Pearson Addison-Wesley. All rights reserved
18-5
An Applet with an Icon (Part 3 of 3)
© 2004 Pearson Addison-Wesley. All rights reserved
18-6
Inserting an Applet in an HTML
Document
• An applet can be placed in an HTML
document with an applet tag:
<applet code="PathToApplet"
width=Number1 height=Number2>
</applet>
• If given a .class file name only, then the
HTML file and the applet file must be in the
same directory
– The PathToApplet can be a full or relative
path name
© 2004 Pearson Addison-Wesley. All rights reserved
18-7
Inserting an Applet in an HTML
Document
• Note that the name of the .class file, not the
.java file, is given
• Note also that the width and height of the applet is
given in this command, and not within the applet
class definition
– The width and height are in pixels
• The following code, when placed in an HTML
document, will display the calculator applet in a
browser as shown
<applet code="AppletCalculator.class"
width=400 height=300>
</applet>
© 2004 Pearson Addison-Wesley. All rights reserved
18-8
An Applet in an HTML Document
<html>
<head>
<title>
Vampire Control
</title>
</head>
. . .
<applet code="AppletCalculator.class"
width=400 height=300>
</applet>
. . .
</html>
© 2004 Pearson Addison-Wesley. All rights reserved
18-9
Browser View
© 2004 Pearson Addison-Wesley. All rights reserved
18-10
Pitfall: Using an Old Web Browser
• An old browser may not be able to run
applets from an HTML document
– Even if a java application runs correctly on the
same system
• Web browsers do not use the same Java
Virtual Machine used to run regular Java
applications
– An old browser will have an old Java Virtual
Machine, or perhaps, no Java Virtual Machine
• However, an applet viewer will work, as long
as a recent version of Java is installed
© 2004 Pearson Addison-Wesley. All rights reserved
18-11
Applets and Security
• An applet can be a program, written by
someone else, that runs on your computer
• Whenever someone else's program runs on
your computer, there are security questions
you should ask:
– Will it read information from your files?
– Will it corrupt your operating system?
Applets are designed so that they cannot do
any of these things (at least easily)
© 2004 Pearson Addison-Wesley. All rights reserved
18-12
Handling Mouse Events
•
The following example illustrates how mouse events can be responded to.
– It also shows how a single listener can register with many sources.
•
The event listener in this case will implement the MouseListener interface.
•
MouseListener consists of five methods:
void
void
void
void
void
•
mouseClicked (MouseEvent me); // the mouse has been clicked on a component.
mouseEntered (MouseEvent me); // the mouse enters a component
mouseExited (MouseEvent me); // the mouse exits a component
mousePressed (MouseEvent me); // a mouse button has been pressed on a component
mouseReleased (MouseEvent me);// a mouse button has been released on a component
Note: You are highly encouraged to check out details on implementing
the KeyListener interface that defines keyboard events. The
WindowListener interface shall be discussed in the next Chapter.
© 2004 Pearson Addison-Wesley. All rights reserved
18-13
Example
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MouseEventTest extends JFrame {
JButton button = new JButton("Press Me");
JLabel label = new JLabel( "Running Total:");
JTextField textField = new JTextField(10);
public MouseEventTest(){
super("A Container With Components");
setSize(300,100);
setLayout(new FlowLayout());
add(label); add(textField); add (button);
setVisible(true);
class LightUpListener extends MouseAdapter {
public void mouseEntered(MouseEvent e) {
Component c = (Component)e.getSource();
c.setBackground(Color.green);
}
public void mouseExited(MouseEvent e) {
Component c = (Component)e.getSource();
c.setBackground(Color.red);
}
}
MouseListener listener = new LightUpListener();
button.addMouseListener(listener);
textField.addMouseListener(listener);
getContentPane().addMouseListener(listener);
}
public static void main(String[] args) {
new MouseEventTest();
}}
© 2004 Pearson Addison-Wesley. All rights reserved
18-14