Java GUI 中的旋转方形面板
我想知道是否可以实现一个方形但旋转 90 度的 GUI 面板(可能是 JPanel)。显然,将有一个包含此面板的顶级容器,并且从视觉上看,主面板就是其中的旋转方形面板。
更具体地说,我会将一个面板(称为“A”)分成 4 个相等的方形子面板,并用 JLabels 填充这些子面板,我正在考虑使用 GridLayout。最后,我会将“A”旋转 90 度以获得我想要的效果。
从我对其他类似问题的阅读来看,您似乎无法旋转 JPanel 本身,但您可以旋转其中包含的内容。这适用于我这里的情况吗?如果有人能指出,我将不胜感激。谢谢。
I wonder if it is possible to implement a GUI panel (possibly JPanel) that is of square shape but rotated 90 degrees. Obviously, there will be a top-level container which contains this panel, and visually the main panel is this rotated square panel within.
More specifically, I would divide a panel (called 'A') into 4 equal square sub-panels, and fill these sub-panels with JLabels, for which I am thinking to use GridLayout. And lastly, I would rotate 'A' 90 degrees to give what I want.
From my reading of other similar questions, it seems that you cannot rotate JPanel itself, but you can rotate what is contained within. Is this applicable to my case here? Would appreciate if someone could point out. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
关键的事情似乎是在旋转图形上下文之后绘制组件。下面是一个示例:
附录 1:正如 @Atreys 注释,旋转的组件已绘制,但交互效果很差。如果组件必须保持可用,则事件坐标也应进行转换。比较这个(相当)复杂的示例,< em>镜像组件。
附录2:如果您还需要转换鼠标坐标,则此 示例可能会有所帮助。
附录 3:或者,考虑此处检查的
drawString()
示例。The critical thing seems to be painting the components after rotating the graphics context. Here's an example:
Addendum 1:As @Atreys comments, the rotated components are drawn, but interact poorly. If the components must remain usable, event coordinates should also be transformed. Compare this (considerably) more complex example that mirrors components.
Addendum 2: If you also need to transform the mouse coordinates, this example may be helpful.
Addendum 3: Alternatively, consider the
drawString()
examples examined here.查看 JXTransformer 中的 SwingHelper 项目位于 java.net。此类充当组件装饰器,允许您对组件应用任意仿射变换。
Check out JXTransformer in the SwingHelper project over at java.net. This class acts as a component decorator that allows you to apply an arbitrary affine transform to a component.
是的,您必须让顶级容器(JPanel 或其他容器)成为旋转内容的项目。实际上,您不是在旋转物品,而是在旋转物品的绘画。
Yes, you would have to have the top-level container (JPanel or other container) be the item that rotates the contents. Really you aren't rotating the items, you are rotating to the painting of the items.
如果您需要做的只是旋转 JLabel 上的文本,您可以使用 旋转图标,那么你就不用担心旋转面板了。
If all you need to do is rotate the text on a JLabel you could use a Rotated Icon, then you don't have to worry about rotating the panel.