我想将 JFrame 的内容绘制到另一个框架上。目前,我只有在 JFrame 可见时才能使用它。
有没有办法绘制隐藏的 JFrame?
附加信息:
在我的项目中,我需要能够旋转和缩放窗口。我不想编写自己的 window-api,所以我想我也许能够以旋转的方式绘制 JFrames 或类似的容器类(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);
}
}
最佳答案
提示 1:JFrame 的 setDefaultLookAndFeelDecorated(false)/setUndecorated(true) 可能对没有标题和边框的窗口有用;
提示 2:因为 setGlassPane/setLayeredPane/setOpaque(false) 可能对第二个“层”有用。
关于java - 如何在别处绘制不可见的 JFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8008087/