JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 技巧文件  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Swing Examples all in one file + Tips
T55555

Java, Ruby, Haskell

版主

發文: 1026
積分: 24
於 2004-01-13 06:40 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
想不到 Swing Examples ( http://www.javaworld.com.tw/jute/post/view?bid=20&id=10232&sty=1&tpg=1&age=0 ) 人氣已達 2200+, 那網頁上次還 404...
其實, Swing Examples 已經很老... (記得好像 2 或 3 年前已見過).
而且有些可以改寫的更好...

這裡 upload 一個 "mirror file" (all in one file, mht format)
如有侵犯 copyright, 敬請告知 (那網頁好像沒有任何 copyright notice...)

==============================================
以下順便提一提我個人覺得可以偷懶的 Tips:
(BTW: 大師, 高手就不用看了, 也就不要偷笑我了 ...)
J2SE 已快到 1.5 了, 用一用新的 Swing 吧.

(1) Exit Application - 不要再用老舊的寫法
Old: using WindowListener (WindowAdapter)'s windowClosing to exit
1
2
3
4
5
  frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
      System.exit(0);
    }
  });

New: no more inner class (or implement WindowListener), just one line:
1
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


(2) Center a JFrame on the screen
Old:
1
2
3
4
5
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension size = frame.getSize();
int x = (screenSize.width - size.width) / 2;
int y = (screenSize.height - size.height) / 2;
frame.setLocation( x, y );

New: Wow, 要寫這麼多嗎? 一行就夠了:
1
frame.setLocationRelativeTo(null);


(3) 要 dump Swing Stuff ? Press Ctrl-Shift-F1 !

(4) 快速的 Test 一個 Component or 一個 Panel, 通常 ...
1
frame.getContentPane().add( myTestPanel );

可以更快, 更省 memory...
1
frame.setContentPane( myTestPanel );  // 根本就沒必要 Default Content Pane


(5) Swing support HTML: Like to have multi-line, different colors,
different fonts ... etc on label, button text ... etc?
Using HTML! Example:
1
2
3
JButton b = new JButton("<html> First Line <br> Second Line </html>");
JLabel l = new JLabel("<html><font color=red> RED </font> <font color=green> green");
// 連 ending </font> </html> 都可以省了, 不過太懶也不是好事...

當然, 如果要像 Excel 旋轉字體, 自己寫 UI class 吧.

...
... etc (寫也寫不完)

Swing 越來越大, JSpiner, default support Drag-n-Drop, full screen mode ... etc
需要的朋友,大家一起來發覺,討論吧.

JavaSwing.mht (901.41k)


T55555 edited on 2004-01-13 06:59
reply to postreply to post
作者 Re:Swing Examples all in one file + Tips [Re:T55555]
yukuan





發文: 32
積分: 0
於 2004-03-03 03:27 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
  不知道有沒有人發現:MultiComponentTable2.java有誤,無法通過 compiling

原始的:
1
2
3
4
5
6
7
8
9
10
11
12
//...
public class MultiComponentTable2 extends JFrame {
   public MultiComponentTable2() {
      //...
      EachRowEditor rowEditor = new EachRowEditor();
      rowEditor.add(0, comboBoxEditor);
      rowEditor.add(1, comboBoxEditor);
      rowEditor.add(2, checkBoxEditor);
      rowEditor.add(3, checkBoxEditor);    
      JTable table = new JTable(dm);
      //...
//...

更正後如下:
1
2
3
4
5
6
7
8
      //...
      JTable table = new JTable(dm);
      EachRowEditor rowEditor = new EachRowEditor(table);
      rowEditor.setEditorAt(0, comboBoxEditor);
      rowEditor.setEditorAt(1, comboBoxEditor);
      rowEditor.setEditorAt(2, checkBoxEditor);
      rowEditor.setEditorAt(3, checkBoxEditor);
      //...


reply to postreply to post
作者 Re:Swing Examples all in one file + Tips [Re:T55555]
godnesss





發文: 43
積分: 0
於 2006-03-27 15:31 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
JavaSwing.mht (901.41k)

這個好強(對我來說)
很多很進階的用法都可以參考(比方說 ToolTip JTree或是很複雜的JTable)
不知道是每個玩Java的前輩都這麼強還是....^^


reply to postreply to post
作者 Re:Swing Examples all in one file + Tips [Re:T55555]
godnesss





發文: 43
積分: 0
於 2006-03-27 15:35 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
(2) Center a JFrame on the screen

關於這個我測試的結果是:

只有左上角是對正畫面的正中央
當我的視窗越大,整個就看起來其實是偏向右下方了
不知道大家的情況是否也一樣?


reply to postreply to post
作者 Re:Swing Examples all in one file + Tips [Re:godnesss]
weaker

小屁屁∼



發文: 379
積分: 4
於 2006-03-28 09:25 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
godnesss wrote:
(2) Center a JFrame on the screen

關於這個我測試的結果是:

只有左上角是對正畫面的正中央
當我的視窗越大,整個就看起來其實是偏向右下方了
不知道大家的情況是否也一樣?


你要先設定好 frame size 後再用 setLocationRelativeTo
這樣就不會有這個問題了。


reply to postreply to post
作者 Re:Swing Examples all in one file + Tips [Re:T55555]
godnesss





發文: 43
積分: 0
於 2006-03-31 13:41 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
原來是這樣子
當初可能我沒注意到

那真是好用
謝謝您解答


reply to postreply to post
» JWorld@TW »  Java 技巧文件

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8