Java Swing打印问题
郁闷死了,JAVA打印整了我半个月了,我实在是快被气死了,请各位高人帮我指点下我这里代码,问题:我只想打印窗口里面的一个容器panel和容器里面的东西,窗口标题呀、窗口边...
郁闷死了,JAVA打印整了我半个月了,我实在是快被气死了,请各位高人帮我指点下我这里代码,问题:我只想打印窗口里面的一个容器panel和容器里面的东西,窗口标题呀、窗口边框呀我是不想打印的。
package test.printTest;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.PrintJob;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Properties;
public class Print01{
private static final long serialVersionUID = -5124405479024666344L;
private JPanel contentPane;
private JTable table;
private JFrame f;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Print01 fr = new Print01();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Print01() {
f=new JFrame();
f.setTitle("\u6253\u5370\u6D4B\u8BD5\u7A97\u53E3");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
f.setContentPane(contentPane);
final JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel label = new JLabel("New label");
label.setBounds(330, 11, 73, 15);
panel.add(label);
JButton button = new JButton("New button");
button.setBounds(144, 7, 138, 23);
panel.add(button);
JButton button_3 = new JButton("\u6253\u5370");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Toolkit kit = Toolkit.getDefaultToolkit();
Properties props = new Properties();
props.put("awt.print.printer", "durango");
props.put("awt.print.numCopies", "2");
if (kit != null) {
PrintJob printJob = kit.getPrintJob(f, "Print Frame", props);
if (printJob != null) {
Graphics pg = panel.getGraphics();//我只想打印里面的panel,不想打印整个窗口。
if (pg != null) {
try {
f.printAll(pg);
} finally {
pg.dispose();
}
}
printJob.end();
}
}
}
});
button_3.setBounds(340, 36, 93, 39);
panel.add(button_3);
JButton button_2 = new JButton("New button");
button_2.setBounds(21, 2, 93, 30);
panel.add(button_2);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(33, 61, 285, 168);
panel.add(scrollPane);
table = new JTable(9,5);
scrollPane.setViewportView(table);
f.setVisible(true);
}
}
按照你的方法改了,但是打印机没有动静,还是打印不了呀. 展开
package test.printTest;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.PrintJob;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Properties;
public class Print01{
private static final long serialVersionUID = -5124405479024666344L;
private JPanel contentPane;
private JTable table;
private JFrame f;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Print01 fr = new Print01();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Print01() {
f=new JFrame();
f.setTitle("\u6253\u5370\u6D4B\u8BD5\u7A97\u53E3");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
f.setContentPane(contentPane);
final JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel label = new JLabel("New label");
label.setBounds(330, 11, 73, 15);
panel.add(label);
JButton button = new JButton("New button");
button.setBounds(144, 7, 138, 23);
panel.add(button);
JButton button_3 = new JButton("\u6253\u5370");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Toolkit kit = Toolkit.getDefaultToolkit();
Properties props = new Properties();
props.put("awt.print.printer", "durango");
props.put("awt.print.numCopies", "2");
if (kit != null) {
PrintJob printJob = kit.getPrintJob(f, "Print Frame", props);
if (printJob != null) {
Graphics pg = panel.getGraphics();//我只想打印里面的panel,不想打印整个窗口。
if (pg != null) {
try {
f.printAll(pg);
} finally {
pg.dispose();
}
}
printJob.end();
}
}
}
});
button_3.setBounds(340, 36, 93, 39);
panel.add(button_3);
JButton button_2 = new JButton("New button");
button_2.setBounds(21, 2, 93, 30);
panel.add(button_2);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(33, 61, 285, 168);
panel.add(scrollPane);
table = new JTable(9,5);
scrollPane.setViewportView(table);
f.setVisible(true);
}
}
按照你的方法改了,但是打印机没有动静,还是打印不了呀. 展开
2个回答
展开全部
备份你的答游山源文件磨皮,执行以下代码,看看有没问题,我这里没有打印机没法测试:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Toolkit;
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.util.Properties;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
public class Print01{
private static final long serialVersionUID = -5124405479024666344L;
private JPanel contentPane;
private JTable table;
private JFrame f;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Print01 fr = new Print01();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Print01() {
f=new JFrame();
f.setTitle("\u6253\u5370\u6D4B\u8BD5\u7A97\u53E3");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
f.setContentPane(contentPane);
final JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel label = new JLabel("New label");
label.setBounds(330, 11, 73, 15);
panel.add(label);
JButton button = new JButton("New button"清中);
button.setBounds(144, 7, 138, 23);
panel.add(button);
JButton button_3 = new JButton("\u6253\u5370");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Toolkit kit = Toolkit.getDefaultToolkit();
// Properties props = new Properties();
// props.put("awt.print.printer", "durango");
// props.put("awt.print.numCopies", "2");
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = printJob.defaultPage(); //获取默认打印页面格式
Printable printable=new Printable(){
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if(pageIndex==1)return Printable.NO_SUCH_PAGE;
panel.printAll(graphics);
return Printable.PAGE_EXISTS;
}};
printJob.setPrintable(printable, pageFormat); //设置打印工作
try {
printJob.print();
} catch (PrinterException e1) {
System.out.println("错误:"+e1);
}
}
});
button_3.setBounds(340, 36, 93, 39);
panel.add(button_3);
JButton button_2 = new JButton("New button");
button_2.setBounds(21, 2, 93, 30);
panel.add(button_2);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(33, 61, 285, 168);
panel.add(scrollPane);
table = new JTable(9,5);
scrollPane.setViewportView(table);
f.setVisible(true);
}
}
监听器里改成如下就会有打印对话框,我把默认打印页数变成了1,可能还有很多问题,你自己根据实际情况多看看,
参考我的博客:http://liuguojie456.blog.163.com/blog/static/12639549200941134025167/
public void actionPerformed(ActionEvent e) {
// Toolkit kit = Toolkit.getDefaultToolkit();
// Properties props = new Properties();
// props.put("awt.print.printer", "durango");
// props.put("awt.print.numCopies", "2");
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = printJob.defaultPage(); //获取默认打印页面格式
Printable printable=new Printable(){
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if(pageIndex==1)return Printable.NO_SUCH_PAGE;
panel.printAll(graphics);
return Printable.PAGE_EXISTS;
}};
printJob.setPrintable(printable, pageFormat); //设置打印工作
try {
if(printJob.printDialog()){
printJob.print();
}
} catch (PrinterException e1) {
JOptionPane.showMessageDialog(null, "错误:"+e1);
}
}
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Toolkit;
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.util.Properties;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
public class Print01{
private static final long serialVersionUID = -5124405479024666344L;
private JPanel contentPane;
private JTable table;
private JFrame f;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Print01 fr = new Print01();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Print01() {
f=new JFrame();
f.setTitle("\u6253\u5370\u6D4B\u8BD5\u7A97\u53E3");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
f.setContentPane(contentPane);
final JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel label = new JLabel("New label");
label.setBounds(330, 11, 73, 15);
panel.add(label);
JButton button = new JButton("New button"清中);
button.setBounds(144, 7, 138, 23);
panel.add(button);
JButton button_3 = new JButton("\u6253\u5370");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Toolkit kit = Toolkit.getDefaultToolkit();
// Properties props = new Properties();
// props.put("awt.print.printer", "durango");
// props.put("awt.print.numCopies", "2");
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = printJob.defaultPage(); //获取默认打印页面格式
Printable printable=new Printable(){
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if(pageIndex==1)return Printable.NO_SUCH_PAGE;
panel.printAll(graphics);
return Printable.PAGE_EXISTS;
}};
printJob.setPrintable(printable, pageFormat); //设置打印工作
try {
printJob.print();
} catch (PrinterException e1) {
System.out.println("错误:"+e1);
}
}
});
button_3.setBounds(340, 36, 93, 39);
panel.add(button_3);
JButton button_2 = new JButton("New button");
button_2.setBounds(21, 2, 93, 30);
panel.add(button_2);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(33, 61, 285, 168);
panel.add(scrollPane);
table = new JTable(9,5);
scrollPane.setViewportView(table);
f.setVisible(true);
}
}
监听器里改成如下就会有打印对话框,我把默认打印页数变成了1,可能还有很多问题,你自己根据实际情况多看看,
参考我的博客:http://liuguojie456.blog.163.com/blog/static/12639549200941134025167/
public void actionPerformed(ActionEvent e) {
// Toolkit kit = Toolkit.getDefaultToolkit();
// Properties props = new Properties();
// props.put("awt.print.printer", "durango");
// props.put("awt.print.numCopies", "2");
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = printJob.defaultPage(); //获取默认打印页面格式
Printable printable=new Printable(){
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if(pageIndex==1)return Printable.NO_SUCH_PAGE;
panel.printAll(graphics);
return Printable.PAGE_EXISTS;
}};
printJob.setPrintable(printable, pageFormat); //设置打印工作
try {
if(printJob.printDialog()){
printJob.print();
}
} catch (PrinterException e1) {
JOptionPane.showMessageDialog(null, "错误:"+e1);
}
}
展开全部
public class En implements Printable {
private static final long serialVersionUID = -5124405479024666344L;
private JPanel contentPane;
private JTable table;
private JFrame f;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
En fr = new En();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public En() {
f = new JFrame();
f.setTitle("\u6253\u5370\u6D4B\u8BD5\u7A97\u53E3");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
f.setContentPane(contentPane);
final JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel label = new JLabel("New label");
label.setBounds(330, 11, 73, 15);
panel.add(label);
JButton button = new JButton("New button");
button.setBounds(144, 7, 138, 23);
panel.add(button);
JButton button_3 = new JButton("\u6253\u5370");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Toolkit kit = Toolkit.getDefaultToolkit();
if (kit != null) {
PrinterJob printerJob = PrinterJob.getPrinterJob();
Book book = new Book();
book.append(En.this, new PageFormat());
printerJob.setPageable(book);
try {
printerJob.printDialog();
printerJob.print();
} catch (PrinterException ex) {
Logger.getLogger(En.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
button_3.setBounds(340, 36, 93, 39);
panel.add(button_3);
JButton button_2 = new JButton("New button");
button_2.setBounds(21, 2, 93, 30);
panel.add(button_2);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(33, 61, 285, 168);
panel.add(scrollPane);
table = new JTable(9, 5);
scrollPane.setViewportView(table);
f.setVisible(true);
}
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
f.paint(graphics);
return Printable.PAGE_EXISTS;
}
}
private static final long serialVersionUID = -5124405479024666344L;
private JPanel contentPane;
private JTable table;
private JFrame f;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
En fr = new En();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public En() {
f = new JFrame();
f.setTitle("\u6253\u5370\u6D4B\u8BD5\u7A97\u53E3");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
f.setContentPane(contentPane);
final JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel label = new JLabel("New label");
label.setBounds(330, 11, 73, 15);
panel.add(label);
JButton button = new JButton("New button");
button.setBounds(144, 7, 138, 23);
panel.add(button);
JButton button_3 = new JButton("\u6253\u5370");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Toolkit kit = Toolkit.getDefaultToolkit();
if (kit != null) {
PrinterJob printerJob = PrinterJob.getPrinterJob();
Book book = new Book();
book.append(En.this, new PageFormat());
printerJob.setPageable(book);
try {
printerJob.printDialog();
printerJob.print();
} catch (PrinterException ex) {
Logger.getLogger(En.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
button_3.setBounds(340, 36, 93, 39);
panel.add(button_3);
JButton button_2 = new JButton("New button");
button_2.setBounds(21, 2, 93, 30);
panel.add(button_2);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(33, 61, 285, 168);
panel.add(scrollPane);
table = new JTable(9, 5);
scrollPane.setViewportView(table);
f.setVisible(true);
}
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
f.paint(graphics);
return Printable.PAGE_EXISTS;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询