gridLayout 中的拉伸按钮
大家,我对Java有一些问题,我创建了五行一列的面板,我还在其中一行添加了按钮,最后我将此面板添加到框架中,我收到了拉伸到全框架的按钮,可以我减小了按钮的大小,我使用过
myButton.setSize(10,10);
,但似乎不起作用,我也使用过,
frame.pack();
可能是问题所在,提前致谢
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
myButton.setSize(10,10);
but it doesn't seem to work, also I use
frame.pack();
may it be the problem, thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该避免自行设置尺寸。您应该选择一个适合您的
LayoutManager
来完成这项工作。(除非您明确执行
setLayout(null)
,否则您设置的大小将被丢弃,并且当前布局管理器将为组件分配新的大小。)您说您正在使用
GridLayout< /代码>。这种布局相当“不灵活”,如果您想要 GridLayout 的好处但更灵活,您可以提供
GridBagLayout
尝试一下。如果您找不到适合您需求的布局管理器,请不要害怕编写自己的布局管理器。 (这是五种相对容易实现的方法。)
如果您发布当前应用程序的屏幕截图,并解释您希望它的外观,我们将能够为您提供进一步的帮助。
一些有用的链接
You should refrain from setting the size yourself. You should select a proper
LayoutManager
that does the job for you.(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.)You say that you're using a
GridLayout
. This layout is fairly "unflexible", and if you want the benefits ofGridLayout
but more flexibility, you could giveGridBagLayout
a try.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
当 aloobe 似乎为您提供了更通用的解决方案时。我想说,直接的解决方案是将按钮添加到 JPanel,将 Layoutmanager 设置为 GridLayout 之类的布局,或者另一个 LayoutManager(如果您发现另一个更适合您)。
并将此面板添加到原始面板中,代替按钮。
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.
And add this panel to your original panel, in place of the button.
当您使用布局管理器(例如
GridLayout
)时,调用setSize()
通常不起作用,因为布局管理器最终决定如何调整每个组件的大小和位置一个容器。您可以调用
setPreferredSize()
、setMaximumSize()
和/或setMinimumSize()
。根据所使用的布局管理器,这些请求中的一个或多个(因为它们确实如此)将得到满足。当您调用frame.pack()时,它会尝试将容器(和子容器)中的所有内容调整为其首选大小。因此,在您的情况下,调用
myButton.setPreferredSize (10, 10);
可能会起作用。但是,如果容器大小发生变化,它仍然会调整大小。如果您不想这样做,我可以发布其他解决方案。When you are using layout managers (like
GridLayout
) then calls tosetSize()
generally don't work, because the layout manager ultimately decides how to size and place each component in a container.What you can do is call
setPreferredSize()
,setMaximumSize()
and/orsetMinimumSize()
. 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 callframe.pack()
it will try to size everything in the container (and subcontainers) to their preferred sizes.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.