向NetBeans GUI构建器添加自定义组件!(WorldWind)

4

好的,我想把NASA的World Wind球体添加到由NetBeans GUI构建器创建的GUI窗口中。我的示例代码实例化了自己的窗口,GUI构建器希望我不要编辑必要的区域来滑动它 :) 我会写自己的代码,但这是NetBeans平台应用程序的一部分,包含我还没有准备处理的代码和注释。我不确定如何完成这个任务。这里是我想在窗口中使用的示例代码:

public class WorldWindTest {

public static void main(String[] args) {

    //create a WorldWind main object
    WorldWindowGLCanvas worldWindCanvas = new WorldWindowGLCanvas();
    worldWindCanvas.setModel(new BasicModel());
            Position myPoint = Position.fromDegrees(31.12, -88.64, 35000);


    //build Java swing interface
    JFrame frame = new JFrame("World Wind");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(worldWindCanvas);
    frame.setSize(800,600);
    frame.setVisible(true);

    //create some "Position" to build a polyline
    LinkedList<Position> list = new LinkedList<Position>();

//          list.add(Position.fromDegrees(i,0.0,i*20000));
    }

            list.add(Position.fromDegrees(30.12, -85.64, 35000));
            list.add(Position.fromDegrees(31.12, -88.64, 35000));


    //create "Polyline" with list of "Position" and set color / thickness
    Polyline polyline = new Polyline(list);
    polyline.setColor(Color.RED);
    polyline.setLineWidth(3.0);

    //create a layer and add Polyline
    RenderableLayer layer = new RenderableLayer();
    layer.addRenderable(polyline);
    //add layer to WorldWind
    worldWindCanvas.getModel().getLayers().add(layer);
}
}   

1
这就是为什么我不使用GUI构建器的原因...它们的“方便”通常会成为不便之处。 :) - mre
1
是的,我明白你的意思,但在NetBeans平台上,它有点强制性。 - The Ox
1
您仍然可以使用NetBeans代码生成添加一个使用BorderLayout的容器,然后使用自定义代码将WorldWindowGLCanvas添加到BorderLayout.CENTER位置的容器中。 - Hovercraft Full Of Eels
2个回答

4

针对我的评论,我认为你可以创建一个类,称之为SetUpWorldWindowGLCanvas,并在其中初始化和设置你的WorldWindowGLCanvas对象,然后提供一个公共的getter方法,允许你获取设置好的WorldWindowGLCanvas对象。即,

public class SetUpWorldWindowGLCanvas {

    WorldWindowGLCanvas worldWindCanvas = new WorldWindowGLCanvas();

    public SetUpWorldWindowGLCanvas() {
        worldWindCanvas.setModel(new BasicModel());
        Position myPoint = Position.fromDegrees(31.12, -88.64, 35000);

        // ... etc
    }

    public WorldWindowGLCanvas getWwGlCanvas() {
        return worldWindCanvas;
    }
}

然后将这个BorderLayout.CENTER放置在您的GUI构建器中创建并使用BorderLayout作为其布局管理器的JPanel中。


+1 封装。这也可以作为一个或多个代码生成属性插入,但描述起来可能会很笨拙。 - trashgod
这个会起作用的。现在遇到了.dll文件的问题,但这种方法是可靠的。谢谢! - The Ox

3

不要在整个应用程序中都使用GUI编辑器,只限制在那些最需要它的容器上,例如复杂的布局。然后,您可以将WorldWindowGLCanvas正常添加到您的顶层容器中。在这个示例中,WorldWindowGLCanvas将与NewJPanel一起出现:

JFrame f = new JFrame();
f.setLayout)new GridLayout(1, 0);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(worldWindCanvas);
f.add(new NewJPanel());
f.pack();
f.setVisible(true);

我认为你的答案与我的相关。既然我们想到了同样的事情,那就点个赞吧,毕竟楼主显然不会再回来了。 - Hovercraft Full Of Eels
哈哈,我回来啦!我起床了,得花些时间享受一下你的生日,不能一直写代码:) 我要试试这些,等会再来。 - The Ox

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接