如何在其他地方绘制不可见的 JFrame?

发布于 2024-12-13 10:17:24 字数 1540 浏览 0 评论 0原文

我想将 JFrame 的内容绘制到另一个框架上。目前,我只有在 JFrame 可见的情况下才能使其工作。
有没有办法绘制隐藏的 JFrame?

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

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 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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

萌︼了一个春 2024-12-20 10:17:24

提示 1:JFrame 的 setDefaultLookAndFeelDecolated(false)/setUndecorated(true) 可能适用于没有标题和边框的窗口;

提示 2:因为 setGlassPane/setLayeredPane/setOpaque(false) 可能可用于第二个“层”。

Hint 1: JFrame's setDefaultLookAndFeelDecorated(false)/setUndecorated(true) might be of use for a window without caption and borders;

Hint 2: as setGlassPane/setLayeredPane/setOpaque(false) might be of use for a second "layer".

ㄖ落Θ余辉 2024-12-20 10:17:24

我想获取框架的图形内容,而不必使框架对用户可见

屏幕图像 类应该有帮助。尽管我认为它仅适用于框架的“内容窗格”,而不适用于整个框架(带有标题栏和边框),除非您使用装饰框架。

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

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.

白首有我共你 2024-12-20 10:17:24

1)你必须使用正确的 LayoutManager,而不是 setSize()setBounds()

2) 如果有 null LayoutManager 使用,然后 Container 返回任何setVisible(true);

3) 如果使用了正确的 LayoutManager,则 Container 返回其 Size 之后称呼pack();,另一方面,此容器在屏幕上不可见(意味着 setVisible(true);

4) JComponents 必须返回 示例

1) you have to use proper LayoutManager, not setSize() or setBounds()

2) if is there null LayoutManager used then Container returns any size after setVisible(true);

3) if is there used proper LayoutManager, then Container return its Size after call pack();, in other hands this container couldn't be visible on the screen ( meaning setVisible(true); )

4) JComponents must to returns PrefferedSize for example

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