2017-07-04 165 views
0

我有一個JPanel來創建一個無法編輯的圖形。如何在JFrame上顯示圖形

當圖形顯示時,邊緣名稱被JFrame的邊界覆蓋。

如何顯示邊緣名稱而不被JFrame覆蓋?

My Example

enter image description here 這是我的代碼:

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

} 
+0

(1)正確設置代碼的格式。 (2)此庫中的類以小寫字母開頭,這已經是一個不好的跡象。 (3)它看起來像'mxGraphComponent'沒有正確地聲明它的大小。 – user1803551

回答

0

也許不想使用BorderLayout,因爲你真的沒有一個用例像How to Use BorderLayoutBorder Layout not working

你的邊緣位於X位置0,它位於佈局內部。

一個選項是通過添加一個像private final double X_OFFSET = 10這樣的常數,然後將其添加到每個操作的X座標中來解決您自己的問題JGraph本身。