gpt4 book ai didi

java - 将 JFrame 的预先存在的代码放入 JPanel 中

转载 作者:太空宇宙 更新时间:2023-11-04 13:13:03 24 4
gpt4 key购买 nike

我有来自 JUNG 库的现有 Java 代码,但我想添加自定义代码来创建用户界面。我使用 Netbeans IDE 上的设计选项卡创建了用户界面。但是当我添加自定义代码时, Swing 元素似乎无法按照设计的顺序排列。我想我的问题在于设置正确的 LAYOUT

这是在 Netbeans 中设计的布局

Image One

我想要的是将以下代码添加到JPanel问题是这段代码将 JFrame 作为参数,但就我而言,我需要将其放在 JPanel

这是我已有的代码及其在下图中的作用: Image Two

public class EditorMouse {

public static void main(String[] args) {

JFrame frame = new JFrame("Interface");
frame.setLayout(new FlowLayout());
SparseMultigraph<GraphElements.MyVertex, GraphElements.MyEdge> g
= new SparseMultigraph<GraphElements.MyVertex, GraphElements.MyEdge>();
// Layout<V, E>, VisualizationViewer<V,E>
// Map<GraphElements.MyVertex,Point2D> vertexLocations = new HashMap<GraphElements.MyVertex, Point2D>();
Layout<GraphElements.MyVertex, GraphElements.MyEdge> layout = new StaticLayout(g);
//layout.setSize(new Dimension(400, 350));
VisualizationViewer<GraphElements.MyVertex, GraphElements.MyEdge> vv
= new VisualizationViewer<GraphElements.MyVertex, GraphElements.MyEdge>(layout);
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
// Create a graph mouse and add it to the visualization viewer
EditingModalGraphMouse gm = new EditingModalGraphMouse(vv.getRenderContext(),
GraphElements.MyVertexFactory.getInstance(),
GraphElements.MyEdgeFactory.getInstance());
// Set some defaults for the Edges...
GraphElements.MyEdgeFactory.setDefaultCapacity(10.0);
GraphElements.MyEdgeFactory.setDefaultWeight(10.0);
// Trying out our new popup menu mouse plugin...
PopupVertexEdgeMenuMousePlugin myPlugin = new PopupVertexEdgeMenuMousePlugin();
// Add some popup menus for the edges and vertices to our mouse plugin.
JPopupMenu edgeMenu = new MyMouseMenus.EdgeMenu(frame);
JPopupMenu vertexMenu = new MyMouseMenus.VertexMenu();
myPlugin.setEdgePopup(edgeMenu);
myPlugin.setVertexPopup(vertexMenu);
gm.remove(gm.getPopupEditingPlugin()); // Removes the existing popup editing plugin
gm.add(myPlugin); // Add our new plugin to the mouse

vv.setGraphMouse(gm);
frame.getContentPane().add(vv);
gm.setMode(ModalGraphMouse.Mode.EDITING); // Start off in editing mode
frame.pack();
frame.setVisible(true);
}

最佳答案

由于 VisualizationViewer 扩展了 JPanel ,我会将初始化它的代码移动到它自己的类中,如下所示:

public class MyVisualizationViewer extends VisualizationViewer<GraphElements.MyVertex, GraphElements.MyEdge> {
public MyVisualizationViewer() {
super(new StaticLayout(new SparseMultigraph<GraphElements.MyVertex, GraphElements.MyEdge>()));
this.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
this.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
// Create a graph mouse and add it to the visualization viewer
EditingModalGraphMouse gm = new EditingModalGraphMouse(this.getRenderContext(),
GraphElements.MyVertexFactory.getInstance(),
GraphElements.MyEdgeFactory.getInstance());
// Set some defaults for the Edges...
GraphElements.MyEdgeFactory.setDefaultCapacity(10.0);
GraphElements.MyEdgeFactory.setDefaultWeight(10.0);
// // Trying out our new popup menu mouse plugin...
PopupVertexEdgeMenuMousePlugin myPlugin = new PopupVertexEdgeMenuMousePlugin();
// // Add some popup menus for the edges and vertices to our mouse plugin.
JPopupMenu edgeMenu = new MyMouseMenus.EdgeMenu(frame);
JPopupMenu vertexMenu = new MyMouseMenus.VertexMenu();
myPlugin.setEdgePopup(edgeMenu);
myPlugin.setVertexPopup(vertexMenu);
gm.remove(gm.getPopupEditingPlugin()); // Removes the existing popup editing plugin
gm.add(myPlugin); // Add our new plugin to the mouse
this.setGraphMouse(gm);
}
}

编译此类后,您可以将其从“项目”窗口拖到 JPanel 的 Netbeans 设计上。

关于java - 将 JFrame 的预先存在的代码放入 JPanel 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33784617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com