Java常用代码段 - 不定时更新中

2014-11-24 09:24:19 · 作者: · 浏览: 0
记录一些自己写项目常用的代码段。
1.格式化常用日期格式
Date date = new Date(System.currentTimeMillis());  
DateFormat d3 = DateFormat.getTimeInstance();  
messageShow.append("["+d3.format(date)+"]" + msg);  

2.使用工具包,设置窗口在屏幕的正中间
Toolkit kit = Toolkit.getDefaultToolkit();      
Dimension screenSize = kit.getScreenSize();  
width = screenSize.width/2-this.getWidth()/2;  
height = screenSize.height/2-this.getHeight()/2;  
this.setLocation(width,height);  
//注意要放在PACK()或者SETSIZE()之后  

3.获取jar包源路径/java文件源路径,支持中文目录

[java] view plaincopyprint 
import java.io.IOException;  
import java.io.UnsupportedEncodingException;  
import java.net.URLDecoder;  
import javax.swing.JFrame;  
@SuppressWarnings("serial")  
public class Ini extends JFrame{  
    private static String LOCATION;  
    static {  
        try {  
            LOCATION = URLDecoder.decode(Ini.class.getProtectionDomain().getCodeSource().getLocation().getFile(),"UTF-8");  
        } catch (UnsupportedEncodingException e) {  
            LOCATION = "";  
        }  
    }  
    public static void main(String[] args) throws IOException {  
        System.out.println(LOCATION);  
    }  
}