简体   繁体   English

Java GUI 中的旋转方形面板

[英]A rotated square panel in Java GUI

I wonder if it is possible to implement a GUI panel (possibly JPanel) that is of square shape but rotated 90 degrees.我想知道是否可以实现一个方形但旋转 90 度的 GUI 面板(可能是 JPanel)。 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.更具体地说,我会将一个面板(称为“A”)分成 4 个相等的正方形子面板,并用 JLabels 填充这些子面板,我正在考虑使用 GridLayout。 And lastly, I would rotate 'A' 90 degrees to give what I want.最后,我会将“A”旋转 90 度以给出我想要的。

From my reading of other similar questions, it seems that you cannot rotate JPanel itself, but you can rotate what is contained within.从我对其他类似问题的阅读来看,您似乎无法旋转 JPanel 本身,但您可以旋转其中包含的内容。 Is this applicable to my case here?这适用于我的情况吗? Would appreciate if someone could point out.如果有人能指出,将不胜感激。 Thanks.谢谢。

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.附录1:正如@Atreys 评论,旋转的组件被绘制,但交互不佳。 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.附录2:如果你还需要变换鼠标坐标,这个例子可能会有所帮助。

Addendum 3: Alternatively, consider the drawString() examples examined here .附录 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();
            }
        });
    }
}

Check out JXTransformer in the SwingHelper project over at java.net.在 java.net 上的SwingHelper项目中查看JXTransformer This class acts as a component decorator that allows you to apply an arbitrary affine transform to a component.这个 class 充当组件装饰器,允许您对组件应用任意仿射变换。

Yes, you would have to have the top-level container (JPanel or other container) be the item that rotates the contents.是的,您必须让顶级容器(JPanel 或其他容器)成为旋转内容的项目。 Really you aren't rotating the items, you are rotating to the painting of the items.实际上,您不是在旋转项目,而是在旋转项目的绘画。

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.如果您需要做的只是旋转 JLabel 上的文本,您可以使用Rotated Icon ,那么您不必担心旋转面板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM