我如何获得java.awt.ImageJFrame?
我想获得一个屏幕截图JFrame(以后在我的应用程序中使用).目前,这是通过使用机器人拍摄指定所JFrame涉及的坐标和尺寸的屏幕截图来完成的.
但是,我相信有一种更好的方法:默认情况下,Swing组件在将自己绘制到屏幕上之前将它们自身渲染为双缓冲区.
有没有办法从组件中获取这些图像?
我提供了关于在Java API或工具上捕获表格数据图像的建议,以便将表格数据转换为PNG图像文件 - 当OP请求代码示例时.事实证明比我想象的更难!该JTable头从该代码写入PNG消失.


import javax.swing.*;
import java.awt.Graphics;
import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
class TableImage {
public static void main(String[] args) throws Exception {
Object[][] data = {
{"Hari", new Integer(23), new Double(78.23), new Boolean(true)},
{"James", new Integer(23), new Double(47.64), new Boolean(false)},
{"Sally", new Integer(22), new Double(84.81), new Boolean(true)}
};
String[] columns = {"Name", "Age", "GPA", "Pass"};
JTable table = new JTable(data, columns);
JScrollPane scroll = new JScrollPane(table);
JPanel p …Run Code Online (Sandbox Code Playgroud) 不幸的是,看起来这个最近封闭的问题还不太清楚.这是典型的输出:
run:
Trying to Remove JDialog
Remove Cycle Done :-)
Checking if still exists any of TopLayoutContainers
JFrame
JDialog
Will Try Remove Dialog again, CycleNo. 1
-----------------------------------------------------------
Trying to Remove JDialog
Remove Cycle Done :-)
Checking if still exists any of TopLayoutContainers
JFrame
JDialog
Will Try Remove Dialog again, CycleNo. 2
-----------------------------------------------------------
Trying to Remove JDialog
Remove Cycle Done :-)
Checking if still exists any of TopLayoutContainers
JFrame
JDialog
Will Try Remove Dialog again, CycleNo. 3
-----------------------------------------------------------
Trying …Run Code Online (Sandbox Code Playgroud) 我正在编写一个测试程序如下:
我发现单击按钮B后内存不会减少.我使用Windows中的任务管理器ctrl+ alt+ 确定了这一点del,并检查了"java"的内存使用情况.
我试图将JFrame渲染到图像而不显示JFrame本身(类似于这个问题所要求的).我试过使用这段代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
然而,当这仅适用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);
}
}
Run Code Online (Sandbox Code Playgroud)
然而,这显示黑色图像(如上面的代码).我很确定我所追求的是可能的,问题是我无法真正找到.我对自定义Swing组件的体验非常有限,因此任何信息都将受到赞赏.
谢谢.
java ×5
swing ×5
jframe ×3
image ×2
jdialog ×1
jtable ×1
memory ×1
memory-leaks ×1
runtime ×1
screenshot ×1