如何在摇摆中将面板保存为图像? [英] how to save panel as image in swing?

查看:115
本文介绍了如何在摇摆中将面板保存为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想将包含标签和按钮等组件的面板转换为图像文件。

Hi i want to convert panel which contains components like label and buttons to image file.

我已完成以下代码。图像已保存。但面板的内容不可见或保存。任何人都可以告诉我如何使用其组件保存面板。

I have done the following code. The image was saved. but the content of the panel not visible or saved. Can anyone tell me how to save the panel with its components.

代码:

package PanelToImage;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;

public class sample extends JPanel {

public JPanel firstpanel;
public JPanel secondpanel;
JLabel label1, label2;
JButton button1, button2;

public sample() {
    firstpanel = new JPanel();
    firstpanel.setSize(400,300); 
    firstpanel.setBackground(Color.RED);
    secondpanel = new JPanel();
    secondpanel.setBackground(Color.GREEN);
    secondpanel.setSize(400,300); 

    label1 = new JLabel("label1");
    label2 = new JLabel("label2");
    button1 = new JButton("button1");
    button2 = new JButton("button2");

    firstpanel.add(label1);
    firstpanel.add(button1);

    secondpanel.add(label2);
    secondpanel.add(button2);

    saveImage(firstpanel);

    add(firstpanel);

    // add(secondpanel);
}

public static void main(String args[]) {

    JFrame frame = new JFrame();
    sample sam = new sample();
    frame.setContentPane(sam);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);

}

private void saveImage(JPanel panel) {
    BufferedImage img = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB);
    panel.paint(img.getGraphics());
    try {
        ImageIO.write(img, "png", new File("E://Screen.png"));
        System.out.println("panel saved as image");

    } catch (Exception e) {
        System.out.println("panel not saved" + e.getMessage());
    }
}
}


推荐答案

这段代码适用于我(在 JFrame 中):

Tthis code works for me (in the JFrame):

Container c = getContentPane();
BufferedImage im = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
c.paint(im.getGraphics());
ImageIO.write(im, "PNG", new File("shot.png"));

也许您使用过自定义面板。如果为true,请尝试在面板的 paint 方法的开头添加 super.paint(g)

Maybe you have used custom panels. If true, try to add super.paint(g) at the beginning of the paint methods of your panels.

编辑:您必须在显示后调用 saveImage 框架:

EDIT: You have to call saveImage after display the frame:

public static void main(String args[]) {
    ...
    frame.setSize(400, 300);
    sam.saveImage(sam.firstpanel);
}

编辑2 :这是保存的图片(很少因为布局,但证明它应该工作):

EDIT 2: This is the saved image (is little because the layout, but is the proof that it should work):

我在<$中调用 saveImage 作为最后一次调用c $ c> main ,并使用用户目录中的文件( new File(Screen.png)nIcE cOw 说。

I called the saveImage as last call in the main, and used a file in the user dir (new File("Screen.png")) as nIcE cOw said.

这篇关于如何在摇摆中将面板保存为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆