Canvas3D没有出现在Swing窗口中

时间:2008-10-17 03:22:05

标签: java swing java-3d

我试图在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的Visual Editor创建的。

2 个答案:

答案 0 :(得分:3)

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

来尝试BorderLayout
canvasPanel.setLayout(new BorderLayout());

答案 1 :(得分:0)

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);