zoukankan      html  css  js  c++  java
  • 一个java swt桌面程序开发到打包的总结(1)(收集)

       --概述与关于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();
            }
    });
     
  • 相关阅读:
    hdu 2485 Destroying the bus stations 迭代加深搜索
    hdu 2487 Ugly Windows 模拟
    hdu 2492 Ping pong 线段树
    hdu 1059 Dividing 多重背包
    hdu 3315 My Brute 费用流,费用最小且代价最小
    第四天 下载网络图片显示
    第三天 单元测试和数据库操作
    第二天 布局文件
    第一天 安卓简介
    Android 获取存储空间
  • 原文地址:https://www.cnblogs.com/wuhenke/p/2786011.html
Copyright © 2011-2022 走看看