2015-06-14 87 views
2

我試圖實現工資單生成器。我已經有了我的GUI,可以在單獨的JPanel上生成工資單。當我點擊「另存爲PNG」按鈕時,它當前將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); 
    } 
} 

在這裏我可以指定路徑和文件名。我想爲"Save as png"按鈕同樣的東西。但我不知道該怎麼做。有人可以幫忙嗎?提前致謝。

回答

1

您可以使用JFileChooser

   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(); 

       } 
      }