JGraphX 正在破坏 Swing 组件的界限
JGraphX is ruining the bounds of Swing-components
我正在尝试实现一些自写的 GUI 元素(如 Java-swing 按钮)和 JGraphX 之间的交互。为了做到这一点,我首先只想在 JGraph 元素旁边显示一个按钮 - 我被卡住了。
我显示按钮本身的代码工作正常:
import javax.swing.*;
public class HelloWorld extends JFrame {
public HelloWorld()
{
super("Everything works");
}
public static void main(String[] args)
{
hello_world frame = new hello_world();
frame.setTitle("bub");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
button.setBounds(10, 10, 100, 50);
frame.setVisible(true);
frame.add(button);
}
}
但是,一旦我添加了 JGraph 组件,按钮就会全屏显示。我不知道为什么以及如何防止这种情况发生。我的代码:
import javax.swing.*;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;
public class HelloWorld extends JFrame {
private mxGraph graph;
private Object window;
public HelloWorld()
{
super("Nothing works");
graph = new mxGraph();
window = graph.getDefaultParent();
graph.getModel().beginUpdate();
try
{
Object v2 = graph.insertVertex(window, null, "Hello World!", 200, 150, 80, 30);
}
finally
{
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
}
public static void main(String[] args)
{
hello_world frame = new hello_world();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
JButton button = new JButton("left");
button.setBounds(10, 10, 100, 50);
frame.setVisible(true);
frame.add(button);
}
}
结果是这样的(我手动调整了 window 的大小以显示按钮。自然地,按钮会填满 space 后面的 JGraph-Component 因此将是不可见的):
Swing 使用布局管理器。 JFrame 的默认布局管理器是 BorderLayout
。当您将组件添加到框架时,使用的默认约束是 BorderLayout.CENTER
。 BorderLayout
.
的CENTER
只能添加一个组件
setBounds(...) 只会暂时起作用。一旦调整了框架的大小,布局管理器就会被调用,按钮将根据布局管理器的规则被赋予新的 size/location。
解决方法是正确使用布局管理器。我不确定您要实现的布局是什么,所以我只能建议您阅读 Layout Managers 上的 Swing 教程以获取工作示例以帮助您入门。然后您可以嵌套具有不同布局管理器的面板以达到您想要的效果。
从简单的开始,例如:
frame.add(button, BorderLayout.PAGE_END);
frame.setVisible(true);
//frame.add(button);
以上将在框架的中央显示您的图表,在底部显示按钮。
我正在尝试实现一些自写的 GUI 元素(如 Java-swing 按钮)和 JGraphX 之间的交互。为了做到这一点,我首先只想在 JGraph 元素旁边显示一个按钮 - 我被卡住了。
我显示按钮本身的代码工作正常:
import javax.swing.*;
public class HelloWorld extends JFrame {
public HelloWorld()
{
super("Everything works");
}
public static void main(String[] args)
{
hello_world frame = new hello_world();
frame.setTitle("bub");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
button.setBounds(10, 10, 100, 50);
frame.setVisible(true);
frame.add(button);
}
}
但是,一旦我添加了 JGraph 组件,按钮就会全屏显示。我不知道为什么以及如何防止这种情况发生。我的代码:
import javax.swing.*;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;
public class HelloWorld extends JFrame {
private mxGraph graph;
private Object window;
public HelloWorld()
{
super("Nothing works");
graph = new mxGraph();
window = graph.getDefaultParent();
graph.getModel().beginUpdate();
try
{
Object v2 = graph.insertVertex(window, null, "Hello World!", 200, 150, 80, 30);
}
finally
{
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
}
public static void main(String[] args)
{
hello_world frame = new hello_world();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
JButton button = new JButton("left");
button.setBounds(10, 10, 100, 50);
frame.setVisible(true);
frame.add(button);
}
}
结果是这样的(我手动调整了 window 的大小以显示按钮。自然地,按钮会填满 space 后面的 JGraph-Component 因此将是不可见的):
Swing 使用布局管理器。 JFrame 的默认布局管理器是 BorderLayout
。当您将组件添加到框架时,使用的默认约束是 BorderLayout.CENTER
。 BorderLayout
.
CENTER
只能添加一个组件
setBounds(...) 只会暂时起作用。一旦调整了框架的大小,布局管理器就会被调用,按钮将根据布局管理器的规则被赋予新的 size/location。
解决方法是正确使用布局管理器。我不确定您要实现的布局是什么,所以我只能建议您阅读 Layout Managers 上的 Swing 教程以获取工作示例以帮助您入门。然后您可以嵌套具有不同布局管理器的面板以达到您想要的效果。
从简单的开始,例如:
frame.add(button, BorderLayout.PAGE_END);
frame.setVisible(true);
//frame.add(button);
以上将在框架的中央显示您的图表,在底部显示按钮。