我试图将JFrame渲染到图像而不显示JFrame本身(类似于this问题所要求的).我试过使用这段代码:
private static BufferedImage getScreenShot(Component component)
{
BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB);
// call the Component's paint method, using
// the Graphics object of the image.
component.paint(image.getGraphics());
return image;
}
但是,这仅在设置JFrame的setVisible(true)时有效.这将导致图像显示在屏幕上,这不是我想要的.我也尝试过这样的东西:
public class MyFrame extends JFrame
{
private BufferedImage bi;
public MyFrame(String name, BufferedImage bi)
{
this.bi = bi;
super(name);
}
@Override
public void paint(Graphics g)
{
g.drawImage(this.bufferedImage, 0, 0, null);
}
}
然而,这显示黑色图像(如上面的代码).我很确定我所追求的是可能的,问题是我无法真正找到.我对自定义Swing组件的体验非常有限,因此任何信息都将受到赞赏.
谢谢.