无痕客

落花无情,流水无痕……

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

   --概述与关于swt的问题

转载自:http://www.cnblogs.com/overstep/tag/swt/

一、概述:

几天一直在用金山打字通练习英语(本人英语比较烂),把金山打字能里面的文章全部掠了N遍。打的没意思了,想想怎么能添加一些外部文件,发现金山打字通自带的外部文件导入,太坑了,得往里面手工复制内容。看了下面的图就知道效率不高吧。

我就想自己写一个能够批量导入的小软件,虽然小,可是五脏俱全。(其实主要目的就是想熟悉下java se的开发) 这里主要写一下,这次写程序遇到的问题,的解决方案与注意。以备下次使用! 还是先看下,我的成果吧!

 

二、关于swt的问题

1,去掉swt窗口的外边框: shell = new Shell(SWT.NO_TRIM);

2,在去掉swt的窗口边框以后,swt窗口是不能拖动的,所以要自己添加事件,能够像正常窗口那样,按住鼠标能手动窗口,放开鼠标窗口移动到鼠标放开的位置。

1),写一个内部内,继承Listener

复制代码
 1     //窗口移动
 2     private class ShellMoveListenter implements Listener{
 3         public void handleEvent(Event arg0) {
 4            switch (arg0.type) {  
 5                 case SWT.MouseDown:  
 6                     p.x = arg0.x;  
 7                     p.y = arg0.y;  
 8                     break;  
 9                 case SWT.MouseMove:  
10                     if (p.x == -1) {  
11                         break;  
12                     }  
13                     Point point = shell.toDisplay(arg0.x, arg0.y);  
14                     shell.setLocation(point.x - p.x, point.y - p.y);  
15                     break;  
16                 case SWT.MouseUp:  
17                     p.x = -1;  
18                     p.y = -1;  
19                     break;  
20           
21                 default:  
22                     break;  
23             }  
24         }
25     }
复制代码

2),让shell绑定该件事

1     Listener listener = new ShellMoveListenter();
2     shell.addListener(SWT.MouseDown, listener);
3     shell.addListener(SWT.MouseMove, listener);
4     shell.addListener(SWT.MouseUp, listener);

3,设置窗口显示在屏幕中间

复制代码
  //得到屏幕分辨率
    Rectangle area = Display.getDefault().getClientArea();
    int windowWidth=area.width;
    int windowHeight=area.height;
    //得到窗口宽高
    int width=shell.getBounds().width;
    int height=shell.getBounds().height;
    //设置窗口位置 
    int x=(windowWidth-width)/2;
    int y=(windowHeight-height)/2;

    shell.setLocation(x, y);
复制代码

4,打开文件夹选项框,并把得到的路径设置到text中

复制代码
1     //打开文件选项框
2     public String openFile(String text){
3         DirectoryDialog dd=new DirectoryDialog(shell);
4         dd.setText(text);
5         dd.setFilterPath("SystemDrive");
6         dd.setMessage("这个是什么??");
7         String selecteddir=dd.open();
8         return selecteddir;
9     }
复制代码
1
2
3
4
5
6
7
button.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent arg0) {
        String path=openFile("请选择要导入的文件夹目录!");
        if(path!=null)
            fileText.setText(path);
    }
});

5,外部资源路径问题,比如说背景图片:建议放在项目下面,这样打包时可以不用打包资源文件。我的项目结构如下:

1), 不能用:Stringpath=ClassLoader.getSystemResource("res/").getPath()+"bg.jpg";//这个在打包后,会报空指针异常,具体是怎么回事,我不知道。

建议用:path1 = System.getProperty("user.dir"); //得到是项目的根目录。

2),中文中问题:path1=URLDecoder.decode(path1,"UTF-8");//进行转码处理。不然会   报找不到路径异常

6,设置窗口打开与关闭的渐显与渐隐效果

1),打开时:渐显

    int alpha=0;
    shell.setAlpha(0);
    shell.open();
while(shell.getAlpha()<255){
    shell.setAlpha(alpha++);
    try {
        Thread.sleep(3);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

2),关闭时:渐隐

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    closeBtn.addSelectionListener(new SelectionAdapter(){       //关闭窗口
    public void widgetSelected(SelectionEvent event) {
        int alpha=254;
        while(!(shell.getAlpha()<=0)){
            shell.setAlpha(alpha--);
            try {
                Thread.sleep(3);
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            }
        }
        shell.close();
        }
});
 
posted on 2012-11-24 15:40  无痕客  阅读(6636)  评论(0编辑  收藏  举报