java 为什么 setPreferredSize 不改变按钮的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2546079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:43:10  来源:igfitidea点击:

Why setPreferredSize does not change the size of the button?

javauser-interfaceswingsizejbutton

提问by Roman

Here is the code:

这是代码:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class TestGrid {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Colored Trails");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 9));
        panel.setMaximumSize(new Dimension(9*30-20,4*30));

        JButton btn;
        for (int i=1; i<=4; i++) {
            for (int j=1; j<=4; j++) {
                btn = new JButton();
                btn.setPreferredSize(new Dimension(30, 30));
                panel.add(btn);
            }

            btn = new JButton();
            btn.setPreferredSize(new Dimension(30, 10));
            panel.add(btn);

            for (int j=1; j<=4; j++) {
                btn = new JButton();
                btn.setPreferredSize(new Dimension(30, 30));
                panel.add(btn);
            }

        }
        mainPanel.add(panel);
        frame.add(mainPanel);

        frame.setSize(450,950);
        frame.setVisible(true);
    }
}

I suppose to have a table of buttons with 4 rows and 9 columns. And the middle column should be narrower that other columns. I tried Dimension(30, 10)and Dimension(30, 10)both have no effect on the width of the middle column. Why?

我想有一个 4 行 9 列的按钮表。中间列应该比其他列更窄。我试过了Dimension(30, 10)Dimension(30, 10)两者都对中间列的宽度没有影响。为什么?

回答by Michael Borgwardt

Layout managers are free to ignore the preferred size. Specifically, GridLayoutwill always make each cell in the grid exactly the same size (it's a pretty useless layout manager for that reason).

布局管理器可以随意忽略首选大小。具体来说,GridLayout将始终使网格中的每个单元格大小完全相同(因此它是一个非常无用的布局管理器)。

You'll have to use a different layout manager, such as nested BoxLayoutor a GroupLayout.

您必须使用不同的布局管理器,例如嵌套BoxLayoutGroupLayout.

回答by Kris

GridLayout is quite inflexible in that each and every cell is the same size, typically honoring the largest height and width settings of any object added to the grid.

GridLayout 非常不灵活,因为每个单元格的大小都相同,通常遵循添加到网格的任何对象的最大高度和宽度设置。

If the rows and/or columns need to have varying sizes you should use GridBagLayout.

如果行和/或列需要有不同的大小,您应该使用 GridBagLayout。

回答by Apurva Mayank

setPreferredSizewill not change the size of the button until dimension is set by using Dimension.

setPreferredSize在使用 Dimension 设置尺寸之前,不会更改按钮的大小。

Example:-

例子:-

Dimension dim = new Dimension(20,20), then use setPerferredSize(dim).

Dimension dim = new Dimension(20,20), then use setPerferredSize(dim).