将 JPanel 添加到 JList 中?
使用 NetBeans GUI 编辑器为学校创建保龄球程序。我可以将 JPanel 添加到 JList 吗?如果是这样怎么办?
Using NetBeans GUI editor to create a bowling program for school. Is it possible for me to add a JPanel to a JList? if so how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这实际上是不可能的(意味着它不会按照您的预期运行) - 您真正想要的是一个列表
LayoutManager
,它将在垂直或水平列表中布局组件。因此,您可以使用带有类似列表的布局管理器的JPanel
,而不是使用JList
。尝试这些:
BoxLayout
将放置单个列/行中的所有JPanel
GridLayout
会将所有JPanels
放在单个列/行中,并使它们的大小相同It's not really possible (meaning it won't behave as you'd expect) - what you actually want is a list
LayoutManager
that will lay out the components in a vertical or horizontal list. So instead of usingJList
, you'd use aJPanel
with a list-like layout manager.Try these:
BoxLayout
will put all theJPanels
in a single column/rowGridLayout
will put all theJPanels
in a single column/row and make them all the same size试试这个。这对我有用。
Try This. it's works for me.
转到本教程:
它与您的问题具有相同的概念...
http:// docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer
goto this tutorial:
it has the same concept with your problem...
http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer
(编辑:我已经使用这个有一段时间了,我很惊讶。迄今为止我见过的最快的列表组件。为什么我以前没有见过这个?)
我刚刚制作了一些不是 JList 的东西,所以它缺少很多功能,但您可以很容易地添加这些功能。
但您得到的是:一个列表(所有成员大小相同。),可以轻松容纳约 20 亿个面板,没有内存或性能问题 - 请参阅演示代码。此外,JPanel 可以包含您想要的任何内容,这些组件将正常工作。
在演示中,JPanel 成员没有内部 JPanel,并且对鼠标事件完全透明(JButton 除外,这很好):添加到整个容器的侦听器接收它们,如演示中所示。如果您添加更多组件层次结构,事情可能会变得棘手,IDK。
不管怎样,这个东西快如闪电,最重要的是,它完成了工作:列表中的 JPanel 既可以操作,也可以选择。 (没有内置选择代码,但就像我说的:很容易做到。鼠标悬停演示代码在里面。)
演示类:
FastPanelList 类:
(EDIT: I've used this for a while now and am surprised. Fastest list component I've seen so far. Why haven't I seen this before?)
I just whipped up something that is not a JList, so it lacks a lot of features, but you can rather easily add those.
But what you get is this: A list (All members equal size.) that can easily hold ~2 billion panels with no memory or performance problems - see demo code. Also, the JPanels can contain anything you want, those components will work normally.
In the demo, the JPanel members have no inner JPanels and are completely transparent to mouse events (except for the JButtons, and that's good): A listener added to the overall container receives them, as shown in the demo. If you add more component hierarchy, things might get tricky, IDK.
Anyway, this thing is lightning fast and, most of all, gets the job done: JPanels in a list that you can operate but also select. (No selection code built-in, but like I said: Easy to do. Mouse hover demo code inside.)
Demo class:
FastPanelList class: