java gridLayout 中的拉伸按钮

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

stretched button in the gridLayout

javaswingjbuttonlayout-managergridbaglayout

提问by rookie

everyone, I have some problem with Java I created panel with five rows and one column, also I added button to the one of the rows, finally I added this panel to the frame, and I received button which is stretched to full frame, can I reduce the size of the button, I used

大家,我对Java有一些问题我创建了五行一列的面板,我还在其中一行添加了按钮,最后我将此面板添加到框架中,我收到了拉伸到全框架的按钮,可以我减小了按钮的大小,我使用了

myButton.setSize(10,10);

but it doesn't seem to work, also I use

但它似乎不起作用,我也使用

frame.pack();

may it be the problem, thanks in advance

可能是问题所在,提前致谢

回答by aioobe

You should refrain from setting the size yourself. You should select a proper LayoutManagerthat does the job for you.

您应该避免自己设置尺寸。你应该选择一个适合LayoutManager你的工作。

(Unless you explicitly do setLayout(null)the sizes you set will be discarded and the current layout manager will assign new sizes to the components.)

(除非您明确执行setLayout(null)您设置的尺寸,否则当前布局管理器将为组件分配新尺寸。)

You say that you're using a GridLayout. This layout is fairly "unflexible", and if you want the benefits of GridLayoutbut more flexibility, you could give GridBagLayouta try.

你说你正在使用一个GridLayout. 这种布局相当“不灵活”,如果您想要GridLayout更多灵活性的好处,您可以GridBagLayout尝试一下。

If you don't find a layout manager that suites your needs, don't be afraid of writing your own. (It's five relatively easy methods to implement.)

如果您找不到适合您需求的布局管理器,请不要害怕编写自己的布局管理器。(这是五种相对容易实现的方法。)

If you post a screen-shot of your current application, and an explanation on how you want it to look, we'll be able to help you further.

如果您发布当前应用程序的屏幕截图,并解释您希望它的外观,我们将能够为您提供进一步的帮助。

Some useful links

一些有用的链接

回答by Stewart Murrie

When you are using layout managers (like GridLayout) then calls to setSize()generally don't work, because the layout manager ultimately decides how to size and place each component in a container.

当您使用布局管理器(如GridLayout)时,setSize()通常调用 以不起作用,因为布局管理器最终决定如何调整每个组件的大小并将其放置在容器中。

What you can do is call setPreferredSize(), setMaximumSize()and/or setMinimumSize(). Depending on the layout manager in use one or more of these requests (because that's really what they are) will be honoured. When you call frame.pack()it will try to size everything in the container (and subcontainers) to their preferred sizes.

您可以做的是调用setPreferredSize(),setMaximumSize()和/或setMinimumSize(). 根据使用的布局管理器的不同,这些请求中的一个或多个(因为它们确实是这样)将得到尊重。当您调用frame.pack()它时,它会尝试将容器(和子容器)中的所有内容调整为首选大小。

So, in your case, calling myButton.setPreferredSize (10, 10);will probably work. However, it will still be resized if the container changes size. If you don't want that, there are other solutions I can post.

所以,在你的情况下,打电话myButton.setPreferredSize (10, 10);可能会奏效。但是,如果容器改变大小,它仍然会被调整大小。如果您不想要那样,我可以发布其他解决方案。

回答by Varun Madiath

When aloobe seems to have a more general solution for you. I'd say the immediate solution would be to add the button to a JPanel, set the Layoutmanager a layout like GridLayout, or another LayoutManager, if you find another suits you better.

当 aloobe 似乎为您提供了更通用的解决方案时。我会说直接的解决方案是将按钮添加到 JPanel,将 Layoutmanager 设置为 GridLayout 或另一个 LayoutManager 之类的布局,如果您发现另一个更适合您的话。

And add this panel to your original panel, in place of the button.

并将此面板添加到您的原始面板中,以代替按钮。