Canvas3D 未出现在 Swing 窗口中

发布于 2024-07-07 14:59:32 字数 494 浏览 5 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城旧梦 2024-07-14 14:59:32

可能您必须在面板上设置一个布局管理器,它会自动将子组件扩展到整个区域。 JPanel 默认情况下有一个 FlowLayout,它不会展开子组件。 您可以通过调用以下方式尝试使用 BorderLayout:

canvasPanel.setLayout(new 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:

canvasPanel.setLayout(new BorderLayout());
请持续率性 2024-07-14 14:59:32

Canvas3D 需要传递给它的尺寸; 从 SimpleUniverse 设置首选配置是不够的。 就我而言,这意味着这段代码:

        // 3D canvas initialization
        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();
        canvas.setSize(100, 100);
        canvasPanel.add(canvas);

Canvas3D needs a size passed to it; setting the preferred configuration from SimpleUniverse is not enough. In my case, that meant this code:

        // 3D canvas initialization
        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();
        canvas.setSize(100, 100);
        canvasPanel.add(canvas);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文