Java GUI 中的旋转方形面板 [英] A rotated square panel in Java GUI

查看:29
本文介绍了Java GUI 中的旋转方形面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能实现一个方形但旋转 90 度的 GUI 面板(可能是 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.

更具体地说,我会将一个面板(称为A")划分为 4 个相等的正方形子面板,并用 JLabel 填充这些子面板,我正在考虑使用 GridLayout.最后,我会将A"旋转 90 度以获得我想要的效果.

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.

从我对其他类似问题的阅读来看,您似乎无法旋转 JPanel 本身,但可以旋转其中包含的内容.这适用于我这里的情况吗?如果有人能指出,将不胜感激.谢谢.

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.

推荐答案

关键似乎是在旋转图形上下文之后 绘制组件.举个例子:

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

附录 1:正如@Atreys 评论的那样,绘制了旋转的组件,但交互效果不佳.如果组件必须保持可用,则还应转换事件坐标.比较这个(相当)更复杂的示例镜像组件.

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.

附录2:如果还需要变换鼠标坐标,这个示例 可能会有所帮助.

Addendum 2: If you also need to transform the mouse coordinates, this example may be helpful.

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

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();
            }
        });
    }
}

这篇关于Java GUI 中的旋转方形面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆