我想通过一个主 JPanel
创建一个商店,其中的每个组件都是一个带有图像、标签和按钮的 JPanel
。
我确实尝试过使用 JList
但 JList
的问题是它仅保留组件的渲染,因此该按钮无法正常工作,并且它是唯一的一个图像。我可以四处走动并使用 MouseEvent,但这对我来说感觉不对,我确信有更好的解决方案。
我希望组件根据帧大小改变其位置,就像在 JList
中一样。
例如,如果我改变屏幕的宽度,组件的位置将随之改变:
对此:
我确实有一个想法,可以在 paintComponent
中使用 GridLayout
或 GridBagLayout
(因为它调用每个渲染。如果您知道另一种方法调用每个渲染 int 我想知道的 JPanel)并通过更改 PaintComponent 内的布局变量来更改组件的位置。
我确实上网寻找解决方案,但我只发现人们使用JTable
,但我看不到它在这里工作。
最佳答案
each component inside it is a JPanel with an image, label and button.
有道理。
changing the positions of the components by changing the layout variables inside the paintComponent.
paintComponent() 方法与更改面板的布局无关。您不应该使用 PaintComponent() 方法。
I do have an idea by using GridLayout or GridBagLayout
您使用布局管理器是正确的,但不幸的是,默认布局管理器都不会自动换行到随机数量的组件。
调整框架大小时会自动调用布局管理器。
所以你可以使用Wrap Layout这是 FlowLayout 的扩展,允许随机换行。
关于java - 如何在 Java Swing 中创建响应式 JList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55818758/