【问题标题】:How did I center a jpanel in a jpanel with netbeans?我如何在带有 netbeans 的 jpanel 中将 jpanel 居中?
【发布时间】:2010-05-20 09:29:25
【问题描述】:

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

我是怎么做的? (或者我应该在另一个 jpanel 中以固定大小的 jpanel 居中 x 和 y)。

结果代码不同:

工作 - 搜索 .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)))
    );

我在属性等方面并排查看过 - 请帮助! :)

【问题讨论】:

  • 看看 Netbeans 为“工作”文件生成的“表单”文件可能会很有趣。这是 NB 创建的一个 xml 文件,包含您的布局结构。发现其中的实际差异可能更容易(但不能保证)。

标签: java swing layout jpanel netbeans6.8


【解决方案1】:

我无法在 Netbeans 中重现将 JPanel 居中的方法(因此,使用 GroupLayout)。如果有人能找到它,我也很高兴知道。

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

【讨论】:

    猜你喜欢
    • 2015-03-23
    • 1970-01-01
    • 2012-04-07
    • 2013-06-02
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    相关资源
    最近更新 更多