我有一个由 myPanel
标识的 JPanel
。我创建了一系列 JButton
,并将它们 add()
到 myPanel
。在我的生成和添加循环结束时,我调用了 myPanel.validate()
。按钮出现。
问题是面板的大小受到影响。无论我选择什么布局管理器,按钮总是添加在同一行上(即使它们下方有更多空间)。
我已经尝试设置 myPanel
的最大大小并在每次 add()
之后设置它的大小。无论我做什么,在 validate()
之后,面板都会爆炸并且我的应用程序的 GUI 会被搞砸。
有什么想法吗?
最佳答案
I create a series of JButtons, and add() them to myPanel... the buttons are always added on the same line (even though there is more space beneath them).
默认的布局管理器都不提供自动换行。您需要指定希望包装发生的方式。在这些情况下可以使用 GridLayout 或 GridBagLayout。
或者您可以尝试 Wrap Layout这是为此目的而写的。
关于java - add()ing 到 JPanel 会破坏它的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4250091/