Java 中 JPanel 中的自动定位组件
我在运行时向 JPanel 添加一些 JCheckBox 组件。
我的问题是所有这些组件都添加在同一个地方。
所以他们互相展示。
有什么方法可以将它们设置为自动定位吗?
我的意思是,它们在 JPanel 中的前一个组件旁边找到了一个位置,并且我摆脱了手动定位它们。
谢谢
I'm adding some JCheckBox components to a JPanel at runtime.
My problem is that all these components are added on the same place.
So they are displaying on each other.
Is there any way to set them for auto positioning?
I mean they find a place next to previous component in JPanel and I get rid of manual positioning them.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的答案是使用 LayoutManager。但是,LayoutManager 的工作方式非常不同,具体取决于您的需要。因此,您需要知道动态复选框的布局方式。 UI 中的可扩展部分将吸收可能的 N 个复选框。如果不知道您想要如何直观地布局组件,我无法真正建议您哪种布局管理器最适合您的情况。
关于 LayoutManagers 的一般建议是 TableLayout 是非常好的动态组件。它非常简单,而且仍然非常强大。比 GridBagLayout 表现更好,需要的代码少得多,并且比 SpringLayout 或 MigLayout 更容易理解。获取它,学习如何使用它,然后真正问问自己。
我是否真的需要动态构建复选框 UI,或者我可以使用 CardLayout 中的一组简单面板来完成此操作,并根据某些输入在它们之间进行切换(这比运行时控件的动态布局简单得多)。
http://java.net/projects/tablelayout
你就明白了。
The short answer is use a LayoutManager. But, LayoutManager work very differently depending on what you need. So you'll need to know how you want your dynamic checkboxes to layout. Where in your UI will be the expandable part to absorb potentially N number of checkboxes. Without knowing how you want to visually layout components you I can't really advise you which layout manager is best for your situation.
General advise about LayoutManagers is TableLayout is very good a dynamic components. It's very straight forward, and still very powerful. Better behaved and much much less code needed than say GridBagLayout, and simpler to understand than say SpringLayout or MigLayout. Get it, learn how to use it, then really ask yourself.
Do I truly need to dynamically build my UI of checkboxes or can I accomplish this with a set of simple panels in a CardLayout and switch between them based on some input (this is much much simpler than dynamic layout of controls at runtime).
http://java.net/projects/tablelayout
You get the picture.