gpt4 book ai didi

java - SWT - AWT 桥

转载 作者:行者123 更新时间:2023-12-01 15:02:22 31 4
gpt4 key购买 nike

主 GUI 基于 SWT。我正在通过单击按钮从 printPDF 类运行打印操作。

 public void startPDFPrint() throws Exception {
Display.getCurrent().syncExec(
new Runnable() {
public void run(){
try {
new AplotPdfPrintLocal().printPDF("c:\\temp\\file.pdf", "PDF Print Job");
}
catch (IOException e) {
e.printStackTrace();
}
catch (PrinterException e) {
e.printStackTrace();
}
}
});
}

printPDF 类没有任何组件或 GUI。它基本上只是创建一个运行打印作业。

public class PDFPrintPage implements Printable {

类中仅有的两个方法

 public void printFile(String filename) throws IOException { (setups the print)

public int print(Graphics g, PageFormat format, int index)
throws PrinterException {

在 printFile 方法中,有一行代码用于打开本地打印机对话框

 pjob.printDialog()

该对话框基于 AWT。

如何打开此对话框,以便我的用户可以选择打印机和份数?

我已经阅读了 SWT_AWT 桥文档,看起来您需要将 AWT 嵌入到 SWT 组件中,但我的类没有任何组件。

我需要创建一个组件方法并在组件中运行 printFile 代码吗?

我知道如果我能弄清楚这篇文章,它也将有助于解决我遇到的所有其他问题。

编辑

请查看我的代码并告诉我哪里有问题。它符合并运行,但我在对话框行收到 SWT 线程异常。

 public class PDFPrintPage extends ApplicationWindow{

private String fileURL;
private PageFormat pfDefault;
private PrinterJob pjob;
private PDFFile pdfFile;

public PDFPrintPage(Shell parent, String inputFileName) {
super(parent);
this.fileURL = inputFileName;
}

public void run() {
setBlockOnOpen(true);
open();
Display.getCurrent().dispose();
}

protected Control createContents(Composite parent) {
final Composite swtAwtComponent = new Composite(parent, SWT.EMBEDDED);
final java.awt.Frame frame = SWT_AWT.new_Frame( swtAwtComponent );
final javax.swing.JPanel panel = new javax.swing.JPanel( );
frame.add(panel);
JButton swingButton = new JButton("Print");
panel.add(swingButton);
swingButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent actionevent) {
try {
printFile(fileURL, frame);
}
catch (IOException e) {
e.printStackTrace();
}
}
});
return swtAwtComponent;
}

public void printFile(String filename, Frame panel) throws IOException {
File file = new File(filename);
FileInputStream fis = new FileInputStream(file);
FileChannel fc = fis.getChannel();
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
pdfFile = new PDFFile(bb); // Create PDF Print Page

final PrintPage pages = new PrintPage(pdfFile);

pjob = PrinterJob.getPrinterJob();
pfDefault = PrinterJob.getPrinterJob().defaultPage();
Paper defaultPaper = new Paper();
defaultPaper.setImageableArea(0, 0, defaultPaper.getWidth(), defaultPaper.getHeight());
pfDefault.setPaper(defaultPaper);
pjob.setJobName(file.getName());

final Dialog awtDialog = new Dialog(panel);
Shell parent = getParentShell();
Shell shell = new Shell(parent, SWT.APPLICATION_MODAL | SWT.NO_TRIM);
shell.setSize(100, 100);
shell.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
awtDialog.requestFocus();
awtDialog.toFront();
}
});
//if (pjob.printDialog()) {
pfDefault = pjob.validatePage(pfDefault);
Book book = new Book();
book.append(pages, pfDefault, pdfFile.getNumPages());
pjob.setPageable(book);
try {
pjob.print();
}
catch (PrinterException exc) {
System.out.println(exc);
}
//}
}

class PrintPage implements Printable {

private PDFFile file;

PrintPage(PDFFile file) {
this.file = file;
}

public int print(Graphics g, PageFormat format, int index) throws PrinterException {
int pagenum = index + 1;
if ((pagenum >= 1) && (pagenum <= file.getNumPages())) {
Graphics2D g2 = (Graphics2D) g;
PDFPage page = file.getPage(pagenum);
Rectangle imageArea = new Rectangle((int) format.getImageableX(), (int) format.getImageableY(),
(int) format.getImageableWidth(), (int) format.getImageableHeight());
g2.translate(0, 0);
PDFRenderer pgs = new PDFRenderer(page, g2, imageArea, null, null);
try {
page.waitForFinish();
pgs.run();
} catch (InterruptedException ie) {

}
return PAGE_EXISTS;
}
else {
return NO_SUCH_PAGE;
}
}
}//End PrintPage Class
}//End PDFPrintPage Class

我可能将您的建议代码添加到完全错误的位置。我的想法是在 focusGained(FocusEvent e) 方法中的何处添加 printDialog 调用。

最佳答案

当您打开打印机对话框时,您需要打开一个大小为零的 shell,这样它看起来就像您的主 SWT Shell 处于非 Activity 状态,而 Swing 模式对话框位于其上方。同样,当您关闭 swing 对话框时,您需要关闭零大小的 Shell。

 java.awt.Dialog awtDialog = ...        
Shell shell = new Shell(parent, SWT.APPLICATION_MODAL | SWT.NO_TRIM);
shell.setSize(0, 0);
shell.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
awtDialog.requestFocus();
awtDialog.toFront();
}
});

引用: http://www.eclipse.org/articles/article.php?file=Article-Swing-SWT-Integration/index.html#sec-event-threads

关于java - SWT - AWT 桥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13458486/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com