简体   繁体   English

如何在JFrame上显示图形

[英]How to show graph on JFrame

I have a JPanel to create a graph which cannot be editable. 我有一个JPanel来创建一个不可编辑的图形。

When the graph shows, the edge name is covered by boundary of JFrame. 如图所示,边缘名称被JFrame的边界覆盖。

How can I show that edge name without being covered by JFrame? 如何显示边缘名称而不被JFrame覆盖?

My Example 我的例子

在此处输入图片说明 This is my code: 这是我的代码:

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);
}

}

Maybe don't want to use a BorderLayout , since you don't really have a use case like How to Use BorderLayout or Border Layout not working . 也许不想使用BorderLayout ,因为您并没有像How to Use BorderLayoutBorder Layout这样的用例。

Your edge is at X-position 0, which just inside your layout. 您的边缘位于布局内的X位置0。

One option is solving your problem withing JGraph itself, by adding a constant like private final double X_OFFSET = 10 and then add it to the X-coordinate of every graph operation. 一种选择是使用JGraph本身解决问题,方法是添加一个常量,如private final double X_OFFSET = 10 ,然后将其添加到每个graph操作的X坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM