【问题标题】:How to show graph on JFrame如何在 JFrame 上显示图形
【发布时间】:2017-07-04 04:14:23
【问题描述】:

我有一个 JPanel 来创建一个无法编辑的图表。

当图形显示时,边名被JFrame的边界所覆盖。

如何在不被 JFrame 覆盖的情况下显示边缘名称?

My Example

这是我的代码:

package myapp;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import com.mxgraph.layout.mxIGraphLayout;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.model.mxICell;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

public class CreateGraph extends JFrame {

private static final long serialVersionUID = 8083868183987456695L;
mxICell a, b, c, d, e, f, g, h;

public CreateGraph() {
final mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();

graph.getModel().beginUpdate();
try {

    a = (mxICell) graph.insertVertex(parent, null, "a", 0, 0, 80, 30);
    b = (mxICell) graph.insertVertex(parent, null, "b", 0, 0, 80, 30);
    c = (mxICell) graph.insertVertex(parent, null, "c", 0, 0, 80, 30);
    d = (mxICell) graph.insertVertex(parent, null, "d", 0, 0, 80, 30);

    graph.insertEdge(parent, null, "ab", a, b);
    graph.insertEdge(parent, null, "bc", b, c);
    graph.insertEdge(parent, null, "cd", c, b);
    graph.insertEdge(parent, null, "cd", c, d);
    graph.insertEdge(parent, null, "da", d, a);

    graph.setCellsEditable(false);   
    graph.setCellsMovable(false); 
    graph.setCellsSelectable(false);

} finally {
    graph.getModel().endUpdate();
}

// define layout
mxIGraphLayout layout = new mxHierarchicalLayout(graph);
layout.execute(graph.getDefaultParent());

mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(graphComponent, BorderLayout.CENTER);

}

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

}

【问题讨论】:

  • (1) 正确格式化您的代码。 (2) 这个库中的类以小写字母开头,这已经是一个不好的迹象。 (3) 看起来mxGraphComponent 没有正确声明它的大小。

标签: java jframe jpanel jgraphx jgraph


【解决方案1】:

也许不想使用BorderLayout,因为您实际上没有像How to Use BorderLayoutBorder Layout not working 这样的用例。

您的边缘位于 X 位置 0,就在您的布局内。

一种选择是通过添加private final double X_OFFSET = 10 之类的常量,然后将其添加到每个graph 操作的X 坐标,来解决JGraph 本身的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 2023-02-08
    相关资源
    最近更新 更多