gpt4 book ai didi

java - JGraphX - 想要整个图表不可编辑

转载 作者:搜寻专家 更新时间:2023-10-31 08:17:06 25 4
gpt4 key购买 nike

我无法在手册或 SO 上找到答案。我想用 JGraphX 制作一个显示一些顶点和边缘的图形,但我不希望用户能够四处移动任何东西,也不希望那些绿色编辑框出现在漩涡或边缘上。它只是为了展示而已。

我尝试了对“Hello World”示例的这种修改,但没有成功。有什么建议吗?

package com.mxgraph.examples.swing;

import java.util.Hashtable;

import javax.swing.JFrame;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxStylesheet;

public class HelloWorld extends JFrame
{

/**
*
*/
private static final long serialVersionUID = -2707712944901661771L;

public HelloWorld()
{
super("Hello, World!");

mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();

//my addition of a stylesheet, I used it on the first node to see if it mad
//a difference, it didn't regarding dragability//////////////////////////////
mxStylesheet stylesheet = graph.getStylesheet();
Hashtable<String, Object> style = new Hashtable<String, Object>();
style.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_ELLIPSE);
style.put(mxConstants.STYLE_OPACITY, 50);
style.put(mxConstants.STYLE_FONTCOLOR, "#774400");
style.put(mxConstants.STYLE_EDITABLE, false);
stylesheet.putCellStyle("ROUNDED", style);

//tried this too///////////////////////////////////////////////
graph.setCellsEditable(false);

graph.getModel().beginUpdate();
try
{
Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80,
30, "ROUNDED");
Object v2 = graph.insertVertex(parent, null, "World!", 240, 150,
80, 30);
graph.insertEdge(parent, null, "Edge", v1, v2);
}
finally
{
graph.getModel().endUpdate();
}

mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
}

public static void main(String[] args)
{
HelloWorld frame = new HelloWorld();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 320);
frame.setVisible(true);
}

}

最佳答案

您可以禁用整个图形组件mxGraphComponent:

graphComponent.setEnabled(false);

关于java - JGraphX - 想要整个图表不可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19847637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com