相关疑难解决方法(0)

Swing:获取JFrame的图像

我如何获得java.awt.ImageJFrame?

我想获得一个屏幕截图JFrame(以后在我的应用程序中使用).目前,这是通过使用机器人拍摄指定所JFrame涉及的坐标和尺寸的屏幕截图来完成的.

但是,我相信有一种更好的方法:默认情况下,Swing组件在将自己绘制到屏幕上之前将它们自身渲染为双缓冲区.

有没有办法从组件中获取这些图像?

java swing screenshot image jframe

62
推荐指数
1
解决办法
4万
查看次数

为什么JTable标题没有出现在图像中?

我提供了关于在Java API或工具上捕获表格数据图像的建议,以便将表格数据转换为PNG图像文件 - 当OP请求代码示例时.事实证明比我想象的更难!该JTable头从该代码写入PNG消失.

PNG

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)

java swing image jtable

27
推荐指数
4
解决办法
6634
查看次数

在运行时删除顶级容器

不幸的是,看起来这个最近封闭的问题还不太清楚.这是典型的输出:

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)

java swing runtime jdialog

18
推荐指数
4
解决办法
9582
查看次数

处理JFrame会导致内存泄漏吗?

我正在编写一个测试程序如下:

  1. 当用户单击按钮A时,它会打开50个JFrame.
  2. 当用户单击按钮B时,它会通过单击按钮A来处置所有显示的JFrame.

我发现单击按钮B后内存不会减少.我使用Windows中的任务管理器ctrl+ alt+ 确定了这一点del,并检查了"java"的内存使用情况.

java memory swing memory-leaks jframe

8
推荐指数
1
解决办法
4243
查看次数

JFrame到图像而不显示JFrame

我试图将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)

然而,当这仅适用JFramesetVisible(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 swing bufferedimage jframe

4
推荐指数
1
解决办法
4246
查看次数