すべてのボタンの最大サイズを (おそらく優先サイズ値から) 計算し、基本的に同じサイズを (setSize を介して) 割り当てる独自のレイアウト マネージャーを作成できます。もちろん、位置情報も提供する必要がありますが、これにより、垂直方向と水平方向のレイアウト オプションを提供できるという利点があります。
アップデート
これは私が使用する実装です。Santhosh Kumar によって作成されたことがわかるように、私はそれを信用しません。すべてのボタンのサイズを同じサイズにするだけでなく、配置も提供するので、非常に便利です。ここで元の投稿を見つけることができますhttp://www.jroller.com/santhosh/entry/how_do_you_layout_command
/**
* @author Santhosh Kumar - santhosh@in.fiorano.com
*/
public class ButtonLayout implements LayoutManager, SwingConstants {
protected static Logger logger = Logger.getLogger(ButtonLayout.class);
private int gap;
private int alignment;
public ButtonLayout() {
setAlignment(RIGHT);
setGap(2);
}
public ButtonLayout(int alignment, int gap) {
setGap(gap);
setAlignment(alignment);
}
public ButtonLayout(int gap) {
this(RIGHT, gap);
}
public int getAlignment() {
return alignment;
}
public void setAlignment(int alignment) {
this.alignment = alignment;
}
public int getGap() {
return gap;
}
public void setGap(int gap) {
this.gap = gap;
}
private Dimension[] dimensions(Component children[]) {
int maxWidth = 0;
int maxHeight = 0;
int visibleCount = 0;
Dimension componentPreferredSize;
for (int i = 0, c = children.length; i < c; i++) {
if (children[i].isVisible()) {
componentPreferredSize = children[i].getPreferredSize();
maxWidth = Math.max(maxWidth, componentPreferredSize.width);
maxHeight = Math.max(maxHeight, componentPreferredSize.height);
visibleCount++;
}
}
int usedWidth = 0;
int usedHeight = 0;
switch (alignment) {
case LEFT:
case RIGHT:
usedWidth = maxWidth * visibleCount + gap * (visibleCount - 1);
usedHeight = maxHeight;
break;
case TOP:
case BOTTOM:
usedWidth = maxWidth;
usedHeight = maxHeight * visibleCount + gap * (visibleCount - 1);
break;
}
return new Dimension[]{
new Dimension(maxWidth, maxHeight),
new Dimension(usedWidth, usedHeight),};
}
public void layoutContainer(Container container) {
Insets insets = container.getInsets();
int width = container.getWidth() - (insets.left + insets.right);
int height = container.getHeight() - (insets.top + insets.bottom);
Component[] children = container.getComponents();
Dimension dim[] = dimensions(children);
int maxWidth = dim[0].width;
int maxHeight = dim[0].height;
int usedWidth = dim[1].width;
int usedHeight = dim[1].height;
for (int i = 0, c = children.length; i < c; i++) {
if (children[i].isVisible()) {
switch (alignment) {
case LEFT:
children[i].setBounds(
insets.left + (maxWidth + gap) * i,
insets.top,
maxWidth,
maxHeight);
break;
case TOP:
children[i].setBounds(
insets.left + ((width - maxWidth) / 2),
insets.top + (maxHeight + gap) * i,
maxWidth,
maxHeight);
break;
case RIGHT:
children[i].setBounds(
width - insets.right - usedWidth + (maxWidth + gap) * i,
insets.top,
maxWidth,
maxHeight);
break;
case BOTTOM:
children[i].setBounds(
insets.left + (maxWidth + gap) * i,
height - insets.bottom - usedHeight + (maxHeight + gap) * i,
// insets.top,
maxWidth,
maxHeight);
break;
}
}
}
}
public Dimension minimumLayoutSize(Container c) {
return preferredLayoutSize(c);
}
public Dimension preferredLayoutSize(Container container) {
Insets insets = container.getInsets();
Component[] children = container.getComponents();
Dimension dim[] = dimensions(children);
int usedWidth = dim[1].width;
int usedHeight = dim[1].height;
return new Dimension(
insets.left + usedWidth + insets.right,
insets.top + usedHeight + insets.bottom);
}
public void addLayoutComponent(String string, Component comp) {
}
public void removeLayoutComponent(Component c) {
}
}