2014-01-09 56 views
0

我想在我的Java GUI揮杆中打印某個部分。所以基本上我有一個框架,然後像文本框,標籤,表格和麪板等其他視圖。在面板內部,我有兩個按鈕。我想排除打印包含兩個按鈕的面板。我怎樣才能做到這一點?到目前爲止,這是我所做的代碼:Java僅打印揮杆中的特定部分

setTitle("VMS Sales Invoice"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 397, 628); 
    getContentPane().setLayout(null); 

    JLabel lblVatable = new JLabel("VATable"); 
    lblVatable.setFont(new Font("SansSerif", Font.BOLD, 12)); 
    lblVatable.setBounds(158, 468, 55, 16); 
    getContentPane().add(lblVatable); 

    txtTotalDiscount = new JTextField(); 
    txtTotalDiscount.setFont(new Font("SansSerif", Font.BOLD, 12)); 
    txtTotalDiscount.setEditable(false); 
    txtTotalDiscount.setBounds(256, 485, 122, 28); 
    getContentPane().add(txtTotalDiscount); 
    txtTotalDiscount.setColumns(10); 

    JLabel lblTotalDiscount = new JLabel("Total Discount"); 
    lblTotalDiscount.setFont(new Font("SansSerif", Font.BOLD, 12)); 
    lblTotalDiscount.setBounds(158, 496, 86, 16); 
    getContentPane().add(lblTotalDiscount); 

    setResizable(false); 
    setLocationRelativeTo(null); // THIS WILL CENTRE THE POSITION OF WINDOW 
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 


    public class btnPrintAction implements ActionListener, Printable{ 
    public int print(Graphics gx, PageFormat pf, int page) throws PrinterException { 

     if (page > 0){ 
      return NO_SUCH_PAGE; 
     } // Only one page 
     Graphics2D g = (Graphics2D)gx; // Cast to Graphics2D object 
     g.translate(pf.getImageableX(), pf.getImageableY()); // Match origins to imageable area 
     //g.drawString ("Hello world", 100, 100); // Print Hello World at offset (100, 100) 

     Graphics2D g2d = (Graphics2D)g; 
     g2d.translate(pf.getImageableX(), pf.getImageableY()); 

     // Print the entire visible contents of a 
     // java.awt.Frame. 
     getContentPane().printAll(g); 


     return PAGE_EXISTS; // Page exists (offsets start at zero!) 

    } 
    public void actionPerformed(ActionEvent e) { 

     PrinterJob job = PrinterJob.getPrinterJob(); // Get the printer's job list 
     job.setPrintable(this); // We print with this class (btnPrintAction, which implements Printable) 
     if (job.printDialog() == true) { // If we clicked OK in the print dialog 
      try { 
       job.print(); 
      } catch (PrinterException ex){ 
       // It did not work (PrinterException thrown), so add any error handling routines. 
       JOptionPane.showMessageDialog(null, ex.toString(), "Printing Error", 
         JOptionPane.WARNING_MESSAGE); 
      } 
     } 

    } 
} 

你有什麼想法如何實現這一點?我需要得到這個工作。非常感謝幫助。謝謝。

+0

分割你的用戶界面爲不同的區域(跨多個組件),您想要打印的組件和那些不這樣做,那麼只打印你想要打印的面板... – MadProgrammer

回答

2

定義用於打印的圖形的剪輯區域。

g.setClip(int x, int y, int width, int height) 

其中剪輯的參數是面板邊界。

+0

還有一個[setClip的那需要形狀](http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#setClip%28java.awt.Shape%29)如果排除按鈕將是非矩形。 – Radiodef

+0

@Stanislavl你能給我一些這樣的實例嗎? – Dunkey

+0

@Dunkey post SSCCE – StanislavL

2

一些解決方案,我能想到的:

  • 把按鈕放在不同的面板(容易,推薦)。
  • 在打印期間將setVisible(false)設置爲不可見,並在設置setVisible之後再次可見(記住setVisible不僅僅是滿足眼睛)。
  • 使用print/printAll/printChildren(不推薦)的覆蓋手動修改打印機制。