隨筆-46  評論-64  文章-2  trackbacks-0
          這個技巧比較Cool也比較基礎常用,關鍵技術是使用圖像重新繪制組件
          看下面demo的代碼
          import?java.awt.Insets;
          import?javax.swing.ImageIcon;
          import?javax.swing.JButton;

          public?class?ImageButton?extends?JButton?{

          ????
          private?static?final?long?serialVersionUID?=?7760427126786950870L;

          ????
          public?ImageButton(ImageIcon?icon)?{
          ????????setSize(icon.getImage().getWidth(
          null),icon.getImage().getHeight(null));
          ????????setIcon(icon);
          ????????setMargin(
          new?Insets(0,0,0,0));
          ????????setIconTextGap(
          0);
          ????????setBorderPainted(
          false);
          ????????setBorder(
          null);
          ????????setText(
          null);

          ????}

          }


          稍微要解釋一下的是講button的邊框都設置成為0,還有邊框的重繪設置false,我們可以用不同的貼圖表示按鈕被選中等的狀態

          使用這個組件的demoCode

          ????????ImageButton?button?=?new?ImageButton("images/*.png");
          ????????button.setPressedIcon(
          new?ImageIcon("images/*.png"));
          ????????button.setRolloverIcon(
          new?ImageIcon("images/*.png"));
          ????????button.setSelectedIcon(
          new?ImageIcon("images/*.png"));
          ????????button.setRolloverSelectedIcon(
          new?ImageIcon("images/*.png"));
          ????????button.setDisabledIcon(
          new?ImageIcon("images/*.png"));
          ????????button.setDisabledSelectedIcon(
          new?ImageIcon("images/*.png"));

          這個Hack要顯示效果好,關鍵就在于貼圖了,可見美工很重要。

          我準備每天Hack一篇

          更多內容,可以看Swing Hacks


          參考資料:
          ?"Swing Hacks by Joshua Marinacci and Chris Adamson. Copyright 2005 O'Reilly Media, Inc., 0-596-00907-0."
          posted on 2007-03-28 23:57 jht 閱讀(526) 評論(0)  編輯  收藏 所屬分類: J2SESwing Tips
          主站蜘蛛池模板: 沙坪坝区| 赤壁市| 南涧| 象州县| 赤水市| 逊克县| 石嘴山市| 济南市| 即墨市| 平原县| 同仁县| 印江| 肥西县| 黑山县| 嘉兴市| 博爱县| 桂阳县| 雷波县| 桂林市| 泉州市| 吴桥县| 襄汾县| 读书| 平江县| 海林市| 南昌市| 馆陶县| 项城市| 延安市| 成都市| 苗栗市| 特克斯县| 年辖:市辖区| 朔州市| 湾仔区| 赣州市| 卢氏县| 宾川县| 皮山县| 青浦区| 蒙自县|