Canvas3D 未出现在 Swing 窗口中
我试图在 Swing JPanel 中插入 Canvas3D 对象,但代码似乎不起作用(即什么也没有发生):
Canvas3D canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration()); SimpleUniverse universe = new SimpleUniverse(canvas); BranchGroup root = new BranchGroup(); root.addChild(new ColorCube()); universe.addBranchGraph(root); universe.getViewingPlatform().setNominalViewingTransform(); canvasPanel.add(canvas);
我缺少什么? JPanel 是使用 NetBean 的可视化编辑器创建的。
I am attempting to insert a Canvas3D object inside a Swing JPanel, but the code doesn't seem to be working (i.e. nothing happens):
Canvas3D canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration()); SimpleUniverse universe = new SimpleUniverse(canvas); BranchGroup root = new BranchGroup(); root.addChild(new ColorCube()); universe.addBranchGraph(root); universe.getViewingPlatform().setNominalViewingTransform(); canvasPanel.add(canvas);
What am I missing? The JPanel was created using NetBean's Visual Editor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能您必须在面板上设置一个布局管理器,它会自动将子组件扩展到整个区域。 JPanel 默认情况下有一个 FlowLayout,它不会展开子组件。 您可以通过调用以下方式尝试使用 BorderLayout:
Probably you have to set a layout manager on the panel, which automatically expands the child components to the full area. A JPanel has a FlowLayout by default, which does not expand the child components. You could try a BorderLayout instead by calling:
Canvas3D 需要传递给它的尺寸; 从 SimpleUniverse 设置首选配置是不够的。 就我而言,这意味着这段代码:
Canvas3D needs a size passed to it; setting the preferred configuration from SimpleUniverse is not enough. In my case, that meant this code: