2

在我们的企业中,我们使用 JFrame.paintAll() 来制作我们自己的(开源)应用程序的屏幕截图。不幸的是,这仅适用于 Windows 和 Mac OS,但在 Linux 上不起作用。

在一个德国论坛上,我找到了一个 SSCCE:

public class SSCCEPaint
{
     public static void main(String[] args) {
       //create a simple JFrame
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200,200);
        frame.setVisible(true);

        //craate an image to draw
        BufferedImage img = new BufferedImage(300,300, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = img.createGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 300, 300);

        //paint the whole JFrame on the picture 
        frame.paintAll(g);

        //Show the picture in a second frame
        JFrame frame2 = new JFrame("Picture");
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.add(new JScrollPane(new JLabel(new ImageIcon(img))));
        frame2.setSize(300,300);
        frame2.setLocation(200,0);
        frame2.setVisible(true);

    }
}

这个 SSCCE 似乎可以根据论坛中的人的说法工作,但作者想用边框画出他的框架。因此他最终使用了 JFrame.printAll()。顺便说一句, printAll() 在我的机器上工作,但我想要没有边框的屏幕截图。

我也已经用 Robot 类写了一个变通方法,但它不如 paintAll() 方法优雅。所以我更喜欢paintAll() 解决方案。我还尝试了 frame.getContentPane().paintAll(),这至少可以工作,但是没有绘制菜单栏。

有人知道更多关于这个话题吗?

4

0 回答 0