1

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

我是怎么做的?(或者我应该在另一个 jpanel 中以固定大小将 x 和 ya 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)))
    );

我已经并排查看了属性等 - 请帮助!:)

4

1 回答 1

2

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

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

于 2010-05-20T15:58:21.583 回答