如何在其他地方绘制不可见的 JFrame? [英] How to paint an invisible JFrame elsewhere?

查看:54
本文介绍了如何在其他地方绘制不可见的 JFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 JFrame 的内容绘制到另一个框架上.目前,我只有在 JFrame 可见时才能使用它.
有没有办法绘制隐藏的 JFrame?

I want to paint the contents of a JFrame onto another frame. Currently, I only get it to work if the JFrame is visible.
Is there a way to paint a hidden JFrame?

附加信息:
在我的项目中,我需要能够旋转和缩放窗口.我不想编写自己的window-api,所以我想我可能能够以旋转的方式绘制JFrames或类似的容器类(Graphics2D-API很好地支持).能够为此使用标准 JFrame 真是太棒了,但是扩展 JFrame 的自定义框架也可以..

Additional info:
In my project I need to be able to rotate and scale windows. I do not want to write my own window-api, so I thought I might be able to just paint JFrames or similar container classes in a rotated way (which the Graphics2D-API supports perfectly well). It would be awesome to be able to use standard JFrames for that, but a custom frame extending a JFrame would also be OK..

public class JFTest extends JFrame {
    private JFrame frameToPaint = null;

    public static void main (String[] args) {
        new JFTest ();
    }

    public JFTest () {
        // some basic initialization
        super ("Container");
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setExtendedState (JFrame.MAXIMIZED_BOTH);
        add (new JPanel () {
            @Override public void paintComponent (Graphics g) {
                super.paintComponent (g);
                // painting the child frame's contents onto this frame
                if (frameToPaint != null) frameToPaint.getRootPane().paintAll (g);
            }
        });
        setVisible (true);

        // initializing some test-frame that will get painted onto the container
        frameToPaint = new JFrame ("child");
        frameToPaint.setSize (200, 100);
        frameToPaint.add (new JLabel ("test"));
        frameToPaint.addComponentListener (new ComponentAdapter() {
            @Override public void componentResized (ComponentEvent e) { repaint (); }
            @Override public void componentHidden  (ComponentEvent e) { repaint (); }
        });

        // magic line. an invisible frame will not get painted! why??
        frameToPaint.setVisible (true);
    }
}

推荐答案

我想获取框架的图形内容,而不必让用户看到框架

I want to get the graphical contents of a frame without having to make the frame visible for the user

Screen Image 类应该会有所帮助.虽然我认为它只适用于框架的内容窗格"而不是整个框架(带有标题栏和边框),除非您使用装饰框架.

The Screen Image class should help. Although I think it will only work for the "content pane" of the frame and not the entire frame (with the title bar and borders) unless you use a decorated frame.

这篇关于如何在其他地方绘制不可见的 JFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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