这次的applet部分做了很大的修改:
package applet;
import javax.swing.JApplet;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.HyperlinkEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.URL;


/** *//**
* This class is used to define response of communication
*
* @author Zenny Chen
*
*/

class CommunicationProtocol ...{
static final String OK_RESPONSE = "OK";
static final String NG_RESPONSE = "NG";
}


/** *//**
* This applet is used for a text area that can make some particular
* strings displayed as hyperlinks. So some components in Java Swing
* are used.<br>
* NB:Java swing is really powerful!
*
* @author Zenny Chen
*
*/

public class TestApplet extends JApplet implements HyperlinkListener, Runnable, MouseListener ...{

/** *//**
* Without this, there will be a warning.
*/
private static final long serialVersionUID = -5281871014922105280L;
private static final String PARAM_NAME = "func";

/** *//**
* client socket
*/
private Socket sock = null;

/** *//**
* the applet running thread
*/
private Thread thread = null;

/** *//**
* Please see j2se docs
*/
private HTMLEditorKit edit = null;

/** *//**
* Please see j2se docs
*/
private HTMLDocument doc = null;

/** *//**
* Please see j2se docs
*/
private JTextPane textpane = null;

/** *//**
* Please see j2se docs
*/
private JScrollPane scroll = null;

/** *//**
* the URL which is specified by the user when a hyperlink is
* clicked.
*/
private volatile String socketURL = null;

/** *//**
* Flag that marks whether connecting to the server has failed.
*/
private volatile boolean isFailed = false;

/** *//**
* the port number of the server(hard coded here)
*/
private final int port = 8079;

/** *//**
* the url of the server<br>
* This will be passed to the applet through JavaScript.
*/
private String urlname = "";

/** *//**
* store the parameter that is defined in HTML <object> tag
*/
private String param = "";

/** *//**
* Initialize the applet.
*/
public void init() ...{
param = this.getParameter(PARAM_NAME);
edit = new HTMLEditorKit();
doc = new HTMLDocument();
textpane = new JTextPane(doc);
textpane.setEditorKit(edit);
scroll = new JScrollPane(textpane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
getContentPane().add(scroll);
textpane.setEditable(false);
textpane.addHyperlinkListener(this);
scroll.addMouseListener(this);
System.out.println("applet initialized");
}

/** *//**
* Start the applet and make it running.
*/
public void start() ...{
System.out.println("applet start");

if(thread == null) ...{
thread = new Thread(this);
thread.start();
}
}

/** *//**
* Stop the applet.
*/
public void stop() ...{

try ...{
if(sock != null) ...{
sock.close();
sock = null;
}
}
catch(IOException e) ...{
System.out.println("socket stop exception");
}

if(thread != null && thread.isAlive()) ...{
Thread.yield();
thread = null;
}
System.out.println("applet stop");
}

/** *//**
* the applet's running method
*
*/
public void run() ...{
String s = "";

while(socketURL == null) ...{
try ...{
Thread.yield();
Thread.sleep(800);
}
catch(InterruptedException e) ...{
e.printStackTrace();
}
}
filterIPAddress();
System.out.println(socketURL);

do ...{
try ...{
if(isFailed) ...{
Thread.yield();
Thread.sleep(200);
}
isFailed = false;
sock = new Socket(socketURL, port);
DataOutputStream out = new DataOutputStream(sock.getOutputStream());
DataInputStream in = new DataInputStream(sock.getInputStream());
out.writeUTF(param);
String cmd = in.readUTF();

if(cmd == null || cmd.equals(CommunicationProtocol.NG_RESPONSE)) ...{
System.out.println("mismatched!");
isFailed = true;
continue;
}
System.out.println("socket created");

do ...{
s = in.readUTF();
Thread.sleep(200);
}
while(s == null);
System.out.println(s);
}
catch(IOException e) ...{
System.out.println("Connecting...");
isFailed = true;
}
catch(InterruptedException ee) ...{
System.out.println("Sleep interrupted");
isFailed = true;
}
}
while(isFailed);
textpane.setText(s);
System.out.println("applet run over");

try ...{
Thread.sleep(800);
}
catch(InterruptedException e) ...{
}
}

/** *//**
* Catch the hyperlink event.
*/
public void hyperlinkUpdate(HyperlinkEvent e) ...{

if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) ...{
URL url = e.getURL();
if(url == null)
urlname = e.getDescription();
else
getAppletContext().showDocument(url, "_blank");
}
}

/** *//**
* for communicating with JavaScript<br>
* Transmit the URL of a specified hyperlink to JavaScript.
* @return urlname
*/
public String getURLName() ...{
return urlname;
}

/** *//**
* for communicating with JavaScript</br>
* Clear the last URL of a specified hyperlink for the preparation
* for the next one.
*
*/
public void clearURLName() ...{
urlname = "";
}

/** *//** *//** *//**
* for communicating with JavaScript<br>
* Get the server's URL thru JavaScript
* @param href reference to the URL string
*/ 
public void setSocketURL(String href) ...{
socketURL = href;
}

/** *//**
* invoked by JavaScript<br>
* JavaScript invoke this method to judge if the applet failed
* at connecting with the specified server. If the connection fails,
* JavaScript will invoke the applet's start() method to make it
* restart.
* @return isFailed flag
* @deprecated
*/
public boolean GetIsFailed() ...{
return isFailed;
}

/** *//**
* Filter the IP address of the server.<br>
* For example: The server URL is: "http://172.31.25.170:8080.../".
* Just extract "172.31.25.170" from the URL above.
*
*/
private void filterIPAddress() ...{
//if(!socketURL.contains("http"))
// return;
char[] ch = socketURL.toCharArray();
int i = "http://".length();
int k = 0;

for(k=i; k<ch.length; k++) ...{
if(ch[k] == ':')
break;
}
socketURL = String.copyValueOf(ch, i, k-i);
}

public void mouseClicked(MouseEvent e) ...{
System.out.println("applet activated!");
repaint();
}

public void mouseEntered(MouseEvent e) ...{
System.out.println("applet activated!");
repaint();
}

public void mouseExited(MouseEvent e) ...{
}

public void mousePressed(MouseEvent e) ...{
}

public void mouseReleased(MouseEvent e) ...{
}
}
Java Applet 实现文本区域超链接
本文介绍了一个使用 Java Swing 构件实现带有超链接功能的文本区域的 Applet 示例。该 Applet 可通过 JavaScript 传递参数,并与服务器进行通信获取数据。
558

被折叠的 条评论
为什么被折叠?



