Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > .NET > VB.NET > SWT的PaintListener
【标  题】:SWT的PaintListener
【关键字】:SWT,PaintListener
【来  源】:http://bjzhanghao.cnblogs.com/archive/2005/09/23/242837.html

SWT的PaintListener

Your Ad Here

以前很少用到这个类(org.eclipse.swt.events.PaintListener),利用它可以用来在control上画一些东西,基本方法是在control上 addPaintListener()一个PaintListener,然后在这个listener里做具体的画图工作,listener在control需要绘制的时候调用。

下面例子代码用来在一个composite的中央绘制一行文字。

package com.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test3 {

    
public static void main(String[] args) {
        Display display 
= new Display();
        Shell shell 
= new Shell(display);
        shell.setLayout(
new FillLayout());
        
final Button button = new Button(shell, SWT.PUSH);
        button.setText(
"This is a button");
        
final Composite comp2 = new Composite(shell, SWT.BORDER);
        comp2.addPaintListener(
new PaintListener() {
            
public void paintControl(PaintEvent e) {
                String text 
= "This is a composite";
                Rectangle area 
= comp2.getClientArea();//client area
                int tw = calcTextWidth(e.gc, text);//text width
                int th = e.gc.getFontMetrics().getHeight();//text height
                Rectangle textArea = new Rectangle(area.x + (area.width - tw) / 2,
                        area.y 
+ (area.height - th) / 2
                        tw,
                        th);
//centerized text area
                e.gc.drawString(text, textArea.x, textArea.y);
                e.gc.drawRectangle(textArea);
            }


            
private int calcTextWidth(GC gc, String text) {
                
int stWidth = 0;
                
for (int i = 0; i < text.length(); i++{
                    
char c = text.charAt(i);
                    stWidth 
+= gc.getAdvanceWidth(c);
                }

                
return stWidth;
            }

        }
);
        shell.open();
        
while (!shell.isDisposed()) {
            
if (!display.readAndDispatch())
                display.sleep();
        }

        display.dispose();
    }

}

运行结果如下图:

[翻译]IAdaptable是什么?:【上一篇】
Draw2d里的Invalidating和Updating:【下一篇】
【相关文章】
  • SWT里Slider和Scale的区别
  • SWT?Swing?这是个问题
  • 将 ActiveX 控件集成到 SWT 应用程序
  • 使用 Java Web Start 部署 SWT 应用程序
  • 为 SWT 应用程序配备内容助理
  • 231个 SWT-JFace-Eclipse小程序代码以及部分截图
  • SWT 资源中心
  • SWT对ActiveX 的支持
  • Eclipse中安装插件SWT Designer和其他插件地址
  • SWT/JFace开发入门指南(十)
  • 【随机文章】
  • 3G、WLAN、Bluetooth三者关系之分析
  • [算法题]约瑟夫(josephus)环的php玩法
  • 今天被out参数玩了一把
  • Matlab 创建独立的外部应用程序
  • 传奇世界 狂龙紫电
  • Vs2003向Vs2005迁移之Windows窗体篇
  • 错误提示"An unsupported operation was attempted"的原因
  • CorelDRAW 12循序渐进(7)-对象的变换
  • serv-u提升权限又一思路
  • C语言中const型变量不可用做数组个数标示符
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.