Java 如何在JFrame上显示图形
我有一个JPanel来创建一个无法编辑的图形 当图形显示时,边名称由JFrame的边界覆盖 如何显示边缘名称而不被JFrame覆盖 这是我的代码:Java 如何在JFrame上显示图形,java,jframe,jpanel,jgraphx,jgraph,Java,Jframe,Jpanel,Jgraphx,Jgraph,我有一个JPanel来创建一个无法编辑的图形 当图形显示时,边名称由JFrame的边界覆盖 如何显示边缘名称而不被JFrame覆盖 这是我的代码: package myapp; import java.awt.BorderLayout; import javax.swing.JFrame; import com.mxgraph.layout.mxIGraphLayout; import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
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);
}
}
可能不想使用
BorderLayout
,因为您没有像or这样的用例
您的边位于X位置0,正好位于布局内部
一个选项是解决JGraph
本身的问题,方法是添加一个常量,如private final double X_OFFSET=10
,然后将其添加到每个图形的X坐标中。(1)正确格式化代码。(2) 这个库中的类以小写开头,这已经是一个坏兆头了。(3) 看起来,mxGraphComponent
没有正确声明其大小。