Java MXA布局未按预期工作

Java MXA布局未按预期工作,java,swing,jgraphx,Java,Swing,Jgraphx,我用它在Swing面板中绘制一个图形,一切都很好。 我尝试在目标单元格中插入两个单元格,但布局正在更改,并且所有单元格都不是水平的 所以我的例子是: 1) 使用一些单元格构建图形: 2) 按top botton并在Hello中添加两个单元格: 正如你们所看到的,问题是布局不再是水平的,即使我再次调用了MXCallayout 谢谢你的回答 package com.mxgraph.examples.swing; import java.awt.BorderLayout; import

我用它在Swing面板中绘制一个图形,一切都很好。 我尝试在目标单元格中插入两个单元格,但布局正在更改,并且所有单元格都不是水平的

所以我的例子是: 1) 使用一些单元格构建图形:

2) 按top botton并在Hello中添加两个单元格:

正如你们所看到的,问题是布局不再是水平的,即使我再次调用了MXCallayout

谢谢你的回答

    package com.mxgraph.examples.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;

import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.model.mxICell;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

public class HelloWorld extends JFrame {

    private mxGraphComponent graphComponent;

    private int idgenerator = 0;

    public HelloWorld() {
    super("Hello, World!");
    mxGraph graph = new mxGraph();
    Object parent = graph.getDefaultParent();

    graph.getModel().beginUpdate();
    try {
        Object v1 = graph.insertVertex(parent,
            String.valueOf(++idgenerator), "Hello", 20, 20, 80, 30);
        Object v2 = graph.insertVertex(parent,
            String.valueOf(++idgenerator), "World!", 240, 150, 80, 30);
        graph.insertEdge(parent, String.valueOf(++idgenerator), "Edge", v1,
            v2);
        Object v3 = graph.insertVertex(parent,
            String.valueOf(++idgenerator), "Brando!", 240, 150, 80, 30);
        graph.insertEdge(parent, String.valueOf(++idgenerator), "Edge", v2,
            v3);
        Object v4 = graph.insertVertex(parent,
            String.valueOf(++idgenerator), "Jotaro!", 240, 150, 80, 30);
        graph.insertEdge(parent, String.valueOf(++idgenerator), "Edge", v3,
            v4);

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

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

    graphComponent.getGraphControl().addMouseListener(new MouseAdapter() {

        @Override
        public void mouseReleased(MouseEvent e) {
        Object cell = graphComponent.getCellAt(e.getX(), e.getY());

        if (cell != null) {
            System.out.println("cell=" + graph.getLabel(cell));
            JOptionPane.showMessageDialog(graphComponent, "cell="
                + graph.getLabel(cell));
        }
        }
    });

    layoutGraph(graph);

    JButton change = new JButton("change");
    change.setPreferredSize(new Dimension(100, 50));
    change.setAction(new AbstractAction() {

        private static final long serialVersionUID = -1085992528036117542L;

        @Override
        public void actionPerformed(ActionEvent e) {
        mxGraph graph = graphComponent.getGraph();
        mxGraphModel model = (mxGraphModel) graph.getModel();
        mxCell before = (mxCell) model.getCells().get("2");
        graph = insertIn(graph, before);

        layoutGraph(graph);
        }

        public mxGraph insertIn(mxGraph graph, mxCell target) {
        Object parent = graph.getDefaultParent();

        graph.getModel().beginUpdate();
        try {
            mxGraphModel model = (mxGraphModel) graph.getModel();
            mxCell ed = (mxCell) target.getEdgeAt(0);

            mxICell t = ed.getTarget();

            model.remove(ed);

            Object v1 = graph.insertVertex(parent,
                String.valueOf(++idgenerator), "HERE!", 20, 20, 80,
                30);

            Object v2 = graph.insertVertex(parent,
                String.valueOf(++idgenerator), "HERE2", 20, 20, 80,
                30);

            target.insert((mxICell) v1);
            target.insert((mxICell) v2);
            target.setCollapsed(false);

            graph.insertEdge(parent, String.valueOf(++idgenerator), "",
                v1, v2);

            graph.insertEdge(parent, String.valueOf(++idgenerator), "",
                v2, t);

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

    getContentPane().add(change, BorderLayout.PAGE_START);

    }

    public void layoutGraph(mxGraph graph) {
    mxHierarchicalLayout layout = new mxHierarchicalLayout(graph);
    layout.setOrientation(SwingConstants.WEST);
    layout.execute(graph.getDefaultParent());
    }

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

}

当您在构造函数中插入已存在的内部单元格时,也会出现此问题,因此这是MXHierarchyCallayout的问题,而不是更新/刷新图形的问题…@EricLeibenguth您是否建议在库中打开一个问题?可能。。。也许有一种方法可以通过扩展来控制
mxHierarchyCallayout
的行为,但我对该库不太了解。这似乎是一个众所周知的问题:这里仍在寻找解决方案……一个快速解决方法是使用mxHierarchyCallayout,但也将坐标放在单元格的几何体中。这不是正确的做法,但可以挽救你的一天