`
djsl6071
  • 浏览: 599856 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

[SWT] Print & Print Preview

阅读更多

转自: http://www.blogjava.net/Javawind/articles/132177.html

从SWT中建立"print dialog", 当call print()时弹出print dialog

public void print(){
        PrintDialog dialog 
= new PrintDialog(da.sShell, SWT.NONE);
        PrinterData data 
= dialog.open();
        
if (data == nullreturn;
        
if (data.printToFile) {
            data.fileName 
= "print.out"// you probably want to ask the user for a filename
        }

        printer 
= new Printer(data);
        print(printer);
        printer.dispose();
    }

紧接着建立print(Printer printer)
private void print(Printer printer) {
        
if (printer.startJob("Text")) {   // the string is the job name - shows up in the printer's job list
            Rectangle trim = printer.computeTrim(0000);
            
//Calculate the scale factor between the screen resolution and printer
            
// resolution in order to correctly size the image for the printer
            Point screenDPI = da.sShell.getDisplay().getDPI();
            Point printerDPI 
= printer.getDPI();
            
int scaleFactor = printerDPI.x / screenDPI.x;
            
//int width = image.getImageData().width;
            
//int height = image.getImageData().height;
            /**//* Create printer GC, and create and set the printer font */
            gc 
= new GC(printer); 
            gc.drawText("some text here", scaleFactor 
* 20, scaleFactor * 20);
            
            printer.endJob();
            gc.dispose();
        }

    }



完成"print" 之后添加"print preview"。只需要让"print" method 带入 2 个arguments:Device 和 GC
当打印时带入Printer device 和 包含打印信息的GC。当要预览时带入 Display device 和GC(
drawing on canvas),就是相当于把要打印的东西显示在canvans里。

create all colors, fonts, and images using the correct device,之后运行gc.dispose()

对于打印的字体的设置
gc.setFont(new Font())


对于打印的字体的设置可以参考:
org.eclipse.swt.custom.StyledText
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class SampleStyledText {
  Display display 
= new Display();
  Shell shell 
= new Shell(display);
  
  StyledText styledText;

  
public SampleStyledText() {
    init();
    
    shell.setLayout(
new GridLayout());
    
    styledText 
= new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    
    styledText.setLayoutData(
new GridData(GridData.FILL_BOTH));
    
    Font font 
= new Font(shell.getDisplay(), "Courier New"12, SWT.NORMAL);
    styledText.setFont(font);
    
    styledText.setText(
"123456789\r\nABCDEFGHI");
    
    StyleRange styleRange1 
= new StyleRange();
    styleRange1.start 
= 2;
    styleRange1.length 
= 16;
    styleRange1.foreground 
= shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
    styleRange1.background 
= shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
    styleRange1.fontStyle 
= SWT.BOLD;
    
    StyleRange styleRange2 
= new StyleRange();
    styleRange2.start 
= 14;
    styleRange2.length 
= 3;
    styleRange2.fontStyle 
= SWT.NORMAL;
    styleRange2.foreground 
= shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
    styleRange2.background 
= shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
    
//    styledText.setStyleRange(styleRange1);
//    styledText.setStyleRange(styleRange2);
    
    
//styledText.setStyleRanges(new StyleRange[]{styleRange1, styleRange2});
    
//styledText.setStyleRanges(new StyleRange[]{styleRange2, styleRange1});
    
    
//styledText.setLineBackground(1, 1, shell.getDisplay().getSystemColor(SWT.COLOR_GRAY));
    
//    styledText.setSelection(4);
//    System.out.println(printStyleRanges(styledText.getStyleRanges()) );
//    styledText.insert("000");
    
    
    System.out.println(printStyleRanges(styledText.getStyleRanges()) );
    
//    styledText.setStyleRanges(new StyleRange[]{styleRange1});
//    System.out.println(printStyleRanges(styledText.getStyleRanges()) );
    
    
//shell.pack();
    shell.setSize(300120);
    shell.open();
    
//textUser.forceFocus();

    
// Set up the event loop.
    while (!shell.isDisposed()) {
      
if (!display.readAndDispatch()) {
        
// If no more entries in event queue
        display.sleep();
      }

    }


    display.dispose();
  }

  
  
private String printStyleRanges(StyleRange[] styleRanges) {
    
    
if(styleRanges == null)
      
return "null";
    
else if(styleRanges.length == 0)
      
return "[]";
    
    StringBuffer sb 
= new StringBuffer();
    
for(int i=0; i<styleRanges.length; i++{
      sb.append(styleRanges[i] 
+ "\n");
    }

    
    
return sb.toString();
  }

  
public static void main(String[] args) {
    
new SampleStyledText();
  }

}





参考:

You will have to think about margins, page breaks, and scaling, and you
may want to think about whether or not the chosen fonts exist on the printer.
For an optimization, you might want to use a set of background images, one
per page, for the display drawing so that your user can flip between the 'pages'
without you having to render them again.

Printing is a lot of work. The widgets don't do it for you - you have to do
all the drawing yourself. Once you have done the work, however, it is a
relatively simple thing to add a "print preview" feature.

The closest thing in SWT to an example of this is the StyledText widget -
have a look at the code in org.eclipse.swt.custom.StyledText. It doesn't do
a print preview specifically, but it does know how to draw itself to the
screen, and to print itself to a printer. Print preview would just be a bit
of a twist on drawing to the screen.
 
分享到:
评论

相关推荐

    SWT&&JFace api

    SWT(Standard Widget Toolkit)和JFace是Eclipse RCP(Rich Client Platform)中的核心组件,用于构建桌面应用程序。这两个库提供了丰富的用户界面组件和高级的UI管理功能,使得开发者可以构建出美观、高效的图形...

    swt&jface&rcp.chm.rar

    SWT (Standard Widget Toolkit)、JFace 和 RCP (Rich Client Platform) 是 Java 开发桌面应用程序的重要工具包,尤其在...而 "swt&jface&rcp.chm" 文件则可以帮助开发者深入学习这些技术,掌握其精髓,提升开发技能。

    SWT&JFACE通信录项目

    《SWT&JFace通信录项目详解》 在IT领域,开发用户界面是软件工程中的重要环节,而Java提供了一系列工具和技术来支持这一过程。本文将深入探讨基于SWT(Standard Widget Toolkit)和JFace的通信录项目,这是一个集成...

    Eclipse SWT&JFace开发实战精解 源码

    本资源"《Eclipse SWT&JFace开发实战精解 源码》"包含四个主要部分,分别涵盖了基础概述、SWT组件、JFace组件增强以及实例应用,旨在帮助开发者深入理解和掌握这两项技术。 **第一部分:基础概述篇** 这部分内容...

    Eclipse+SWT&JFace;开发实战精解

    《Eclipse+SWT&JFace;开发实战精解》一书主要涵盖了使用Eclipse集成开发环境,结合SWT(Standard Widget Toolkit)和JFace库进行桌面应用开发的核心技术和实践方法。Eclipse是一个开放源码的Java IDE,而SWT和JFace...

    keygen SWT&Swing.Designer_Keygen

    SWT&Swing.Designer_Keygen

    初学SWT&JFace

    【初学SWT&JFace】—— 探索Java图形用户界面的新篇章 在Java编程领域,GUI(图形用户界面)开发是构建交互式应用程序的关键部分。SWT(Standard Widget Toolkit)与JFace是两个非常重要的库,它们为Java开发者提供...

    swt&jface api

    在"swt&jface.chm"这个帮助文档中,你可能会找到以下关键知识点: 1. SWT组件:详细介绍了SWT提供的各种控件,如Button、Label、Text、Table、Tree等,以及如何创建、配置和操作这些控件。 2. 布局管理:讲解了...

    SWT&JFace必备jar包

    它们是swt-awt-win32-3305.dll、swt-gdip- win32-3305.dll、swt-wgl-win32-3305.dll、swt-win32-3305.dll和swt.jar。在发布时,要将 4个dll文件放到path路径中,或者使用-Djava.library.path设置相应的路径。将swt....

    swt&jface英文教程

    《The Definitive Guide to SWT and JFace》是由Robert Harris和Rob Warner合著的一本权威指南,专注于介绍SWT(Standard Widget Toolkit)和JFace这两个关键的Java GUI库。本书旨在帮助开发者深入理解和有效地利用...

    SWT&jface;开发jar包很全的

    开发jar包很全的"压缩包包含的swt&jface.jar文件,应该包含了所有必要的类库,可以支持开发者快速构建功能丰富的跨平台应用程序。通过导入这个jar包,开发者可以在项目中直接使用SWT和JFace的功能,无需关注底层实现...

    SWT & JFace_最新最全_API

    SWT (Standard Widget Toolkit) 和 JFace 是两个在Java GUI编程中非常重要的库,主要用于创建桌面应用程序。它们是由Eclipse项目开发并维护的,旨在提供一个与平台无关的图形用户界面(GUI)工具包,与Java Swing...

    Eclipse SWT&JFace开发实战精解 源代码

    在《Eclipse SWT&JFace开发实战精解》这本书中,读者可以期待学习以下关键知识点: 1. **SWT组件**:涵盖所有基本和高级的SWT组件,包括按钮、文本输入、表格、树形视图、滚动条等,以及如何自定义这些组件。 2. *...

    SWT&Swing.Designer_Window.Builder.Pro_v6_for_Eclipse3.2_Keygen

    SWT&Swing.Designer_Window.Builder.Pro_v6_for_Eclipse3.2_Keygen,是eclipse的插件,开发swt的工具

    Swt&JFace In Action(中英文对照)

    《Swt&JFace In Action》是一本深入探讨Swt和JFace技术的专业书籍,提供了中英文双语对照,方便读者理解和学习。Swt(Standard Widget Toolkit)是Eclipse项目的一部分,它为Java应用程序提供了与操作系统图形用户...

    swt.jar 32&64

    SWT (Standard Widget Toolkit) 是一个开放源代码的Java库,用于构建图形用户界面(GUI)。它是Eclipse项目的一部分,提供了与操作系统更紧密集成的窗口小部件,与Java的默认GUI库AWT和Swing相比,它能提供更加原生...

    SWT&JFACE GEF or Eclipse Platform

    SWT(Standard Widget Toolkit)与JFace是Eclipse平台中用于构建图形用户界面(GUI)的重要工具包。它们提供了一种高效且可移植的方式来创建Java应用程序的用户界面,弥补了Java原生GUI库如AWT和Swing在界面效果和...

    swt.jar32&64

    SWT (Standard Widget Toolkit) 是一个开放源代码的Java库,用于构建图形用户界面(GUI)。它是Eclipse项目的一部分,提供了与操作系统更紧密集成的GUI组件,与Java的默认GUI库AWT和Swing相比,它能提供更好的性能和...

Global site tag (gtag.js) - Google Analytics