Java GUI 中的旋转方形面板

发布于 2024-11-15 03:07:28 字数 285 浏览 2 评论 0原文

我想知道是否可以实现一个方形但旋转 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小鸟爱天空丶 2024-11-22 03:07:29

关键的事情似乎是在旋转图形上下文之后绘制组件。下面是一个示例:

在此处输入图像描述

附录 1:正如 @Atreys 注释,旋转的组件已绘制,但交互效果很差。如果组件必须保持可用,则事件坐标也应进行转换。比较这个(相当)复杂的示例,< em>镜像组件。

附录2:如果您还需要转换鼠标坐标,则此 示例可能会有所帮助。

附录 3:或者,考虑此处检查的 drawString() 示例。

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/** @see https://stackoverflow.com/questions/6333464 */
public class RotatePanel extends JPanel {

    public RotatePanel() {
        this.setPreferredSize(new Dimension(320, 240));
        this.add(new JLabel("Hello World!", JLabel.CENTER));
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        int w2 = getWidth() / 2;
        int h2 = getHeight() / 2;
        g2d.rotate(-Math.PI / 2, w2, h2);
        super.paintComponent(g);
    }

    private void display() {
        JFrame f = new JFrame("RotatePanel");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new RotatePanel().display();
            }
        });
    }
}

The critical thing seems to be painting the components after rotating the graphics context. Here's an example:

enter image description here

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.

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/** @see https://stackoverflow.com/questions/6333464 */
public class RotatePanel extends JPanel {

    public RotatePanel() {
        this.setPreferredSize(new Dimension(320, 240));
        this.add(new JLabel("Hello World!", JLabel.CENTER));
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        int w2 = getWidth() / 2;
        int h2 = getHeight() / 2;
        g2d.rotate(-Math.PI / 2, w2, h2);
        super.paintComponent(g);
    }

    private void display() {
        JFrame f = new JFrame("RotatePanel");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new RotatePanel().display();
            }
        });
    }
}
千纸鹤 2024-11-22 03:07:29

查看 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.

下雨或天晴 2024-11-22 03:07:29

是的,您必须让顶级容器(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.

凝望流年 2024-11-22 03:07:29

如果您需要做的只是旋转 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文