简体   繁体   English

如何在有或没有标题栏的情况下打印JFrame?

[英]How to print JFrame with or without Title Bar?

My code is basically like following: 我的代码基本上如下:

BufferedImage image = ......
Graphics g = image.createGraphics();
jFrame.printAll(g);

When I view the image, the title bar area always black... 当我查看图像时,标题栏区域始终为黑色...

Does any know how to print jFrame with title bar or without title bar? 有什么知道如何打印带标题栏或不带标题栏的jFrame吗?

Does any know how to print jFrame with title bar or without title bar? 有什么知道如何打印带标题栏或不带标题栏的jFrame吗?

Print just the content pane. 仅打印内容窗格。

jFrame.getContentPane().printAll(g);

When I view the image, the title bar area always black... 当我查看图像时,标题栏区域始终为黑色...

The title bar (and borders) are not painted by Swing since they are a window from the platform you are using. Swing不会绘制标题栏(和边框),因为它们是您所使用平台的一个窗口。

If you want to get an image of the entire frame then you need to use Robot.createScreenCapture() . 如果要获取整个帧的图像,则需要使用Robot.createScreenCapture() The Screen Image class will do this easily for you. 屏幕图像类将为您轻松完成此操作。

Then you can try to print the BufferedImage that is created. 然后,您可以尝试打印创建的BufferedImage。 I'm not sure the best way to do this, but you could just create an ImageIcon using the buffered image. 我不确定执行此操作的最佳方法,但是您可以使用缓冲的图像来创建ImageIcon。 Then you could add the icon to a JLabel and then print the label. 然后,您可以将图标添加到JLabel,然后打印标签。 You would probably need to invoke label.setSize( label.getPreferredSize() ) before doing this. 在执行此操作之前,您可能需要调用label.setSize(label.getPreferredSize())。

试试: yourFrame.setUndecorated(true);

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

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