如何使 JToggleButton-size 在选择和取消选择时固定?

How to make JToggleButton-size fixed upon selection and deselection?

如何使 SelectedNot Selected 状态下的 JToggleButton 大小固定且相等?

如下图所示,我现在有一个可变长度的按钮:

未选中尺码:

已选尺寸

我尝试了 setSize()setPreferedSize() 方法,但没有任何改变。

当前按键方式:

private void connectionTglBtnActionPerformed(java.awt.event.ActionEvent evt) {                                                 

        if (connectionTglBtn.isSelected()) {
            connectionTglBtn.setText("S");
        } else {
            connectionTglBtn.setText("SSSS");
        }
}

更新:

这是我的布局管理器初始化方法:

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(readersComBox, javax.swing.GroupLayout.PREFERRED_SIZE, 338, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(refreshBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(connectionTglBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jButton1)
                .addGap(0, 0, Short.MAX_VALUE)))
        .addContainerGap())
);
layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
            .addComponent(readersComBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(refreshBtn)
            .addComponent(connectionTglBtn))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jLabel1)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(jButton1)
        .addContainerGap())
);

在上面的代码片段中,connectionTglBtn 是我们正在谈论的按钮。

一种方法是设置 JToggleButtonpreferredSize,但更重要的是如何使用 GroupLayout 将按钮添加到底层容器。 GroupLayout 可能关心也可能不关心 preferredSize 属性.

参考this,可以使用GroupLayout的规则,如下所述:

GroupLayout defines constants that provide precise control over resize behavior. They can be used as parameters in the addComponent(Component comp, int min, int pref, int max) method. Here are two examples:

  1. To force a component to be resizable (allow shrinking and growing):
    group.addComponent(component, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ...

This allows the component to resize between zero size (minimum) to any size (Short.MAX_VALUE as maximum size means "infinite"). If we wanted the component not to shrink below its default minimum size, we would use GroupLayout.DEFAULT_SIZE instead of 0 in the second parameter.

  1. To make a component fixed size (suppress resizing):
    group.addComponent(component, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
      GroupLayout.PREFERRED_SIZE) ...

In these examples the initial size of the component is not altered, its default size is the component's preferred size. If we wanted a specific size for the component, we would specify it in the second parameter instead of using GroupLayout.DEFAULT_SIZE.

所以在你的代码中你有:

.addComponent(connectionTglBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))

您应该根据 规则 2 更改它以强制您的 connectionTglBtn固定大小.

希望这会有所帮助。