扣丁学堂Java在线教程之SWT(JFace) 体验之FontRegistry

合集下载

swt教程

swt教程

一、开发环境搭建JDK安装、Eclipse环境搭建、新建工程、配置路径、配置服务器等二、JAVA基础培训2.1、面向对象简介(抽象、封装、继承、多态,与函数型语言的区别)2.2、基本类型、运算符、分支结构、循环结构简介详见:考核案例一2.3、集合框架(Map、List、Iterator等常用集合讲解)2.4、文件读取及IO(多种输入输出流及文件基本操作介绍:InputStream、OutputStream、RandomAccessFile等)2.5、异常处理详见:考核案例二2.6、XML文件读取详见:考核案例三2.7、多线程(synchronized关键字、锁机制、锁范围等介绍)2.8、数据操作(JDBC)详见:考核案例四三、J2EE框架培训3.1、基础环境搭建(公司框架导入及运行)3.2、J2EE应用综述(J2EE与传统应用的区别、运行环境、主流框架简介等)3.3、Spring开发流程及核心模块讲解(AOP、IoC、Bean等,及开发示例)3.4、dwr开发流程及核心模块讲解(配置文件、action调用等,及开发示例)3.5、Hibernate开发流程及核心模块讲解(映射文件、HQL、注释等、及开发示例)3.6、SSH集成详见:考核案例五四、ArcGis培训4.1、HTMl与JavaScript(4课时)Html+CSS语法介绍JavaScript 语法与对象jquery 语法介绍jquery UI 语法介绍详见:考核案例六4.2、arcgis后台培训(4课时)ArcMap操作和常用工具、数据源介绍(文件、fdb、sde)坐标系、图层管理、配图样式ArcGis Server 使用与地图服务详见:考核案例七4.3、arcgis for JavaScript(8课时)1、arcgis for JavaScript 开发包与环境搭建2、创建地图(约2分钟)3、引入静态/动态/影像服务。

4、切换底图服务。

第7章 SWT控件详解

第7章  SWT控件详解

7.4.4
标签的妙用
在这个例子中,还看到标签的另外一种用法,就是把 标签部件当作分隔线使用。要把标签变成分隔线只要构造的 时候赋予 SWT.SEPARATOR样式、以及分隔线的类型。例 如: new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL); //这是横向分隔线; new Label(this, SWT.SEPARATOR | SWT.VERTICAL); //这是竖线。
public boolean getGender(){ if(radioMale.getSelection()){ //获得单选框的选择状态,进行判断 ,如果被选择,则返回true return true; } if(radioFemale.getSelection()){ return false; } return true; }
第7章
SWT控件详解
上一章学习了如何用SWT小部件制作GUI界面,这一 章则详细讲解一些主要部件的使用。SWT的部件非常丰富 ,要在有限的篇幅中详细讲解每一个部件是非常困难的。而 且Eclipse的帮助系统提供了详细的部件API参考手册,所 以本书也尽量不重复参考手册中的内容,而是通过一些在开 发中经常遇到的应用实例,对常用的部件进行讲解。另外, SWT中,很多部件的使用都是大同小异的,学习过一些常 用的部件,其他部件也很容易理解。
7.4.3
设置字体的颜色
设置字体的颜色用 setForeground(Color color)方法, 设置背景颜色用setBackground(Color color)方法。在前面 介绍SWT部件的继承关系时候,可以看到,Label等等部件 都是继承自Control的,而setForeground和 setBackground都是Control中就实现的方法,所以所有继 承自Control的部件都有这两个方法设置字体的颜色和背景颜 色。 颜色的定义用的是Color对象。在后面关于SWT绘图的 内容中会详细说明Color的使用。一般的应用可以不需要自己 构造Color对象。在SWT中,已经预先定义了一批系统颜色 。正如在例子中那样。用 Display.getCurrent().getSystemColor(int color)这个静态 方法就可以获得预定义的Color对象。getSystemColor的参 数来自SWT中预定义的常量: COLOR_BLACK、。。。。。参考手册

Java程序设计之swt教程(2021年整理精品文档)

Java程序设计之swt教程(2021年整理精品文档)

(完整word版)Java程序设计之swt教程编辑整理:尊敬的读者朋友们:这里是精品文档编辑中心,本文档内容是由我和我的同事精心编辑整理后发布的,发布之前我们对文中内容进行仔细校对,但是难免会有疏漏的地方,但是任然希望((完整word版)Java程序设计之swt教程)的内容能够给您的工作和学习带来便利。

同时也真诚的希望收到您的建议和反馈,这将是我们进步的源泉,前进的动力。

本文可编辑可修改,如果觉得对您有帮助请收藏以便随时查阅,最后祝您生活愉快业绩进步,以下为(完整word版)Java程序设计之swt教程的全部内容。

第4章SWT图形用户界面本章要点1.SWT程序开发步骤。

2.SWT常用组件的使用。

3.SWT的布局。

4.SWT的事件处理。

5.SWT Designer简介。

本章难点1.SWT常用组件的使用。

2.SWT的布局.3.SWT的事件处理。

4.1SWT/JFace简介SWT(Standard Widget Toolkit)即标准小窗口工具箱,是IBM公司推出的一种在Eclipse 中使用的集成开发环境,SWT提供可移植的API,并与底层本机OS GUI平台紧密集成,它是一个与本地窗口系统集成在一起的小部件集和图形库。

SWT由JNI(Java Native Interface,Java 本机接口)调用操作系统的内部API,因此运行速度快,能够获得与操作系统的内部应用程序相同的外观.JFace是一个用户界面工具箱,也是一个易用、功能强大的图形包,它简化了常见的图形用户界面的编程任务。

SWT和JFace都是Eclipse平台上的主要组件.JFace是在SWT的基础上创建的,但JFace并不能完全覆盖SWT的功能,JFace和SWT的关系如图4.1所示。

由于JFace 的功能更强大,因此做图形界面开发时一般优先选用JFace。

图4.1 JFace和SWT的关系4.1.1 SWT程序开发步骤在eclipse的plugins目录下,找到文件org.eclipse。

swt技巧(二)

swt技巧(二)

swt技巧(二)SWT技巧简介SWT(Standard Widget Toolkit)是一种使用Java编写的界面工具包,它提供了一组丰富的图形界面控件和功能,用于构建跨平台的桌面应用程序。

在本文中,我们将介绍一些SWT的常用技巧,帮助您更好地利用SWT开发应用程序。

开发环境搭建1.下载并安装Java Development Kit(JDK)–访问官方网站,下载适用于您操作系统的JDK安装包–执行安装程序,并根据提示完成安装2.下载并配置Eclipse开发环境–访问官方网站,下载适用于您操作系统的Eclipse IDE for Java Developers安装包–执行安装程序,并根据提示完成安装–启动Eclipse,选择合适的工作区(Workspace)–配置Java运行环境(JRE),选择已安装的JDK路径3.下载并导入SWT库–访问Eclipse官方插件市场,搜索并安装SWT插件–在Eclipse项目中,右键单击项目,选择“属性”并导航到“Java Build Path”–在“库”选项卡下,点击“添加外部JARs”按钮,选择SWT库的路径常用控件和布局管理器•Shell:应用程序窗口的容器•Composite:可嵌套的容器•Label:文本标签•Text:文本输入框•Button:按钮•Combo:下拉列表框•Table:表格•Tree:树状结构•GridLayout:网格布局管理器•FormLayout:表单布局管理器•RowLayout:行布局管理器事件处理•使用addListener()方法添加事件监听器•实现Listener接口或使用匿名内部类处理事件•常用事件类型:、、``等绘图和图像操作•使用GC(Graphical Context)对象绘制图形•使用Image类加载和处理图像数据•实现PaintListener接口处理绘图事件资源管理•使用ResourceManager类管理图像、颜色等资源•使用dispose()方法释放资源,防止内存泄漏高级功能•实现自定义控件:继承Canvas或Composite类,并重写相应方法•实现自定义布局管理器:继承Layout类,并重写相应方法•实现拖放功能:使用DragSource和DropTarget类•实现本地系统集成:使用Program类打开外部程序或文件结语本文介绍了一些常用的SWT技巧,帮助您更好地使用SWT进行应用程序开发。

SwtJfacetableViewer入门教程二(让TableViewer按照列排序)

SwtJfacetableViewer入门教程二(让TableViewer按照列排序)

SwtJfacetableViewer⼊门教程⼆(让TableViewer按照列排序)有⼀个功能是我们常使⽤的,就是在列的头上点击⼀下,整个表的记录按照这个列来排序,再点击⼀下按照这个列的反序来排序。

那JFace是如何实现这个功能的呢?在JFace中是通过⼀个排序器来实现的,就是ViewerSorter下边写出详细的步骤⼀、定义⼀个sorter继承⾃ViewerSorterimport java.util.Date;import org.eclipse.jface.viewers.Viewer;import org.eclipse.jface.viewers.ViewerSorter;public class Sorter extends ViewerSorter {private static final int ID = 1;private static final int NAME = 2;private static final int SEX = 3;private static final int AGE = 4;private static final int CREATE_DATE = 5;public static final Sorter ID_ASC = new Sorter(ID);public static final Sorter ID_DESC = new Sorter(-ID);public static final Sorter NAME_ASC = new Sorter(NAME);public static final Sorter NAME_DESC = new Sorter(-NAME);public static final Sorter SEX_ASC = new Sorter(SEX);public static final Sorter SEX_DESC = new Sorter(-SEX);public static final Sorter AGE_ASC = new Sorter(AGE);public static final Sorter AGE_DESC = new Sorter(-AGE);public static final Sorter CREATE_DATE_ASC = new Sorter(CREATE_DATE);public static final Sorter CREATE_DATE_DESC = new Sorter(-CREATE_DATE);private int sortType ;private Sorter(int sortType){this.sortType = sortType;}public int compare(Viewer viewer, Object e1, Object e2) {People p1 = (People)e1;People p2 = (People)e2;switch(sortType){case ID:{Long l1 = p1.getId();Long l2 = p2.getId();return pareTo(l2);}case -ID:{Long l1 = p1.getId();Long l2 = p2.getId();return pareTo(l1);}case NAME:{String s1 = p1.getName();String s2 = p2.getName();return pareTo(s2);}case -NAME:{String s1 = p1.getName();String s2 = p2.getName();return pareTo(s1);}case SEX:{String s1 = p1.getSex();String s2 = p2.getSex();return pareTo(s2);}case -SEX:{String s1 = p1.getSex();String s2 = p2.getSex();return pareTo(s1);}case AGE:{Integer i1 = p1.getAge();Integer i2 = p2.getAge();return pareTo(i2);}case -AGE:{Integer i1 = p1.getAge();Integer i2 = p2.getAge();return pareTo(i1);}case CREATE_DATE:{Date d1 = p1.getCreateDate();Date d2 = p2.getCreateDate();pareTo(d2);case -CREATE_DATE:{Date d1 = p1.getCreateDate();Date d2 = p2.getCreateDate();pareTo(d1);}}return 0;}}⼆、在TableViewer上,为每⼀列加⼊事件监听器类似这样的结构newColumnTableColumn.addSelectionListener(new SelectionAdapter(){boolean asc = true;public void widgetSelected(SelectionEvent e){tableViewer.setSorter(asc?Sorter.ID_ASC:Sorter.ID_DESC);asc = !asc;}});都加⼊后TestTableViewer的结果:import org.eclipse.jface.viewers.TableViewer;import org.eclipse.swt.SWT;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import yout.FillLayout;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.Table;import org.eclipse.swt.widgets.TableColumn;public class TestTableViewer {private static Table table;/*** Launch the application* @param args*/public static void main(String[] args) {final Display display = Display.getDefault();final Shell shell = new Shell();shell.setSize(500, 375);shell.setText("SWT Application");//final TableViewer tableViewer = new TableViewer(shell, SWT.CHECK|SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER|SWT.V_SCROLL|SWT.H_SCR OLL);table = tableViewer.getTable();table.setLinesVisible(true);table.setHeaderVisible(true);table.setBounds(97, 79, 373, 154);final TableColumn newColumnTableColumn = new TableColumn(table, SWT.NONE);newColumnTableColumn.setWidth(39);newColumnTableColumn.setText("ID");//加⼊事件监听器newColumnTableColumn.addSelectionListener(new SelectionAdapter(){boolean asc = true;public void widgetSelected(SelectionEvent e){tableViewer.setSorter(asc?Sorter.ID_ASC:Sorter.ID_DESC);asc = !asc;}});final TableColumn newColumnTableColumn_1 = new TableColumn(table, SWT.NONE);newColumnTableColumn_1.setWidth(85);newColumnTableColumn_1.setText("姓名");// 加⼊事件监听器newColumnTableColumn_1.addSelectionListener(new SelectionAdapter(){boolean asc = true;public void widgetSelected(SelectionEvent e){tableViewer.setSorter(asc?_ASC:_DESC);asc = !asc;}});final TableColumn newColumnTableColumn_2 = new TableColumn(table, SWT.NONE);newColumnTableColumn_2.setWidth(41);newColumnTableColumn_2.setText("性别");// 加⼊事件监听器newColumnTableColumn_2.addSelectionListener(new SelectionAdapter(){boolean asc = true;public void widgetSelected(SelectionEvent e){tableViewer.setSorter(asc?Sorter.SEX_ASC:Sorter.SEX_DESC);asc = !asc;});final TableColumn newColumnTableColumn_3 = new TableColumn(table, SWT.NONE);newColumnTableColumn_3.setWidth(43);newColumnTableColumn_3.setText("年龄");// 加⼊事件监听器newColumnTableColumn_3.addSelectionListener(new SelectionAdapter(){boolean asc = true;public void widgetSelected(SelectionEvent e){tableViewer.setSorter(asc?Sorter.AGE_ASC:Sorter.AGE_DESC);asc = !asc;}});final TableColumn newColumnTableColumn_4 = new TableColumn(table, SWT.NONE);newColumnTableColumn_4.setWidth(126);newColumnTableColumn_4.setText("创建⽇期");// 加⼊事件监听器newColumnTableColumn_4.addSelectionListener(new SelectionAdapter(){boolean asc = true;public void widgetSelected(SelectionEvent e){tableViewer.setSorter(asc?Sorter.CREATE_DATE_ASC:Sorter.CREATE_DATE_DESC);asc = !asc;}});tableViewer.setContentProvider(new ContentProvider());tableViewer.setLabelProvider(new TableLabelProvider());tableViewer.setInput(People.getPeople());shell.open();shell.setLayout(new FillLayout());yout();while (!shell.isDisposed()) {if (!display.readAndDispatch())display.sleep();}}}试⼀下结果是不是出来了?好了,最后解释⼏点:1,sorter中利⽤了jdk的compareTo来实现⽐较,当然你也可以根据⾃⼰的需求来实现。

【Java学习】Font字体类的用法介绍

【Java学习】Font字体类的用法介绍

【Java学习】Font字体类的⽤法介绍⼀、Font类简介Font类是⽤于设置图形⽤户界⾯上的字体样式的,包括字体类型(例如宋体、仿宋、Times New Roman等)、字体风格(例如斜体字、加粗等)、以及字号⼤⼩。

⼆、Font类的引⽤声明Font类位于java.awt包中,使⽤时需要在代码顶端声明import java.awt.Font;或者import java.awt.*;三、Font类的构造函数Font类的构造函数如下:public Font(String familyName,int style,int size)具体解释如下:(1)familyName是字体类型,例如宋体、仿宋、Times New Roman等;(2)style是字体风格,例如斜体字、加粗等;官⽅提供4种固定值,如下:Font.PLAIN(普通)Font.BOLD(加粗)Font.ITALIC(斜体)Font.BOLD+ Font.ITALIC(粗斜体)(3)size是字体⼤⼩,其默认单位为pt(磅),数字越⼤、字就越⼤(例如12pt字⽐10pt的字要⼤)。

四、Font类的实例化⽅法利⽤上⾯第三点介绍的构造函数,我们可以创建⼀个⾃定义样式的字体变量f。

例如:Font f = new Font("宋体",Font.BOLD,20);上述代码表⽰我声明了⼀种⾃定义字体:宋体、加粗、20pt⼤⼩。

五、Font类的设置⽅法当设置好了字体样式后,可以使⽤public void setFont(Font font)⽅法将指定组件的字体样式更新。

该⽅法适⽤于任意组件,例如按钮JButton、标签JLabel、多⾏⽂本框JTextArea等。

完整代码⽰例如下:(1)我有⼀个多⾏⽂本框,给它起名字叫做ta:JTextArea ta = new JTextArea();(2)然后我设置⼀种新的字体样式f1:Font f1= new Font("宋体",Font.BOLD,20);(3)我为ta⽂本框设置字体样式为f1:ta.setFont(f1);(4)⼤功告成!。

Swt常用控件中文教程

Swt常用控件中文教程1、Eclipse中swt的配置建议配置:jdk1.4.2以及eclipse3.1在代码中调用swt控件之前,首先建立一个项目,然后选择该项目的properties -> Java Build Path,将standard Widget ToolKit加入到Library页当中。

如下图所示:接下来可以建立第一个eclipse小程序,新建一个class,并且在该class所对应的代码中输入如下程序,其中package以及class名称根据实际情况来确定名称。

package mypakage;import org.eclipse.swt.widgets.*;import org.eclipse.swt.*;/*导入需要的类库*/public class Myfrm1 {public Myfrm1()public static void main(String[] args) {Display display = new Display();Shell shell = new Shell(display);/*shell为一个窗口对象*/Label label = new Label(shell, SWT.NONE);label.setText("Hello, World!"); /*创建一个标签对象并且设置标题文字*/label.pack();shell.pack();shell.open(); /*打开并显示窗口*/while(!shell.isDisposed())if(!display.readAndDispatch())display.sleep(); /*在窗口没有销毁之前,显示对象一直处于等待状态*/display.dispose(); /*否则,销毁对象,释放对象所占据的资源*/label.dispose();}}运行上述代码(run -> debug -> swt application)将产生如下所示的一个窗口2、button的使用按钮可能的类型有很多,例如:SWT.BORDER 含有边框的按钮SWT.CHECK 复选按钮SWT.PUSH 普通按钮SWT.RADIO 单选按钮3、Text的使用文本框的类型也有很多种选择,例如:SWT.BORDER 含有边框SWT.READ_ONLY 只读下图为包含按钮以及文本框的窗口设计上述窗口所对应的代码为:package mypakage;import org.eclipse.swt.widgets.*;import org.eclipse.swt.SWT;import org.eclipse.swt.events.*;import yout.*;public class Myfrm1 {public Myfrm1()public static void main(String[] args) {Display display = new Display( );Shell shell = new Shell(display);shell.setSize(300, 200);shell.setLayout(new RowLayout( ));shell.setText("Button Example");final Button button = new Button(shell, SWT.BORDER);button.setText("Click Me");final Text text = new Text(shell, SWT.BORDER);shell.open( );while(!shell.isDisposed( )) {if(!display.readAndDispatch( )) display.sleep( );}display.dispose( );}}如果想对控件的位置以及大小进行精确的设置,可以使用setBounds(x, y, width, height)方法来取代shell.setLayout(new RowLayout( ))。

Java视频之SWT(JFace)体验之FillLayout布局

Java视频之SWT(JFace)体验之FillLayout布局本篇文章小编和大家分享扣丁学堂视频教程SWT(JFace)体验之FillLayout布局,FillLayout 是非常简单的一种布局方式,它会以同样大小对父组件中的子组件进行布局,这些子组件将以一行或一列的形式排列。

FillLayout布局FillLayout是非常简单的一种布局方式,它会以同样大小对父组件中的子组件进行布局,这些子组件将以一行或一列的形式排列。

一般来说,用户可以在任务栏、工具栏中放置FillLayout布局,通过FillLayout布局对子组件进行定位,也可以当子组件只有一个组件时,通过FillLayout布局填充整个父组件的空间。

FillLayout的风格FillLayout布局中,可以把子组件按水平或垂直的方式进行排列,这些风格是当创建FillLayout实类时以参数形式指定的。

演示代码:package swt_jface.demo2;import org.eclipse.swt.SWT;import yout.FillLayout;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;public class FillLayoutSample {Display display = new Display();Shell shell = new Shell(display);public FillLayoutSample() {FillLayout fillLayout = new FillLayout(SWT.VERTICAL);fillLayout.marginHeight = 5;fillLayout.marginWidth = 5;fillLayout.spacing = 1;shell.setLayout(fillLayout);Button button1 = new Button(shell, SWT.PUSH);button1.setText("button1");Button button2 = new Button(shell, SWT.PUSH);button2.setText("button number 2");Button button3 = new Button(shell, SWT.PUSH);button3.setText("3");shell.pack();shell.open();while (!shell.isDisposed()) {if (!display.readAndDispatch()) {display.sleep();}}display.dispose();}public static void main(String[] args) {new FillLayoutSample();}}以上就是扣丁学堂Java在线学习小编给大家分享的用三角函数在canvas上画虚线的方法,希望对小伙伴们有所帮助。

java font 方法

java font 方法Java中的Font类是用于处理字体的类,它提供了许多方法来设置和操作字体。

在本文中,我们将介绍一些常用的Font方法,并讨论如何在Java中使用这些方法来处理字体。

1. 创建字体在Java中,我们可以使用Font类的构造函数来创建字体。

构造函数有多种重载形式,可以根据需要选择合适的构造函数。

例如,我们可以使用以下代码创建一个默认字体:```javaFont font = new Font("宋体", Font.PLAIN, 12);```这里的参数指定了字体名称、样式和大小。

在这个例子中,我们创建了一个宋体字体,样式为常规,大小为12。

2. 设置字体样式Font类提供了一些方法来设置字体的样式,例如设置字体的粗细、倾斜和下划线等。

下面是一些常用的示例代码:```javaFont boldFont = font.deriveFont(Font.BOLD); // 设置为粗体Font italicFont = font.deriveFont(Font.ITALIC); // 设置为斜体Font underlineFont = font.deriveFont(Font.PLAIN |Font.UNDERLINE); // 设置为带下划线```3. 获取字体信息Font类还提供了一些方法来获取字体的相关信息,例如字体的名称、样式和大小等。

以下是一些示例代码:```javaString fontName = font.getName(); // 获取字体名称int fontStyle = font.getStyle(); // 获取字体样式int fontSize = font.getSize(); // 获取字体大小```4. 测量文本尺寸在某些情况下,我们可能需要知道特定文本在给定字体下的宽度和高度。

Font类提供了一个方法来测量文本的尺寸。

以下是一个示例代码:```javaFontMetrics metrics = graphics.getFontMetrics(font);int textWidth = metrics.stringWidth("Hello, World!"); // 获取文本宽度int textHeight = metrics.getHeight(); // 获取文本高度```这里的graphics是一个Graphics对象,用于绘制文本。

SWT(JFace)体验之Canvas(画布)

SWT(JFace)体验之Canvas(画布)说道画图,我们首先要介绍一下SWT的GC类。

[java] view plaincopyprint?1.package swt_jface.demo10;2.import org.eclipse.swt.SWT;3.import org.eclipse.swt.custom.CLabel;4.import org.eclipse.swt.graphics.GC;5.import org.eclipse.swt.graphics.Image;6.import org.eclipse.swt.widgets.Display;7.import org.eclipse.swt.widgets.Shell;8.public class GCCreation {9.10.Display display = new Display();11.Shell shell = new Shell(display);12.public GCCreation() {13.14.Image image = new Image(display, 'C:/icons/eclipse.gi f');15.Image image2 = new Image(display, image.getImage Data());16.GC gc = new GC(image2);17.gc.setForeground(display.getSystemColor(SWT.COLOR _WHITE));18.gc.drawOval(10, 10, 90, 40);19.gc.dispose();20.21.CLabel label = new CLabel(shell, SWT.NULL);bel.setImage(image);bel.setBounds(10, 10, 130, 130);24.25.CLabel label2 = new CLabel(shell, SWT.NULL);bel2.setImage(image2);bel2.setBounds(150, 10, 130, 130);28.shell.pack();29.shell.open();30.31.while (!shell.isDisposed()) {32.if (!display.readAndDispatch()) {33.display.sleep();34.}35.}36.display.dispose();37.}38.public static void main(String[] args) {39.new GCCreation();40.}41.}接下来请参看以下示例:演示代码1:[java] view plaincopyprint?1.package swt_jface.demo10;2.import org.eclipse.swt.SWT;3.import org.eclipse.swt.events.PaintEvent;4.import org.eclipse.swt.events.PaintListener;5.import org.eclipse.swt.widgets.Canvas;6.import org.eclipse.swt.widgets.Display;7.import org.eclipse.swt.widgets.Shell;8.public class CanvasExample {9.10.Display display = new Display();11.Shell shell = new Shell(display);12.public CanvasExample() {13.Canvas canvas = new Canvas(shell, SWT.NULL);14.canvas.setBounds(10, 10, 200, 100);15.canvas.addPaintListener(new PaintListener() {16.public void paintControl(PaintEvent e) {17. e.gc.drawRoundRectangle(10, 10, 180, 80, 10, 10);18.}19.});20.21.shell.pack();22.shell.open();23.24.while (!shell.isDisposed()) {25.if (!display.readAndDispatch()) {26.display.sleep();27.}28.}29.display.dispose();30.}31.public static void main(String[] args) {32.new CanvasExample();33.}34.}演示代码2:(三角)[java] view plaincopyprint?1.package swt_jface.demo10;2.import org.eclipse.swt.SWT;3.import org.eclipse.swt.events.PaintEvent;4.import org.eclipse.swt.events.PaintListener;5.import org.eclipse.swt.graphics.Image;6.import org.eclipse.swt.graphics.Region;7.import yout.FillLayout;8.import org.eclipse.swt.widgets.Canvas;9.import org.eclipse.swt.widgets.Display;10.import org.eclipse.swt.widgets.Shell;11.public class Clipping {12.13.Display display = new Display();14.Shell shell = new Shell(display);15.public Clipping() {16.17.shell.setLayout(new FillLayout());18.19.final Canvas canvas = new Canvas(shell, SWT.NULL);20.final Image image = new Image(display, 'C:/icons/ecli pse.gif');21.canvas.addPaintListener(new PaintListener() {22.public void paintControl(PaintEvent e) {23.Region region = new Region();24.region.add(new int[]{60, 10, 10, 100, 110, 100});25. e.gc.setClipping(region);26.27. e.gc.drawImage(image, 0, 0);28.}29.});30.shell.setSize(200, 140);31.shell.open();32.33.while (!shell.isDisposed()) {34.if (!display.readAndDispatch()) {35.display.sleep();36.}37.}38.display.dispose();39.}40.public static void main(String[] args) {41.new Clipping();42.}43.}演示代码3:(动态画布)[java] view plaincopyprint?1.package swt_jface.demo10;2.import org.eclipse.swt.SWT;3.import org.eclipse.swt.events.PaintEvent;4.import org.eclipse.swt.events.PaintListener;5.import org.eclipse.swt.graphics.GC;6.import org.eclipse.swt.graphics.Image;7.import org.eclipse.swt.graphics.Point;8.import org.eclipse.swt.graphics.Rectangle;9.import yout.FillLayout;10.import org.eclipse.swt.widgets.Canvas;11.import org.eclipse.swt.widgets.Display;12.import org.eclipse.swt.widgets.Shell;13.public class DoubleBuffer {14.15.Display display = new Display();16.Shell shell = new Shell(display);17.public DoubleBuffer() {18.19.shell.setLayout(new FillLayout());20.21.final Canvas canvas = new Canvas(shell, SWT.NULL);22.canvas.addPaintListener(new PaintListener() {23.public void paintControl(PaintEvent e) {24.Point size = canvas.getSize();25.int x1 = (int) (Math.random() * size.x);26.int y1 = (int) (Math.random() * size.y);27.int x2 = Math.max(canvas.getBounds().width - x1 - 10, 50);28.int y2 = Math.max(canvas.getBounds().height - y1 - 10 , 50);29. e.gc.drawRoundRectangle(x1, y1, x2, y2, 5, 5);30.display.timerExec(100, new Runnable() {31.public void run() {32.canvas.redraw();33.}34.});35.36.}37.});38.final Canvas doubleBufferedCanvas = new Canvas(shel l, SWT.NO_BACKGROUND);39.doubleBufferedCanvas.addPaintListener(new PaintList ener() {40.public void paintControl(PaintEvent e) {41.Image image = (Image) doubleBufferedCanvas.getDat a('double-buffer-image');42.if (image == null43.|| image.getBounds().width != doubleBufferedCanvas. getSize().x44.|| image.getBounds().height != doubleBufferedCanvas. getSize().y) {45.image =46.new Image(47.display,48.doubleBufferedCanvas.getSize().x,49.doubleBufferedCanvas.getSize().y);50.doubleBufferedCanvas.setData('double-buffer-image', image);51.}52.GC imageGC = new GC(image);53.imageGC.setBackground(e.gc.getBackground());54.imageGC.setForeground(e.gc.getForeground());55.imageGC.setFont(e.gc.getFont());56.Rectangle imageSize = image.getBounds();57.imageGC.fillRectangle(0, 0, imageSize.width + 1, imag eSize.height + 1);58.Point size = doubleBufferedCanvas.getSize();59.int x1 = (int) (Math.random() * size.x);60.int y1 = (int) (Math.random() * size.y);61.int x2 = Math.max(doubleBufferedCanvas.getBounds() .width - x1 - 10, 50);62.int y2 = Math.max(doubleBufferedCanvas.getBounds() .height - y1 - 10, 50);63.imageGC.drawRoundRectangle(x1, y1, x2, y2, 5, 5);64. e.gc.drawImage(image, 0, 0);65.imageGC.dispose();66.display.timerExec(100, new Runnable() {67.public void run() {68.doubleBufferedCanvas.redraw();69.}70.});71.}72.});73.shell.setSize(300, 200);74.shell.open();75.76.while (!shell.isDisposed()) {77.if (!display.readAndDispatch()) {78.display.sleep();79.}80.}81.display.dispose();82.}83.public static void main(String[] args) {84.new DoubleBuffer();85.}86.}演示代码4:(图片)[java] view plaincopyprint?1.package swt_jface.demo10;2.import org.eclipse.swt.SWT;3.import org.eclipse.swt.events.PaintEvent;4.import org.eclipse.swt.events.PaintListener;5.import org.eclipse.swt.graphics.GC;6.import org.eclipse.swt.graphics.Image;7.import org.eclipse.swt.graphics.ImageData;8.import org.eclipse.swt.graphics.ImageLoader;9.import yout.FillLayout;10.import org.eclipse.swt.widgets.Canvas;11.import org.eclipse.swt.widgets.Control;12.import org.eclipse.swt.widgets.Display;13.import org.eclipse.swt.widgets.Shell;14.public class DrawImages {15.16.Display display = new Display();17.Shell shell = new Shell(display);18.public DrawImages() {19.20.shell.setLayout(new FillLayout());21.Canvas canvas = new Canvas(shell, SWT.NULL);22.final Image image = new Image(display, 'C:/icons/ecli pse.gif');23.canvas.addPaintListener(new PaintListener() {24.public void paintControl(PaintEvent e) {25. e.gc.drawImage(image, 10, 10);26. e.gc.drawImage(image, 0, 0, 100, 100, 200, 10, 200, 50) ;27.28.}29.});30.shell.setSize(430, 200);31.shell.open();32.captureControl(canvas, 'canvas.bmp');33.while (!shell.isDisposed()) {34.if (!display.readAndDispatch()) {35.display.sleep();36.}37.}38.display.dispose();39.}40.41.public static void captureControl(Control control, String file) {42.GC gc = new GC(control);43.Image image = new Image(control.getDisplay(), contr ol.getSize().x, control.getSize().y);44.gc.copyArea(image, 0, 0);45.ImageLoader loader = new ImageLoader();46.loader.data = new ImageData[] { image.getImageData( ) };47.loader.save(file, SWT.IMAGE_BMP);48.gc.dispose();49.}50.public static void main(String[] args) {51.new DrawImages();52.}53.}演示代码5:(画线)[java] view plaincopyprint?1.package swt_jface.demo10;2.import org.eclipse.swt.SWT;3.import org.eclipse.swt.events.PaintEvent;4.import org.eclipse.swt.events.PaintListener;5.import yout.FillLayout;6.import org.eclipse.swt.widgets.Canvas;7.import org.eclipse.swt.widgets.Display;8.import org.eclipse.swt.widgets.Shell;9.public class Drawings {10.Display display = new Display();11.Shell shell = new Shell(display);12.public Drawings() {13.14.shell.setLayout(new FillLayout());15.Canvas canvas = new Canvas(shell, SWT.BORDER);16.canvas.setBackground(display.getSystemColor(SWT.C OLOR_WHITE));17.canvas.addPaintListener(new PaintListener() {18.public void paintControl(PaintEvent e) {19.20. e.gc.drawString('SWT.LINE_SOLID (default)', 10, 10);21. e.gc.drawString('SWT.LINE_DASH', 10, 30);22. e.gc.drawString('SWT.LINE_DOT', 10, 50);23. e.gc.drawString('SWT.LINE_DASHDOT', 10, 70);24. e.gc.drawString('SWT.LINE_DASHDOTDOT', 10, 90);25. e.gc.drawString('Line width = 1 (default)', 10, 120);26. e.gc.drawString('Line width = 3', 10, 140);27.28.int start = 150;29. e.gc.setLineWidth(1);30. e.gc.setLineStyle(SWT.LINE_SOLID);31. e.gc.drawLine(start, 15, start + 200, 15);32. e.gc.setLineStyle(SWT.LINE_DASH);33. e.gc.drawLine(start, 35, start + 200, 35);34. e.gc.setLineStyle(SWT.LINE_DOT);35. e.gc.drawLine(start, 55, start + 200, 55);36. e.gc.setLineStyle(SWT.LINE_DASHDOT);37. e.gc.drawLine(start, 75, start + 200, 75);38. e.gc.setLineStyle(SWT.LINE_DASHDOTDOT);39. e.gc.drawLine(start, 95, start + 200, 95);40.41. e.gc.setLineStyle(SWT.LINE_SOLID);42. e.gc.drawLine(start, 125, start + 200, 125);43. e.gc.setLineWidth(3);44. e.gc.drawLine(start, 145, start + 200, 145);45.46.int[] points = new int[3 * 2];47.points[0] = 10;48.points[1] = 10;49.points[2] = 10;50.points[3] = 100;51.points[4] = 100;52.points[5] = 100;53. e.gc.drawPolyline(points);54.55. e.gc.setLineStyle(SWT.LINE_SOLID);56. e.gc.setBackground(display.getSystemColor(SWT.COL OR_DARK_GREEN));57. e.gc.fillArc(10, 10, 200, 100, 0, -90);58. e.gc.setLineStyle(SWT.LINE_DOT);59. e.gc.drawLine(0, 60, 220, 60);60. e.gc.drawLine(110, 0, 110, 120);61.}62.});63.64.shell.pack();65.shell.open();66.67.while (!shell.isDisposed()) {68.if (!display.readAndDispatch()) {69.display.sleep();70.}71.}72.display.dispose();73.}74.public static void main(String[] args) {75.new Drawings();76.}77.}演示代码6:(文本效果)[java] view plaincopyprint?1.package swt_jface.demo10;2.import org.eclipse.swt.SWT;3.import org.eclipse.swt.events.PaintEvent;4.import org.eclipse.swt.events.PaintListener;5.import org.eclipse.swt.graphics.Font;6.import org.eclipse.swt.graphics.Image;7.import org.eclipse.swt.graphics.Point;8.import org.eclipse.swt.graphics.Rectangle;9.import yout.FillLayout;10.import org.eclipse.swt.widgets.Canvas;11.import org.eclipse.swt.widgets.Display;12.import org.eclipse.swt.widgets.Shell;13.public class DrawT ext {14.15.Display display = new Display();16.Shell shell = new Shell(display);17.public DrawText() {18.19.shell.setLayout(new FillLayout());20.21.final Canvas canvas = new Canvas(shell, SWT.NO_BAC KGROUND);22.23.final Image image = new Image(display, 'C:/icons/ecli pse.gif');24.canvas.addPaintListener(new PaintListener() {25.public void paintControl(PaintEvent e) {26.27.Rectangle size = image.getBounds();28. e.gc.drawImage(image, 0, 0, size.width, size.height, 0, 0, canvas.getSize().x, canvas.getSize().y);29.Font font = new Font(display, 'Tahoma', 18, SWT.BOLD );30. e.gc.setFont(font);31. e.gc.setForeground(display.getSystemColor(SWT.COLO R_WHITE));32. e.gc.setBackground(display.getSystemColor(SWT.COL OR_BLUE));33.String english = 'SWT rocks!';34.String chinese = '/u4e2d/u6587/u6c49/u5b57/u6d4b/ u8bd5';35. e.gc.drawString(english, 10, 10);36. e.gc.drawString(chinese, 10, 80, true);37.String text = 'Text to be drawn in the center';38.Point textSize = e.gc.textExtent(text);39. e.gc.drawText(text, (canvas.getSize().x - textSize.x)/2, (c anvas.getSize().y - textSize.y)/2);40.font.dispose();41.}42.});43.shell.setSize(300, 150);44.shell.open();45.46.while (!shell.isDisposed()) {47.if (!display.readAndDispatch()) {48.display.sleep();49.}50.}51.display.dispose();52.}53.54.public static void main(String[] args) {55.new DrawText();56.}57.}演示代码7:(T型)[java] view plaincopyprint?1.package swt_jface.demo10;2.import org.eclipse.swt.SWT;3.import org.eclipse.swt.events.PaintEvent;4.import org.eclipse.swt.events.PaintListener;5.import org.eclipse.swt.graphics.Image;6.import org.eclipse.swt.graphics.ImageData;7.import org.eclipse.swt.graphics.PaletteData;8.import org.eclipse.swt.graphics.RGB;9.import yout.FillLayout;10.import org.eclipse.swt.widgets.Canvas;11.import org.eclipse.swt.widgets.Display;12.import org.eclipse.swt.widgets.Shell;13.public class Palettes {14.15.Display display = new Display();16.Shell shell = new Shell(display);17.public Palettes() {18.shell.setLayout(new FillLayout());19.PaletteData paletteData = new PaletteData(0xFF0000, 0x00FF00, 0x0000FF);20.ImageData imageData = new ImageData(100, 100, 24, paletteData);21.for (int i = 0; i < 100; i++) { // each column.22.for (int j = 0; j < 100; j++) { // each row.23.if (j < 30 || (i > 35 && i < 65))24.imageData.setPixel(i, j, 0xFF);25.else26.imageData.setPixel(i, j, 0xFFFF00);27.}28.}29.// int pixelValue = imageData.getPixel(50, 50);30.// int redComponent = pixelValue & imageData.palette.redMask;31.// int greenComponent = pixelValue & image Data.palette.greenMask;32.// int blueComponent = pixelValue & imageD ata.palette.blueMask;33.PaletteData paletteData2 =34.new PaletteData(new RGB[] { new RGB(0, 0, 255), // blue35.new RGB(255, 255, 0) // yellow36.});37.38.ImageData imageData2 = new ImageData(100, 100, 1, paletteData2);39.for (int i = 0; i < 100; i++) { // each column.40.for (int j = 0; j < 100; j++) { // each row.41.if (j < 30 || (i > 35 && i < 65))42.imageData2.setPixel(i, j, 0);43.else44.imageData2.setPixel(i, j, 1);45.}46.}47.48.System.out.println(imageData.palette.getRGB(imageD ata.getPixel(50, 50)));49.50.final Image image2 = new Image(display, imageData2) ;51.//final Image image = new Image(display, imageData);52.final Canvas canvas = new Canvas(shell, SWT.NULL);53.canvas.setBackground(display.getSystemColor(SWT.C OLOR_WHITE));54.canvas.addPaintListener(new PaintListener() {55.public void paintControl(PaintEvent e) {56. e.gc.drawImage(image2, 0, 0);57.}58.});59.shell.pack();60.shell.open();61.62.while (!shell.isDisposed()) {63.if (!display.readAndDispatch()) {64.display.sleep();65.}66.}67.display.dispose();68.}69.public static void main(String[] args) {70.new Palettes();71.}72.}演示代码8:(三环)[c-sharp] view plaincopyprint?1.package swt_jface.demo10;2.import org.eclipse.swt.SWT;3.import org.eclipse.swt.events.PaintEvent;4.import org.eclipse.swt.events.PaintListener;5.import yout.FillLayout;6.import org.eclipse.swt.widgets.Canvas;7.import org.eclipse.swt.widgets.Display;8.import org.eclipse.swt.widgets.Shell;9.public class XOR {10.11.Display display = new Display();12.Shell shell = new Shell(display);13.public XOR() {14.15.shell.setLayout(new FillLayout());16.final Canvas canvas = new Canvas(shell, SWT.NULL);17.canvas.setBackground(display.getSystemColor(SWT.C OLOR_WHITE));18.19.canvas.addPaintListener(new PaintListener() {20.public void paintControl(PaintEvent e) {21. e.gc.setXORMode(true);22. e.gc.setBackground(display.getSystemColor(SWT.COL OR_GREEN));23. e.gc.fillOval(60, 10, 100, 100); // Top24. e.gc.setBackground(display.getSystemColor(SWT.COL OR_RED));25. e.gc.fillOval(10, 60, 120, 120); // left bottom26. e.gc.setBackground(display.getSystemColor(SWT.COL OR_BLUE));27. e.gc.fillOval(110, 60, 100, 100); // right bottom28.}29.});30.31.shell.setSize(300, 220);32.shell.open();33.34.while (!shell.isDisposed()) {35.if (!display.readAndDispatch()) {36.display.sleep();37.}38.}39.display.dispose();40.}41.public static void main(String[] args) {42.new XOR();43.}44.}演示代码9:(Transparency)[c-sharp] view plaincopyprint?1.package swt_jface.demo10;2.import org.eclipse.swt.SWT;3.import org.eclipse.swt.events.PaintEvent;4.import org.eclipse.swt.events.PaintListener;5.import org.eclipse.swt.graphics.Image;6.import org.eclipse.swt.graphics.ImageData;7.import org.eclipse.swt.graphics.RGB;8.import yout.FillLayout;9.import org.eclipse.swt.widgets.Canvas;10.import org.eclipse.swt.widgets.Display;11.import org.eclipse.swt.widgets.Shell;12.public class Transparency {13.14.Display display = new Display();15.Shell shell = new Shell(display);16.public Transparency() {17.18.shell.setLayout(new FillLayout());19.ImageData imageData = new ImageData('C:/icons/ecli pse.jpg');20.final Image image = new Image(display, imageData);21.RGB white = new RGB(255, 255, 255);22.for(int i=0; i<imageData.width; i++) {23.for(int j=0; j<imageData.height; j++) {24.RGB rgb = imageData.palette.getRGB(imageData.getP ixel(i, j));25.int threshold = 220;26.if(rgb.red > threshold && rgb.green > threshold && r gb.blue > threshold)27.imageData.setPixel(i, j, imageData.palette.getPixel(whi te));28.}29.}30.31.imageData.transparentPixel = imageData.palette.getPi xel(new RGB(255, 255, 255));32.final Image image2 = new Image(display, imageData);33.final Canvas canvas = new Canvas(shell, SWT.NULL);34.canvas.addPaintListener(new PaintListener() {35.public void paintControl(PaintEvent e) {36. e.gc.drawImage(image, 10, 20);37. e.gc.drawImage(image2, 200, 20);38.}39.});40.shell.setSize(400, 140);41.shell.open();42.43.while (!shell.isDisposed()) {44.if (!display.readAndDispatch()) {45.display.sleep();46.}47.}48.display.dispose();49.}50.public static void main(String[] args) {51.new Transparency();52.}53.}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

扣丁学堂Java在线教程之SWT(JFace) 体验之FontRegistry
本篇文章小编主要和大家分享一下SWT(JFace) 体验之FontRegistry,喜欢Java开发的小伙伴可以随着小编一起来了解下。

测试代码如下:
package swt_jface.demo;
import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;
import yout.GridData;
import yout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class FontRegistryExample {
Display display = new Display();
Shell shell = new Shell(display);
FontRegistry fontRegistry;
public FontRegistryExample() {
init();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private void init() {
shell.setLayout(new GridLayout(2, false));
fontRegistry = new FontRegistry(display);
fontRegistry.put("button-text", new FontData[]{new FontData("Arial", 9,
SWT.BOLD)} );
fontRegistry.put("code", new FontData[]{new FontData("Courier New", 10,
SWT.NORMAL)});
Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP);
text.setFont(fontRegistry.get("code"));
text.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
text.setText("public static void main() {\n\tSystem.out.println(\"Hello\");
\n}");
GridData gd = new GridData(GridData.FILL_BOTH);
gd.horizontalSpan = 2;
text.setLayoutData(gd);
Button executeButton = new Button(shell, SWT.PUSH);
executeButton.setText("Execute");
executeButton.setFont(fontRegistry.get("button-text"));
Button cancelButton = new Button(shell, SWT.PUSH);
cancelButton.setText("Cancel");
cancelButton.setFont(fontRegistry.get("button-text"));
}
public static void main(String[] args) {
new FontRegistryExample();
}
}
以上就是小编给大家分享的SWT(JFace) 体验之FontRegistry,希望对小伙伴们有所帮助,想要了解更多内容的小伙伴可以登录扣丁学堂官网咨询。

相关文档
最新文档