Java 如何删除打印页中的页边距

Java 如何删除打印页中的页边距,java,swing,printing,Java,Swing,Printing,我在打印面板上遇到问题。在我打印的页面中,内容的两侧都有多余的边距,并且内容区域太小,无法包含该面板中的所有文本。所以我的结果页面是 我的代码在这里 package UI; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import

我在打印面板上遇到问题。在我打印的页面中,内容的两侧都有多余的边距,并且内容区域太小,无法包含该面板中的所有文本。所以我的结果页面是

我的代码在这里

package UI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.text.SimpleDateFormat;
import java.util.Date;
 import java.util.Dictionary;
 import javax.print.PrintService;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

public class printService  extends JFrame implements Printable {
JFrame f = new JFrame("Printing");
JPanel p3 = new JPanel(new GridBagLayout());
JPanel main = new JPanel();

JLabel title = new JLabel("Ni Lar Aung");
JLabel subtitle = new JLabel("Mobile Sale & Service");
JLabel lblNo = new JLabel("No");
JLabel lblType = new JLabel("Type");
JLabel lblAmt = new JLabel("Service Amt");
JLabel lblDes = new JLabel("Description");
JLabel lblTot = new JLabel("Total");

public printService(DefaultTableModel tm, int rowcount,int dis ) {
    Container c=getContentPane();
    c.setLayout(new BorderLayout());
    p3.setLayout(new GridBagLayout());

    p3.setBackground(Color.white);

    GridBagConstraints con = new GridBagConstraints();
    con.anchor = GridBagConstraints.NORTH;
    con.insets = new Insets(5, 5, 5, 5);
    con.fill = GridBagConstraints.NONE;



    con.gridx = 1;
    con.gridy = 0;
    con.gridwidth = 2;
    title.setFont(new Font("Times New Roman", Font.BOLD,13));
    p3.add(title, con);

    con.gridx = 1;
    con.gridy = 1;
    con.gridwidth = 2;
    subtitle.setFont(new Font("Times New Roman", Font.BOLD,11));
    p3.add(subtitle, con);

    con.gridx = 3;
    con.gridy = 2;
    con.gridwidth = 2;
    Date d = new Date();
    SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
    JLabel lblDate = new JLabel(simple.format(d));
    lblDate.setFont(new Font("Times New Roman", Font.PLAIN,9));
    p3.add(lblDate, con);

    con.gridx = 0;
    con.gridy = 3;
    con.weightx = 0.5;
    con.gridwidth =1;
    lblNo.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(lblNo, con);

    con.gridx = 1;
    con.gridy = 3;
    con.weightx = 0.5;
    con.gridwidth =1;
    lblType.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(lblType, con);

    con.gridx = 2;
    con.gridy = 3;
    con.weightx = 0.5;
    con.gridwidth =1;
    lblAmt.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(lblAmt, con);

    /*con.gridx = 3;
    con.gridy = 3;
    con.weightx = 0.5;
    con.gridwidth =1;
    lblDes.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(lblDes, con);*/

    con.gridx = 3;
    con.gridy = 3;
    con.weightx = 0.5;
    con.gridwidth =1;
    p3.add(lblTot, con);
    lblTot.setFont(new Font("Times New Roman", Font.PLAIN,8));
    int totalcost = 0;
    int row = 4;
    int quantity = 1;
    int price = 1000;
    int discout = dis;
    String stock = "stockName";
    System.out.println(rowcount+"rowcount");
    System.out.println(tm.getRowCount());
    System.out.println(tm.getColumnCount());
    //System.out.println(Service.dt.getValueAt(0, 0)+"hhhh");
    for (int i = 0; i < rowcount; i++) {
        con.gridx = 0;
        con.gridy = row;
        con.weightx = 0.5;
        con.gridwidth =1;
        JLabel lblNo = new JLabel(i + "");
        lblNo.setFont(new Font("Times New Roman", Font.PLAIN,8));
        p3.add(lblNo, con);

        con.gridx = 1;
        con.gridy = row;
        con.weightx = 0.5;
        con.gridwidth =1;
        JLabel val1 = new JLabel(tm.getValueAt(i, 1).toString());
        val1.setFont(new Font("Times New Roman", Font.PLAIN,8));
        p3.add(val1, con);
        System.out.println(tm.getValueAt(i, 1)+"llll");


        con.gridx = 2;
        con.gridy = row;
        con.weightx = 0.5;
        con.gridwidth =1;
        JLabel val2 = new JLabel(tm.getValueAt(i, 2).toString());
        val2.setFont(new Font("Times New Roman", Font.PLAIN,8));
        p3.add(val2, con);
        System.out.println(tm.getValueAt(i, 2));

        /*con.gridx = 3;
        con.gridy = row;
        con.weightx = 0.5;
        con.gridwidth =1;
        JLabel val3 = new JLabel(tm.getValueAt(i, 3).toString());
        val3.setFont(new Font("Times New Roman", Font.PLAIN,8));
        p3.add(val3, con);*/

        con.gridx = 3;
        con.gridy = row;
        con.weightx = 0.5;
        con.gridwidth =1;
        JLabel val4 = new JLabel(tm.getValueAt(i, 4).toString());
        val4.setFont(new Font("Times New Roman", Font.PLAIN,8));
        p3.add(val4, con);

        totalcost += Integer.parseInt(tm.getValueAt(i, 4).toString());
        row++;
    }

    con.gridy = row;
    con.gridx = 3;
    p3.add(new JLabel("-------"), con);
    row++;

    con.gridy = row;
    con.gridx = 1;
    JLabel totAmt = new JLabel("Total Amount");
    totAmt.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(totAmt, con);

    con.gridy = row;
    con.gridx = 3;
    JLabel total = new JLabel(totalcost + "");
    total.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(total, con);
    row++;

    con.gridy = row;
    con.gridx = 1;
    JLabel lblDisAmt = new JLabel("Discount Allow");
    lblDisAmt.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(lblDisAmt, con);

    con.gridy = row;
    con.gridx = 3;
    JLabel Dis = new JLabel(discout + ""); 
    Dis.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(Dis, con);
    row++;

    con.gridy = row;
    con.gridx = 3;
    p3.add(new JLabel("-------"), con);
    row++;

    con.gridy = row;
    con.gridx = 1;
    JLabel netAmt = new JLabel("Net Amount");
    netAmt.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(netAmt, con);

    con.gridy = row;
    con.gridx = 3;
    JLabel result = new JLabel((totalcost - discout) + ""); 
    result.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(result, con);
    row++;

    con.gridy = row;
    con.gridx = 1;
    con.gridwidth = 3;
    JLabel thanks = new JLabel("Thank You");
    thanks.setFont(new Font("Times New Roman", Font.BOLD, 9));
    p3.add(thanks,con);


    final JButton but=new JButton("print");
    con.gridy = row;
    con.gridx = 4;
    p3.add(but, con);

    row++;
    c.setSize(300, 400);
    c.setVisible(true);
    setVisible(true);
    setSize(300,200);
    c.add(main);
    main.setVisible(true);



    GridBagConstraints m = new GridBagConstraints();
    m.anchor = GridBagConstraints.NORTH;
    m.insets = new Insets(5,5,5,5);
    m.fill = GridBagConstraints.BOTH;

    m.gridy = 0;
    m.gridx = 0;
    main.add(p3,m);
    main.setBackground(Color.yellow);
    p3.setVisible(true);
but.setVisible(false);
/*  but.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    but.setVisible(false);
    printForm();
  }

      });*/
    printForm();
}

   public void printForm() {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
PrintUtility.getPrinterServiceNameList();
PrintService service = PrintUtility
        .findPrintService("XP-80C");
System.out.println("default printer selection");

try {
    job.printDialog();
    System.out.println("within try block");
    job.setPrintService(service);
    job.print();
} catch (PrinterException ex) {
    System.out.println("within catch");
  }
   }


@Override
public int print(Graphics g, PageFormat pf, int page)
        throws PrinterException {
    // TODO Auto-generated method stub
    System.out.println("ddddddddd");
    if (page > 0)
        return NO_SUCH_PAGE;
    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());
    p3.printAll(g);
    return PAGE_EXISTS;

}

}
packageui;
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Container;
导入java.awt.Dimension;
导入java.awt.Font;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Insets;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.print.PageFormat;
导入java.awt.print.Printable;
导入java.awt.print.PrinterException;
导入java.awt.print.PrinterJob;
导入java.text.simpleDataFormat;
导入java.util.Date;
导入java.util.Dictionary;
导入javax.print.PrintService;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.table.DefaultTableModel;
公共类printService扩展JFrame实现可打印{
JFrame f=新JFrame(“打印”);
JPanel p3=新的JPanel(新的GridBagLayout());
JPanel main=新的JPanel();
JLabel title=新JLabel(“尼拉昂”);
JLabel subtitle=新的JLabel(“移动销售与服务”);
JLabel lblNo=新JLabel(“否”);
JLabel lblType=新的JLabel(“类型”);
JLabel lblAmt=新JLabel(“服务金额”);
JLabel lblDes=新JLabel(“说明”);
JLabel lblTot=新JLabel(“总计”);
公共打印服务(DefaultTableModelTM、整数行数、整数dis){
容器c=getContentPane();
c、 setLayout(新的BorderLayout());
p3.setLayout(新的GridBagLayout());
p3.挫折背景(颜色:白色);
GridBagConstraints con=新的GridBagConstraints();
con.anchor=gridbag.NORTH;
con.插图=新插图(5,5,5,5);
con.fill=GridBagConstraints.NONE;
con.gridx=1;
con.gridy=0;
con.gridwidth=2;
title.setFont(新字体(“Times new Roman”,字体粗体,13));
p3.增加(标题、合同);
con.gridx=1;
con.gridy=1;
con.gridwidth=2;
副标题.setFont(新字体(“Times new Roman”,字体粗体,11));
p3.添加(副标题,con);
con.gridx=3;
con.gridy=2;
con.gridwidth=2;
日期d=新日期();
SimpleDataFormat simple=新的SimpleDataFormat(“yyyy-MM-dd”);
JLabel lblDate=新的JLabel(simple.format(d));
setFont(新字体(“Times new Roman”,字体.PLAIN,9));
p3.添加(lblDate,con);
con.gridx=0;
con.gridy=3;
con.weightx=0.5;
con.gridwidth=1;
setFont(新字体(“Times new Roman”,字体.PLAIN,8));
p3.添加(lblNo,con);
con.gridx=1;
con.gridy=3;
con.weightx=0.5;
con.gridwidth=1;
setFont(新字体(“Times new Roman”,字体.PLAIN,8));
p3.添加(LBL类型,con);
con.gridx=2;
con.gridy=3;
con.weightx=0.5;
con.gridwidth=1;
lblAmt.setFont(新字体(“Times new Roman”,普通字体,8));
p3.添加(lblAmt,con);
/*con.gridx=3;
con.gridy=3;
con.weightx=0.5;
con.gridwidth=1;
setFont(新字体(“Times new Roman”,普通字体,8));
p3.添加(lblDes,con)*/
con.gridx=3;
con.gridy=3;
con.weightx=0.5;
con.gridwidth=1;
p3.添加(lblTot,con);
setFont(新字体(“Times new Roman”,字体.PLAIN,8));
整数总成本=0;
int行=4;
整数数量=1;
国际价格=1000;
int折扣=dis;
字符串stock=“stockName”;
System.out.println(rowcount+“rowcount”);
System.out.println(tm.getRowCount());
System.out.println(tm.getColumnCount());
//System.out.println(Service.dt.getValueAt(0,0)+“hhhh”);
对于(int i=0;i Rectangle bounds = p3.getBounds();
 p3.setBounds(0, 0, (int)pf.getWidth(), (int)pf.getHeight());
 p3.printAll(g);
 p3.setBounds(bounds);