sui366 发表于 2013-2-5 01:21:38

swing 笔记(一)

1、synchronized关键字。
  相当于锁,第一次实例化后,将会将状态保存在内存中,第二次直接取当前的状态。直至程序退出。例如:
Public class MiniWebPlugin{
private static MiniWebPlugin singleton;
private static final Object LOCK = new Object();
public static MiniWebPlugin getInstance() {
        // Synchronize on LOCK to ensure that we don't end up creating
        // two singletons.
        synchronized (LOCK) {
            if (null == singleton) {
                MiniWebPlugin controller = new MiniWebPlugin();
                singleton = controller;
                return controller;
            }
        }
        return singleton;
}
}
 
2、计时器
TimerTask task = new SwingTimerTask() {
            public void doRun() {
            }
    };
TaskEngine.getInstance().schedule(task, 1000, 5000);
(延时1秒执行,每隔5秒执行一次)
TaskEngine.getInstance().cancel()方法停止。
TaskEngine.getInstance().run()方法启动。
 
3、static关键字使用心得:
  加 static关键字后只实例化一次,在实例未销毁前,值(状态)一直存在。防止多次调用到目标代码后出现多个实例化对象在处理“药客资讯”链接CSS中,是一个成功的使用典范。
 
4、窗口放于屏幕中央:
JFrame
 GraphicUtils.centerWindowOnComponent(Frame,SparkManager.getMainWindow());
JPanel
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
  Dimension frameSize = getSize();
  setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
 
 
5、运行: Runtime.getRuntime().exec("cmd.exe /c start " +url.toString());
 
-Dplugin=search
 
6、获取操作系统名称:System.getProperty("os.name").toLowerCase()
 
7、检索系统进程:
public static void main(String[] args) {
     Process p;
   try {
    p = Runtime.getRuntime().exec( "cmd.exe /c tasklist");
 
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = "";
            while((line=input.readLine())!=null){
               System.out.println(line);
            }
            input.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
          
}
 
8、通过超链接启动本地应用程序。
(1)修改注册表
  RegistryKey headKey = new RegistryKey(RootKey.HKEY_CLASSES_ROOT,  "yaokeIM");
    
     if(!headKey.exists()){
         headKey.create();
        
         String startUpUrl = System.getProperty("user.dir") + "\\Yaoke.exe";
    
         RegistryValue value1 = new RegistryValue("", ValueType.REG_SZ, "URL:yaokeIM Protocol Handler");
         headKey.setValue(value1);
         RegistryValue value2 = new RegistryValue("URL Protocol", ValueType.REG_SZ, "");
         headKey.setValue(value2);
        
         RegistryKey DefaultKey = new RegistryKey(RootKey.HKEY_CLASSES_ROOT,  "yaokeIM\\DefaultIcon");
         DefaultKey.create();
         RegistryValue value3 = new RegistryValue("", ValueType.REG_SZ, startUpUrl);
         DefaultKey.setValue(value3);
        
         RegistryKey valueKey = new RegistryKey(RootKey.HKEY_CLASSES_ROOT,  "yaokeIM\\shell\\open\\command");
         valueKey.create();
         RegistryValue value4 = new RegistryValue("", ValueType.REG_SZ, startUpUrl);
         valueKey.setValue(value4);
     }
(2)超链接:<a href="yaokeIM://">药客通在线</a>
 
9、超链接通过注册表向应用程序传递参数。
例:链接 <a href = “yaoke://suisui”>药客通_suisui</a>
注册表 节点 yaoke
程序:
public static void main(String[] args) {
  System.out.println(args);
}
输出: yaoke://suisui/;
 
10、命令行jar文件夹打包和解压

在命令行执行
打包:jar cvf youfile.jar *.*
解压:jar   -xvf   jfreechart-0.9.20.zip
 
11、快捷键启动功能:
SparkManager.getWorkspace().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
.put(KeyStroke.getKeyStroke("F8"), "showBrowser");
        SparkManager.getWorkspace().getActionMap()
.put("showBrowser", new AbstractAction("showBrowser") {
            public void actionPerformed(ActionEvent evt) {
                display();
            }
        });
按F8将调用display方法。
 
12 将Window窗体设为透明
com.sun.awt.AWTUtilities.setWindowOpacity(window, 0.6f);
 
13、获取鼠标坐标
Point mousepoint = MouseInfo.getPointerInfo().getLocation();   
 
13、el判断是否为空
<c:if test="${empty user.vcard.trueName }">${user.username }</c:if>
<c:if test="${not empty user.vcard.trueName}">(${user.username })</c:if>
 
14、为面板绘制渐变效果背景色
 
public void paintComponent(Graphics g) {
        BufferedImage cache = new BufferedImage(2, getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = cache.createGraphics();
 
        GradientPaint paint = new GradientPaint(0, 0, (Color)UIManager.get("main.defaultColor"), 0, getHeight(), Color.white, true);
 
        g2d.setPaint(paint);
        g2d.fillRect(0, 0, getWidth(), getHeight());
        g2d.dispose();
 
        g.drawImage(cache, 0, 0, getWidth(), getHeight(), null);
}
 
将彩色图片变为黑白色
 
public static void gray(String source, String result)  
         {  
             try 
             {  
                 BufferedImage src = ImageIO.read(new File(source));  
                 ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);  
                 ColorConvertOp op = new ColorConvertOp(cs, null);  
                 src = op.filter(src, null);  
                 ImageIO.write(src, "JPEG", new File(result));  
             }  
             catch (IOException e)  
             {  
                 e.printStackTrace();  
             }  
         }  
 
刷新全部组件
SwingUtilities.updateComponentTreeUI
页: [1]
查看完整版本: swing 笔记(一)