package editorgraph;import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.util.Hashtable;
import java.util.Map;
import java.applet.*;import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.event.CellEditorListener;import com.jgraph.JGraph;
import com.jgraph.graph.CellView;
import com.jgraph.graph.GraphCellEditor;
import com.jgraph.graph.GraphConstants;
import com.jgraph.graph.GraphModel;
import com.jgraph.plaf.basic.BasicGraphUI;/** 
* An example that demonstrates how to use a JDialog 
* as a CellEditor in JGraph. 

* @version 1.1 23/12/02 
* @author Gaudenz Alder 
*/
public class MyApplet extends Applet {
  public void init() {
    JFrame frame = new JFrame("EditorGraph");
frame.getContentPane().add(new EditorGraph());
frame.pack();
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
  }


class EditorGraph extends JGraph { /** 
* Constructs a EditorGraph with a sample model. 
*/
public EditorGraph() {
} /** 
* Constructs a EditorGraph for <code>model</code>. 
*/
public EditorGraph(GraphModel model) {
super(model);
} /** 
* Override parent method with custom GraphUI. 
*/
public void updateUI() {
setUI(new EditorGraphUI());
invalidate();
}

/*public static void main(String[] args) {
JFrame frame = new JFrame("EditorGraph");
frame.getContentPane().add(new EditorGraph());
frame.pack();
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}*/ /** 
* Definition of the custom GraphUI. 
*/
public class EditorGraphUI extends BasicGraphUI { protected CellEditorListener cellEditorListener; protected JFrame editDialog = null; /** 
* Create the dialog using the cell's editing component. 
*/
protected void createEditDialog(Object cell, MouseEvent event) {
Dimension editorSize = editingComponent.getPreferredSize();
editDialog = new JFrame("Edit " + graph.convertValueToString(cell));
editDialog.setSize(editorSize.width, editorSize.height);
editDialog.getContentPane().add(editingComponent);
editingComponent.validate();
editDialog.pack();
editDialog.show();
} /** 
* Stops the editing session. If messageStop is true the editor 
* is messaged with stopEditing, if messageCancel is true the 
* editor is messaged with cancelEditing. If messageGraph is true 
* the graphModel is messaged with valueForCellChanged. 
*/
protected void completeEditing(
boolean messageStop,
boolean messageCancel,
boolean messageGraph) {
if (stopEditingInCompleteEditing
&& editingComponent != null
&& editDialog != null) {
Component oldComponent = editingComponent;
Object oldCell = editingCell;
GraphCellEditor oldEditor = cellEditor;
Object newValue = oldEditor.getCellEditorValue();
Rectangle editingBounds = graph.getCellBounds(editingCell);
boolean requestFocus =
(graph != null
&& (graph.hasFocus() || editingComponent.hasFocus()));
editingCell = null;
editingComponent = null;
if (messageStop)
oldEditor.stopCellEditing();
else if (messageCancel)
oldEditor.cancelCellEditing();
editDialog.dispose();
if (requestFocus)
graph.requestFocus();
if (messageGraph) {
Map map = GraphConstants.createMap();
GraphConstants.setValue(map, newValue);
Map nested = new Hashtable();
nested.put(oldCell, map);
graphLayoutCache.edit(nested, null, null, null);
}
updateSize();
// Remove Editor Listener 
if (oldEditor != null && cellEditorListener != null)
oldEditor.removeCellEditorListener(cellEditorListener);
cellEditor = null;
editDialog = null;
}
} /** 
* Will start editing for cell if there is a cellEditor and 
* shouldSelectCell returns true.<p> 
* This assumes that cell is valid and visible. 
*/
protected boolean startEditing(Object cell, MouseEvent event) {
completeEditing();
if (graph.isCellEditable(cell) && editDialog == null) { // Create Editing Component **** ***** 
CellView tmp = graphLayoutCache.getMapping(cell, false);
cellEditor = tmp.getEditor();
editingComponent =
cellEditor.getGraphCellEditorComponent(
graph,
cell,
graph.isCellSelected(cell));
if (cellEditor.isCellEditable(event)) {
editingCell = cell; // Create Wrapper Dialog **** ***** 
createEditDialog(cell, event); // Add Editor Listener 
if (cellEditorListener == null)
cellEditorListener = createCellEditorListener();
if (cellEditor != null && cellEditorListener != null)
cellEditor.addCellEditorListener(cellEditorListener); if (cellEditor.shouldSelectCell(event)) {
stopEditingInCompleteEditing = false;
try {
graph.setSelectionCell(cell);
} catch (Exception e) {
System.err.println("Editing exception: " + e);
}
stopEditingInCompleteEditing = true;
} if (event instanceof MouseEvent) {
/* Find the component that will get forwarded all the 
mouse events until mouseReleased. */
Point componentPoint =
SwingUtilities.convertPoint(
graph,
new Point(event.getX(), event.getY()),
editingComponent); /* Create an instance of BasicTreeMouseListener to handle 
passing the mouse/motion events to the necessary 
component. */
// We really want similiar behavior to getMouseEventTarget, 
// but it is package private. 
Component activeComponent =
SwingUtilities.getDeepestComponentAt(
editingComponent,
componentPoint.x,
componentPoint.y);
if (activeComponent != null) {
new MouseInputHandler(
graph,
activeComponent,
event);
}
}
return true;
} else
editingComponent = null;
}
return false;
} }}

解决方案 »

  1.   

    在写applet参数的时候需要把你用到的包一起包含进去,或者直接打包成jar,如:
    <applet
    codebase="."
    code="xxx.class,x.jar,y.har"
    >
      

  2.   

    对x.jar的路径有要求吗??是不是只要在classpath定义过就可以了呢
      

  3.   

    把你自己写的class文件都打到一个jar包中,如:
    jar cvf MyClass.jar XXXX(表示放你的class文件的目录名)一个index.htm文件,如:
    <HTML>
    <HEAD>
    <TITLE> A Simple Program </TITLE>
    </HEAD>
    <BODY>
    <APPLET CODE="XXXX.MyClass.class(你的主要执行的类)" archive="MyClass.jar" WIDTH=100% HEIGHT=100% ALT="If you could run this applet, you'd see some animation">
    </APPLET>
    </BODY>
    </HTML>
    好,在ie中执行index.htm就可以了!