动态窗口大小和按钮
我有一个 JPanel,我正在运行时动态创建 JButton。我希望 JPanel 大小能够根据添加到其中的 JButton 数量动态调整。我应该使用什么布局?我的 JPanel 将被添加到 JFrame 中,所以任何人都可以解释我必须做什么才能确定我想要做什么?
编辑 经过一番研究后,我使用以下布局mode.setLayout(new GridLayout(5, 0, 1, 1));
这里的mode
是JPanel。现在,如果我只将 2 个 JButton 添加到 JPanel,则按钮将显示在另一个下面,这很好。但我在这两个按钮的底部获得了额外的空间。如果我有 15 个按钮,我会得到 5 行和 3 列,这也很好,但是窗口尺寸很小,我必须在运行时手动调整窗口大小才能完全显示按钮。
具体问题:如何管理上述动态按钮场景中的窗口大小?
I have a JPanel and i am creating JButtons dynamically on runtime. I want the JPanel size to be adjusted dynamically according to the number of JButtons added to it. What layout should i use ? And my JPanel will be added to the JFrame so can anyone explain what do i have to do to establish what I am trying to do ?
EDIT
After a bit of research I am using the following layoutmode.setLayout(new GridLayout(5, 0, 1, 1));
Here mode
is the JPanel. Now if i have just 2 JButtons added to the JPanel the buttons will be displayed one below the other which is fine. But I am getting extra space at the bottom of those 2 buttons. If I have 15 buttons I am getting 5 rows and 3 columns which is again fine but the window size is small I and I have to resize the window manually on runtime for displaying the buttons fully.
Specific Question: How to manage the window size on the above dynamic buttons scenario ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
动态添加组件的基本代码为:
更新:
可以使用以下方式获取需要打包的Window:
The basic code for adding components dynamically is:
Update:
You can use the following to get the Window that needs to be packed: