方便機器處理的日期時間類別 java.time.Instant; java.time.Duration.
方便人類閱讀的日期時間類別: java.time.LocalDate, java.time.LocalTime, java.time.LocalDateTime; java.time.Period
2017年9月16日 星期六
2016年11月10日 星期四
PrintWirter 和 FileWriter 有什麽不同?
PrintWriter 用來輸出格式化的文字到文字輸出串流中,提供 println, printf 或 format 方法來輸出格式化的文字。 Class PrintWriter API
Print Writer 提供 auto-flush 的功能。所以此兩個參數的建構子 PrintWriter(Writer out, boolean autoFlush)建立具備 auto-flush 的 PrintWriter.
FileWriter 是便利類別(Convenience Class), 幫我們將文字寫入檔案。此方法使用預設的字元編碼(default character encoding)及預設的位元緩衝區大小。Class FileWriter API
2015年11月20日 星期五
查詢常用的 regexp 表示式, 如 email 的 regexp 表示式
常常為了特定常用用途的 regexp 表示式傷腦筋嗎? 像是 email. 現在, 可以到 RegExLib.com 網站去查詢.
http://regexlib.com/Search.aspx?k=email&c=1&m=-1&ps=20
Java 語言的 Regexp:
^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$;
Ref: http://emailregex.com/#comment-1900789300
2015年11月9日 星期一
Cookie Helper
對處理 Cookie 很煩嗎? 試試這個 Cookie Helper.
原作者: Vasil Lukach, 2014/01/05
Source: http://stackoverflow.com/questions/20934016/how-to-add-cookie-in-jsf
1: public class CookieHelper {
2: public void setCookie(String name, String value, int expiry) {
3: FacesContext facesContext = FacesContext.getCurrentInstance();
4: HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
5: Cookie cookie = null;
6: Cookie[] userCookies = request.getCookies();
7: if (userCookies != null && userCookies.length > 0 ) {
8: for (int i = 0; i < userCookies.length; i++) {
9: if (userCookies[i].getName().equals(name)) {
10: cookie = userCookies[i];
11: break;
12: }
13: }
14: }
15: if (cookie != null) {
16: cookie.setValue(value);
17: } else {
18: cookie = new Cookie(name, value);
19: cookie.setPath(request.getContextPath());
20: }
21: cookie.setMaxAge(expiry);
22: HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
23: response.addCookie(cookie);
24: }
25: public Cookie getCookie(String name) {
26: FacesContext facesContext = FacesContext.getCurrentInstance();
27: HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
28: Cookie cookie = null;
29: Cookie[] userCookies = request.getCookies();
30: if (userCookies != null && userCookies.length > 0 ) {
31: for (int i = 0; i < userCookies.length; i++) {
32: if (userCookies[i].getName().equals(name)) {
33: cookie = userCookies[i];
34: return cookie;
35: }
36: }
37: }
38: return null;
39: }
40: }
2011年1月28日 星期五
Swing layout
Swing layout 會自動的調整元件的大小, 不同的 layout 調整大小時所遵照的規則有所不同, 整理如下:
BoxLayout: requested maximum sizes
FlowLayout: preferred sizes
BorderLayout: preferred sizes
GridLayout: Each component takes all the available space within its cell, and each cell is exactly the same size
GridBagLayout: preferred sizes
BoxLayout: requested maximum sizes
FlowLayout: preferred sizes
BorderLayout: preferred sizes
GridLayout: Each component takes all the available space within its cell, and each cell is exactly the same size
GridBagLayout: preferred sizes
2011年1月16日 星期日
Swing JTable 的使用
使用 Swing JTable 的建議驟:
1.繼承 AbstractTableModel
2. 實作以下的方法:
1.繼承 AbstractTableModel
2. 實作以下的方法:
public int getRowCount(); public int getColumnCount(); public Object getValueAt(int row, int column);
getValueAt() 的方法主要傳回儲存資料的集合中的值.
3. 若要改變 table 的 header, 要覆寫以下方法:
String
getColumnName(int column)
4. 撰寫一方法, 設定或更新儲存資料的集合. 更新完後, 記得到執行
void fireTableDataChanged()
或者其它 void fireXXX 的方法, 將資料顯示出來.
2011年1月4日 星期二
建立 Swing 應用程式: Netbean vs Eclipse
在 NetBeans IDE 建立 Swing 應用程式的方式有數種. 一種就是用 NetBeans 所提供 的 Application Framework (JSR 296). 另外一種方式是使用 NetBeans Platform.
在 Eclipse 上建立 Swing 應用程式, 可使用 WindowBuilder Pro. 兩者都可以用拖曳的方式來產生 swing GUI.
這兩天試用下來的結果, 覺得 Netbeans 的 Application Framework 用得比較順手. NetBeans Platform 就改天再試了. WindowBuilder Pro 在製作 GUI 時, 有時會發生 Exceptions, 不知道是什麼原因.
在 Eclipse 上建立 Swing 應用程式, 可使用 WindowBuilder Pro. 兩者都可以用拖曳的方式來產生 swing GUI.
這兩天試用下來的結果, 覺得 Netbeans 的 Application Framework 用得比較順手. NetBeans Platform 就改天再試了. WindowBuilder Pro 在製作 GUI 時, 有時會發生 Exceptions, 不知道是什麼原因.
2011年1月3日 星期一
Limitations on inner classes in methods
在某個方法中宣告 inner class, 例如
public void perform(){
int localVar;
MyInnerClass {
}
}
使用這種型式的 inner class 有一個很重要的限制:
"any of the method's local variables that are referenced by the inner class must be declared final."
若有一個物件 myInnerClass 的方法中參考到 localVar 變數, 則此變數應宣告為 final (常數).
原因: 在 perform() 方法中所建立來的 MyInnerClass 物件所看到的 localVar 應是一樣的. 但每一個 MyInnerClass 物件又都是獨立的有自己的看到的 localVar. 所以這個 localVar 會被 copy 給每個 MyInnerClass 同時限制成唯讀.
Ref: Section 6.6.2 Inner Classes within methods in Learning Java, 2nd edition, O'Relly, 2002,
public void perform(){
int localVar;
MyInnerClass {
}
}
使用這種型式的 inner class 有一個很重要的限制:
"any of the method's local variables that are referenced by the inner class must be declared final."
若有一個物件 myInnerClass 的方法中參考到 localVar 變數, 則此變數應宣告為 final (常數).
原因: 在 perform() 方法中所建立來的 MyInnerClass 物件所看到的 localVar 應是一樣的. 但每一個 MyInnerClass 物件又都是獨立的有自己的看到的 localVar. 所以這個 localVar 會被 copy 給每個 MyInnerClass 同時限制成唯讀.
Ref: Section 6.6.2 Inner Classes within methods in Learning Java, 2nd edition, O'Relly, 2002,
Inner class 用於處理 swing action 的處理
某個 swing 元件的 action listener 通常會使用 inner class 的技巧來處理. inner class 代表著一種 "live in" 的概念, 若 B 類別相依在 A類別, 沒有 A 就沒有 B 類別, 則 B 類別可以成為 A 類別的 inner class.
Inner class 的特性.
an inner class lets us avoid the encapsulation problem because it can access all the members of its enclosing instance.
Inner class 的特性.
an inner class lets us avoid the encapsulation problem because it can access all the members of its enclosing instance.
訂閱:
文章 (Atom)