如何使用 Netbeans 将 jpanel 置于 jpanel 的中心?

发布于 2024-09-02 02:47:26 字数 3000 浏览 5 评论 0原文

使用 Netbeans,我成功地将一个具有固定大小的 jpanel 置于另一个 jpanel 的中心。现在我不能重复它——只能复制它。

我做得怎么样? (或者我应该将 x 和 ya jpanel 与另一个 jpanel 中的固定大小居中)。

结果在代码中有所不同:

工作 - 搜索 .addContainerGap(并查看下一个不工作:

        javax.swing.GroupLayout center3Layout = new javax.swing.GroupLayout(center3);
        center3.setLayout(center3Layout);
        center3Layout.setHorizontalGroup(
            center3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1064, Short.MAX_VALUE)
            .addGroup(center3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(center3Layout.createSequentialGroup()
                    .addContainerGap(30, Short.MAX_VALUE)
                    .addComponent(mainPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 
javax.swing.GroupLayout.DEFAULT_SIZE, 
javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(30, Short.MAX_VALUE)))
        );
        center3Layout.setVerticalGroup(
            center3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 650, Short.MAX_VALUE)
            .addGroup(center3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(center3Layout.createSequentialGroup()
                    .addContainerGap(23, Short.MAX_VALUE)
                    .addComponent(mainPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(23, Short.MAX_VALUE)))
        );

不工作 - 搜索 .addGap 与上面的工作相比。

    javax.swing.GroupLayout center2Layout = new javax.swing.GroupLayout(center2);
    center2.setLayout(center2Layout);
    center2Layout.setHorizontalGroup(
        center2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 1073, Short.MAX_VALUE)
        .addGroup(center2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(center2Layout.createSequentialGroup()
                .addGap(0, 34, Short.MAX_VALUE)
                .addComponent(mainPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 35, Short.MAX_VALUE)))
    );
    center2Layout.setVerticalGroup(
        center2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 654, Short.MAX_VALUE)
        .addGroup(center2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(center2Layout.createSequentialGroup()
                .addGap(0, 25, Short.MAX_VALUE)
                .addComponent(mainPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 25, Short.MAX_VALUE)))
    );

我在属性等中并排查看 - 请帮忙!:)

With Netbeans I've succeded center a jpanel with fixed size, within an other jpanel. Now I can't repeat it - only copy it.

How did I do? (or should I do to center x and y a jpanel with fixed size in another jpanel).

The result differs in code:

Working - search for .addContainerGap( and see next not working:

        javax.swing.GroupLayout center3Layout = new javax.swing.GroupLayout(center3);
        center3.setLayout(center3Layout);
        center3Layout.setHorizontalGroup(
            center3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1064, Short.MAX_VALUE)
            .addGroup(center3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(center3Layout.createSequentialGroup()
                    .addContainerGap(30, Short.MAX_VALUE)
                    .addComponent(mainPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 
javax.swing.GroupLayout.DEFAULT_SIZE, 
javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(30, Short.MAX_VALUE)))
        );
        center3Layout.setVerticalGroup(
            center3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 650, Short.MAX_VALUE)
            .addGroup(center3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(center3Layout.createSequentialGroup()
                    .addContainerGap(23, Short.MAX_VALUE)
                    .addComponent(mainPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(23, Short.MAX_VALUE)))
        );

Not working - search for .addGap compared with above working.

    javax.swing.GroupLayout center2Layout = new javax.swing.GroupLayout(center2);
    center2.setLayout(center2Layout);
    center2Layout.setHorizontalGroup(
        center2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 1073, Short.MAX_VALUE)
        .addGroup(center2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(center2Layout.createSequentialGroup()
                .addGap(0, 34, Short.MAX_VALUE)
                .addComponent(mainPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 35, Short.MAX_VALUE)))
    );
    center2Layout.setVerticalGroup(
        center2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 654, Short.MAX_VALUE)
        .addGroup(center2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(center2Layout.createSequentialGroup()
                .addGap(0, 25, Short.MAX_VALUE)
                .addComponent(mainPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 25, Short.MAX_VALUE)))
    );

I've looked side by side in properties etc - please help! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

方圜几里 2024-09-09 02:47:26

我无法在 Netbeans 中重现将 JPanel 置于另一个 JPanel 中心的方法(使用 GroupLayout 等)。如果有人能找到它,我也很高兴知道。

但是,我可以建议您将外部面板的布局更改为 GridBagLayout(您也可以在 Netbeans 中使用上下文菜单中的“setLayout”执行此操作)。
默认的 GridBagConstraints 应该正是您想要的需要时,内面板将以其首选尺寸居中。因此,您实际上不需要自己亲自指定它们。

I can't reproduce as well a way to center a JPanel in another, in Netbeans (using the GroupLayout, so). If someone can find it, I would be glad to know as well.

However, I can advise you to change the layout of your outer panel to a GridBagLayout (you can do this in Netbeans as well with "setLayout" in the contextual menu).
The default GridBagConstraints should be exactly what you need, the inner panel will be centered, with its preferred size. So you shouldn't actually need to dive into specifying them yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文