本文介绍了Canvas3D没有出现在Swing窗口中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Swing JPanel中插入一个Canvas3D对象,但代码似乎没有工作(即没有任何反应):

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

我缺少什么? JPanel是使用NetBean的Visual Editor创建的。

What am I missing? The JPanel was created using NetBean's Visual Editor.

推荐答案

可能你必须在面板上设置一个布局管理器,它会自动扩展儿童组成部分到整个区域。默认情况下,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:

canvasPanel.setLayout(new BorderLayout());

这篇关于Canvas3D没有出现在Swing窗口中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 03:30