现在的位置: 首页 > 综合 > 正文

(JSP)在文本域中显示超链接new——applet部分

2013年03月23日 ⁄ 综合 ⁄ 共 3913字 ⁄ 字号 评论关闭

        这次的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

抱歉!评论已关闭.