JavaScript DHTML/Development/Applet Jar

Материал из Web эксперт
Перейти к: навигация, поиск

Accessing Applets from within JavaScript

//java source file
import java.applet.*;
import java.awt.*;
public class FancyText extends Applet {
 String text="I like Java!";

 public void paint(Graphics g) {
  g.drawString(text,30,30);
 }
 
 public void setText(String s) {
  text=s;
  repaint();
 }
}
//Loading and Accessing an Applet 
<head>
<TITLE>Accessing Applets</TITLE>
<SCRIPT LANGUAGE="JavaScript"><!--
function accessApplet() {
 setTimeout("changeText("I like JavaScript!")",2000)
 setTimeout("changeText("I like JavaScript and Java!")",4000)
 setTimeout("changeText("I like Java!")",6000)
 setTimeout("accessApplet()",8000)
}
function changeText(s) {
 window.document.fancyText.setText(s)
}
// --></SCRIPT>
</HEAD>
<BODY onLoad="accessApplet()">
<APPLET CODE="FancyText.class" NAME="fancyText"
 WIDTH=450 HEIGHT=150>
[The FancyText Applet]
</APPLET>



A More Fully Scripted Clock

/*
JavaScript Bible, Fourth Edition
by Danny Goodman 
John Wiley & Sons CopyRight 2001
*/
<HTML>
<HEAD>
<TITLE>Clock with Lots o" Widgets</TITLE>
<SCRIPT LANGUAGE="JavaScript1.1">
function setTimeZone(popup) {
    var choice = popup.options[popup.selectedIndex].value
    document.clock2.setTimeZone(choice)
}
function setColor(form) {
    var bg = form.backgroundColor.options[form.backgroundColor.selectedIndex].value
    var fg = form.foregroundColor.options[form.foregroundColor.selectedIndex].value
    document.clock2.setColor(bg, fg)
}
function setFont(form) {
    var fontName = form.theFont.options[form.theFont.selectedIndex].value
    var fontStyle = form.theStyle.options[form.theStyle.selectedIndex].value
    var fontSize = form.theSize.options[form.theSize.selectedIndex].value
    document.clock2.setFont(fontName, fontStyle, fontSize)
}
function getAppletInfo(form) {
    form.details.value = document.clock2.getInfo()
}
function showSource() {
    var newWindow = window.open("ScriptableClock.java","", 
    "WIDTH=450,HEIGHT=300,RESIZABLE,SCROLLBARS")
}
</SCRIPT>
</HEAD>
<BODY>
<APPLET CODE="ScriptableClock.class" NAME="clock2" WIDTH=500 HEIGHT=45>
<PARAM NAME=bgColor VALUE="Black">
<PARAM NAME=fgColor VALUE="Red">
</APPLET>
<P>
<FORM NAME="widgets2">
Select Time Zone:
<SELECT NAME="zone" onChange="setTimeZone(this)">
    <OPTION SELECTED VALUE="Locale">Local Time
    <OPTION VALUE="GMT">Greenwich Mean Time
</SELECT><P>
Select Background Color:
<SELECT NAME="backgroundColor" onChange="setColor(this.form)">
    <OPTION VALUE="White">White
    <OPTION SELECTED VALUE="Black">Black
    <OPTION VALUE="Red">Red
    <OPTION VALUE="Green">Green
    <OPTION VALUE="Blue">Blue
    <OPTION VALUE="Yellow">Yellow
</SELECT>
Select Color Text Color:
<SELECT NAME="foregroundColor" onChange="setColor(this.form)">
    <OPTION VALUE="White">White
    <OPTION VALUE="Black">Black
    <OPTION SELECTED VALUE="Red">Red
    <OPTION VALUE="Green">Green
    <OPTION VALUE="Blue">Blue
    <OPTION VALUE="Yellow">Yellow
</SELECT><P>
Select Font:
<SELECT NAME="theFont" onChange="setFont(this.form)">
    <OPTION SELECTED VALUE="TimesRoman">Times Roman
    <OPTION VALUE="Helvetica">Helvetica
    <OPTION VALUE="Courier">Courier
    <OPTION VALUE="Arial">Arial
</SELECT><BR>
Select Font Style:
<SELECT NAME="theStyle" onChange="setFont(this.form)">
    <OPTION SELECTED VALUE="Plain">Plain
    <OPTION VALUE="Bold">Bold
    <OPTION VALUE="Italic">Italic
</SELECT><BR>
Select Font Size:
<SELECT NAME="theSize" onChange="setFont(this.form)">
    <OPTION VALUE="12">12
    <OPTION VALUE="18">18
    <OPTION SELECTED VALUE="24">24
    <OPTION VALUE="30">30
</SELECT><P>
<HR>
<INPUT TYPE="button" NAME="getInfo" VALUE="Applet Info..." onClick="getAppletInfo(this.form)">
<P>
<TEXTAREA NAME="details" ROWS=11 COLS=70></TEXTAREA>
</FORM>
<HR>
</BODY>
</HTML>



Basic Signed Script Structure

/*
JavaScript Bible, Fourth Edition
by Danny Goodman 
John Wiley & Sons CopyRight 2001
*/

<HTML>
<HEAD> 
<TITLE>Signed Scripts Testing</TITLE>
<SCRIPT LANGUAGE="JavaScript" ARCHIVE="myArchive.jar" SRC="access.js"></SCRIPT> 
<SCRIPT LANGUAGE="JavaScript" ID="1">
function fetchFile(form) {
   form.output.value = getFile()
}
function newRaisedWindow() { 
   // statements for this function
} 
</SCRIPT> 
</HEAD> 
<BODY>
A Source Code Example Only
<FORM>
<TEXTAREA NAME="output" COLS=60 ROWS=10 WRAP="virtual"></TEXTAREA><BR> 
<INPUT TYPE="button" VALUE="Read File" onClick="this.form.output.value = "";
fetchFile(this.form);" ID="2"><BR>
<TEXTAREA NAME="input" COLS=60 ROWS=10 WRAP="virtual"> </TEXTAREA><BR> 
<INPUT TYPE="button" VALUE="Save File" onClick="setFile(this.form.input.value);" ID="3"><P>
<INPUT TYPE="button" VALUE="New Window..." onClick="newRaisedWindow();" ID="4"> 
</FORM> 
</BODY> 
</HTML>



Calling Java Methods

<HTML>
<HEAD>
<TITLE>Calling Java Methods</TITLE>
</HEAD>
<BODY>
<P>This script writes the text, <EM>Hello World!</EM>
 to the Java console window.</P>
<SCRIPT LANGUAGE="JavaScript"><!--
java.lang.System.out.println("Hello World!")
// --></SCRIPT>
</BODY>
</HTML>



Stopping and Starting an Applet

<HTML>
<HEAD>
<TITLE>A Script That Could Stop a Clock</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function pauseClock() {
    document.clock1.stop()
}
function restartClock() {
    document.clock1.start()
}
</SCRIPT>
<BODY>
<H1>Simple control over an applet</H1>
<HR>
<APPLET CODE="ScriptableClock.class" NAME="clock1" WIDTH=500 HEIGHT=45>
<PARAM NAME=bgColor VALUE="Green">
<PARAM NAME=fgColor VALUE="Blue">
</APPLET>
<P>
<FORM NAME="widgets1">
<INPUT TYPE="button" VALUE="Pause Clock" onClick="pauseClock()">
<INPUT TYPE="button" VALUE="Restart Clock" onClick="restartClock()">
</FORM>
</BODY>
</HTML>



Using JavaScript in an Applet

/*
Mastering JavaScript, Premium Edition
by James Jaworski 
ISBN:078212819X
Publisher Sybex CopyRight 2001
*/
<title>Accessing JavaScript from an applet</TITLE>
<form NAME="textForm">
<P>Enter some text and then click Display Text:
 <INPUT TYPE="text" NAME="textField" SIZE="20"></P>
</FORM>
<APPLET CODE="ReadForm.class" WIDTH=400 HEIGHT=100
 NAME="readApp" MAYSCRIPT>
[The ReadForm Applet]
</APPLET>

//Reading a JavaScript Form (ReadForm.java)

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import netscape.javascript.JSObject;
import netscape.javascript.JSException;
public class ReadForm extends Applet {
 String text="Enter some text for me to display!";
 Font font = new Font("TimesRoman",Font.BOLD+Font.ITALIC,24);
 JSObject win, doc, form, textField;
 public void init() {
  win = JSObject.getWindow(this);
  doc = (JSObject) win.getMember("document");
  form = (JSObject) doc.getMember("textForm");
  textField = (JSObject) form.getMember("textField");
  setLayout(new BorderLayout());
  Panel buttons = new Panel();
  Button displayTextButton = new Button("Display Text");
  displayTextButton.addActionListener(new ButtonEventHandler());
  buttons.add(displayTextButton);
  add("South",buttons);
 }
 public void paint(Graphics g) {
  g.setFont(font);
  g.drawString(text,30,30);
 }
 class ButtonEventHandler implements ActionListener {
  public void actionPerformed(ActionEvent e){
   String s = e.getActionCommand();
   if("Display Text".equals(s)) {
    text= (String) textField.getMember("value");
    win.eval("alert(\"This alert comes from Java!\")");
    repaint();
   }
  }
 }
}