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);
}
}
按照你的方法改了,但是打印机没有动静,还是打印不了呀.
展开
 我来答
方杭尽
2010-01-13 · TA获得超过1万个赞
知道大有可为答主
回答量:1296
采纳率:0%
帮助的人:645万
展开全部
备份你的答游山源文件磨皮,执行以下代码,看看有没问题,我这里没有打印机没法测试:

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);
}
}
daay1986
2010-01-13 · TA获得超过6018个赞
知道大有可为答主
回答量:2208
采纳率:0%
帮助的人:1470万
展开全部
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;
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式