Java 将JPanel保存到png文件中

Java 将JPanel保存到png文件中,java,Java,我正在尝试实现一个工资条生成器。我已经让GUI在单独的JPanel上生成工资单。当我单击“另存为PNG”按钮时,它当前正在将Jpanel保存为项目文件夹中的PNG文件。但我希望它能够在保存时指定文件路径和文件名。以下是我迄今为止所做的工作 public static BufferedImage getScreenShot(Component component) { BufferedImage image = new BufferedImage(component.getWidth(),

我正在尝试实现一个工资条生成器。我已经让GUI在单独的
JPanel
上生成工资单。当我单击“另存为PNG”按钮时,它当前正在将Jpanel保存为项目文件夹中的PNG文件。但我希望它能够在保存时指定文件路径和文件名。以下是我迄今为止所做的工作

public static BufferedImage getScreenShot(Component component) {
    BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB);
    component.paint(image.getGraphics());
    return image;
}

public void saveScreenShot(Component component, String fname) throws Exception {

    BufferedImage img = getScreenShot(component);
    ImageIO.write(img, "png", new File(fname));
}
private void SaveAsPNGButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                

    try {
        saveScreenShot(PaySlip, "My panel Image.png");
    } catch (Exception e) {

    }
}
我希望它像下面的代码一样。我用它将搜索结果从
Jtable
保存到文本文件中

 private void Export2TextActionPerformed(java.awt.event.ActionEvent evt) {                                            

    try {
        JFileChooser fc = new JFileChooser();
        int option = fc.showSaveDialog(SearchEmployeeGUI.this);
        if (option == JFileChooser.APPROVE_OPTION) {
            try {
                String filename = fc.getSelectedFile().getName();
                String path = fc.getSelectedFile().getParentFile().getPath();

                int len = filename.length();
                String ext = "";
                String file = "";

                if (len > 4) {
                    ext = filename.substring(len - 4, len);
                }

                if (ext.equals(".txt")) {
                    file = path + "\\" + filename;
                } else {
                    file = path + "\\" + filename + ".txt";
                }
                FileWriter fw = new FileWriter(file);
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write("Employee ID       First Name      Last Name       Gender      Contact No      Email       Date of Join        Designation     Basic Salary");
                bw.newLine();
                bw.write("--------------------------------------------------------------------------------------------------------------------------------------------");
                bw.newLine();

                for (int i = 0; i < EmployeeTable.getRowCount(); i++) {
                    for (int j = 0; j < EmployeeTable.getColumnCount(); j++) {
                        bw.write(EmployeeTable.getModel().getValueAt(i, j) + "    ");
                    }
                    bw.newLine();

                }
                bw.close();
                fw.close();
                int answer = JOptionPane.showConfirmDialog(null, "Would you like to open the exported file?", "Successfully exported!", option);
                if (answer == JOptionPane.YES_OPTION) {
                    try {
                        Desktop dt = Desktop.getDesktop();
                        dt.open(new File(file));

                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(null, e);
                    }
                }

            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}  
private void Export2TextActionPerformed(java.awt.event.ActionEvent evt){
试一试{
JFileChooser fc=新的JFileChooser();
int option=fc.showsavedilog(SearchEmployeeGUI.this);
if(option==JFileChooser.APPROVE\u选项){
试一试{
字符串文件名=fc.getSelectedFile().getName();
字符串路径=fc.getSelectedFile().getParentFile().getPath();
int len=filename.length();
字符串ext=“”;
字符串文件=”;
如果(len>4){
ext=filename.substring(len-4,len);
}
如果(ext.equals(“.txt”)){
文件=路径+“\\”+文件名;
}否则{
文件=路径+“\\”+文件名+“.txt”;
}
FileWriter fw=新的FileWriter(文件);
BufferedWriter bw=新的BufferedWriter(fw);
填写(“员工ID姓名性别联系人无电子邮件加入日期指定基本工资”);
换行符();
写(“------------------------------------------------------------------------------------------------------------------------------------------------------”);
换行符();
for(int i=0;i
在这里,我可以指定路径和文件名。我想为
“另存为png”
按钮做同样的事情。但我不知道该怎么做。有人能帮忙吗?提前感谢。

您可以使用

                String suggesteddir = ".";
            String EXTENSION = ".png";
            JFileChooser fileChooser = new JFileChooser(suggesteddir);
            JFrame choose = new JFrame();
            choose.setTitle("Save To ...");
             int status = fileChooser.showSaveDialog(choose);
            if (status == JFileChooser.APPROVE_OPTION) 
            {

                try 
                {
                    File selectedFile = fileChooser.getSelectedFile();
                    String newfile = selectedFile.getCanonicalPath();
                    if (!newfile.endsWith(EXTENSION)) {
                        newfile=newfile + EXTENSION;
                    }

                    ImageIO.write(img, "png", new File(newfile)); //write img to file

                } catch (IOException ex) {
                    ex.printStackTrace();

                }
            }