gpt4 book ai didi

java - 截图

转载 作者:行者123 更新时间:2023-12-02 07:17:17 29 4
gpt4 key购买 nike

我想对我创建的面板进行屏幕截图,代码如下。任何人都可以告诉我为什么我没有得到。谢谢

    public static final void makeScreenshot(JFrame argFrame) 
{
Rectangle rec = argFrame.getBounds();
BufferedImage bufferedImage = new BufferedImage(rec.width, rec.height, BufferedImage.TYPE_INT_ARGB);
argFrame.paint(bufferedImage.getGraphics());

try
{
// Create temp file.
File temp = File.createTempFile("C:\\Documents and Settings\\SriHari\\My Documents\\NetBeansProjects\\test\\src\\testscreenshot", ".jpg");

// Use the ImageIO API to write the bufferedImage to a temporary file
ImageIO.write(bufferedImage, "jpg", temp);

//temp.deleteOnExit();
}


catch (IOException ioe) {}
} //
public static void main(String args[])
{
TimeTableGraphicsRunner ts= new TimeTableGraphicsRunner();
for(long i=1;i<1000000000;i++);
ts.makeScreenshot(jf);
System.out.println("hi");
}

最佳答案

以下内容对我有用:

public static void main (String [] args)
{
final JFrame frame = new JFrame ();

JButton button = new JButton (new AbstractAction ("Make Screenshot!")
{
@Override
public void actionPerformed (ActionEvent e)
{
Dimension size = frame.getSize ();
BufferedImage img = new BufferedImage (size.width, size.height, BufferedImage.TYPE_3BYTE_BGR);
Graphics g = img.getGraphics ();
frame.paint (g);
g.dispose ();
try
{
ImageIO.write (img, "png", new File ("screenshot.png"));
}
catch (IOException ex)
{
ex.printStackTrace ();
}
}
});

frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane ().setLayout (new BorderLayout ());
frame.getContentPane ().add (button, BorderLayout.CENTER);
frame.pack ();
frame.setVisible (true);
}

它不会渲染窗口标题和边框,因为这是由操作系统处理的,而不是 Swing。

关于java - 截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14786319/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com