Java 更改 JBorderLayout 内 JTextField 的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4308401/
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-08-14 15:43:22  来源:igfitidea点击:

Change the size of a JTextField inside a JBorderLayout

javaresizesizejframejtextfield

提问by Jon

When I use the code below it doesn't alter the size at all, it still fills the area in the grid.

当我使用下面的代码时,它根本不会改变大小,它仍然填充网格中的区域。

JPanel displayPanel = new JPanel(new GridLayout(4, 2));

JTextField titleText = new JTextField("title");

displayPanel.add(titleText);

titleText.setSize(200, 24);

采纳答案by Joseph Gordon

From the api on GridLayout:

从 GridLayout 上的 api:

The container is divided into equal-sized rectangles, and one component is placed in each rectangle.

容器被分成大小相等的矩形,每个矩形中放置一个组件。

Try using FlowLayout or GridBagLayout for your set size to be meaningful. Also, @Serplat is correct. You need to use setPreferredSize( Dimension )instead of setSize( int, int ).

尝试使用 FlowLayout 或 GridBagLayout 使您的设置大小有意义。另外,@Serplat 是正确的。您需要使用setPreferredSize( Dimension )而不是setSize( int, int )

    JPanel displayPanel = new JPanel();
    // JPanel displayPanel = new JPanel( new GridLayout( 4, 2 ) );
    // JPanel displayPanel = new JPanel( new BorderLayout() );
    // JPanel displayPanel = new JPanel( new GridBagLayout() );

    JTextField titleText = new JTextField( "title" );

    titleText.setPreferredSize( new Dimension( 200, 24 ) );

    // For FlowLayout and GridLayout, uncomment:
    displayPanel.add( titleText );

    // For BorderLayout, uncomment:
    // displayPanel.add( titleText, BorderLayout.NORTH );

    // For GridBagLayout, uncomment:
    // displayPanel.add( titleText, new GridBagConstraints( 0, 0, 1, 1, 1.0,
    // 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
    // new Insets( 0, 0, 0, 0 ), 0, 0 ) );

回答by AlexR

Try to play with

试着玩

setMinSize()
setMaxSize()
setPreferredSize()

These method are used by layout when it decide what should be the size of current element. The layout manager calls setSize() and actually overrides your values.

布局在决定当前元素的大小时使用这些方法。布局管理器调用 setSize() 并实际覆盖您的值。

回答by Serplat

With a BorderLayout you need to use setPreferredSizeinstead of setSize

使用 BorderLayout 您需要使用setPreferredSize而不是setSize

回答by camickr

Any component added to the GridLayout will be resized to the same size as the largest component added. If you want a component to remain at its preferred size, then wrap that component in a JPanel and then the panel will be resized:

添加到 GridLayout 的任何组件都将调整为与添加的最大组件相同的大小。如果您希望组件保持其首选大小,则将该组件包装在 JPanel 中,然后面板将调整大小:

JPanel displayPanel = new JPanel(new GridLayout(4, 2)); 
JTextField titleText = new JTextField("title"); 
JPanel wrapper = new JPanel( new FlowLayout(0, 0, FlowLayout.LEADING) );
wrapper.add( titleText );
displayPanel.add(wrapper); 
//displayPanel.add(titleText);