有才華的人,別忘記給滋潤你的那塊土壤施肥

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            28 隨筆 :: 5 文章 :: 147 評論 :: 0 Trackbacks

          用了swing有一段時間了。最近在看它的源碼,所以就想著也寫一些自己喜歡UI,首先就從簡單的button開始,不料想就碰到問題了。
          問題是這樣的,寫它的測試用例的時候,用了兩種方法去測試:
          一是:
          KJButton btn1 = new KJButton("button 1");//JButton的子類
          二是:
          JButton btn2 = new JButton("button 2");
          btn2.setUI(
          new KJButtonUI());//設置自定義的UI

          結果當鼠標放在btn1的上面的時候button的背景顏色不會跟著變化,但是當鼠標移到在btn2的上面卻會改變,想了很久不知道是怎么回事,望高手幫忙哈。(效果圖如下,不曉得如何把鼠標放在上面再截圖,所以沒截對比圖。下圖button2是鼠標放在上面的效果,但button1卻不會)

          源碼如下:
          KJButtonUI.java
          package org.kissjava.ui;

          import java.awt.*;
          import java.awt.geom.RoundRectangle2D;
          import javax.swing.*;
          import javax.swing.border.Border;
          import javax.swing.plaf.ComponentUI;
          import javax.swing.plaf.basic.BasicButtonUI;
          import javax.swing.plaf.basic.BasicHTML;
          import javax.swing.text.View;

          public class KJButtonUI extends BasicButtonUI{
              
          public static final Color BUTTON_COLOR = new Color(5115447);
              
              
          public void installUI(JComponent c) {
                  AbstractButton button 
          = (AbstractButton)c;
                  Border border 
          = button.getBorder();
                  c.putClientProperty(
          "oldBorder", border);
                  c.setBorder(
          null);
                  installListeners(button);
              }

              
          public static ComponentUI createUI(JComponent c){
                  
          return new KJButtonUI();
              }
           
              @Override
              
          public void paint(Graphics g, JComponent c) {
                  
          final AbstractButton button = (AbstractButton) c;
                  ButtonModel model 
          = button.getModel();
                  FontMetrics fm 
          = g.getFontMetrics();
                  Insets i 
          = c.getInsets();
                  Rectangle viewRect 
          = new Rectangle();
                  Rectangle iconRect 
          = new Rectangle();
                  
          final Rectangle textRect = new Rectangle();
                  viewRect.x 
          = i.left;
                  viewRect.y 
          = i.top;
                  viewRect.width 
          = button.getWidth() - (i.right + viewRect.x);
                  viewRect.height 
          = button.getHeight() - (i.bottom + viewRect.y);
                  textRect.x 
          = textRect.y = textRect.width = textRect.height = 0;
                  iconRect.x 
          = iconRect.y = iconRect.width = iconRect.height = 0;    
                  Font f 
          = c.getFont();        
                  String text 
          = SwingUtilities.layoutCompoundLabel(c, fm, button.getText(), button
                          .getIcon(), button.getVerticalAlignment(), button
                          .getHorizontalAlignment(), button.getVerticalTextPosition(), button
                          .getHorizontalTextPosition(), viewRect, iconRect, textRect, button
                          .getText() 
          == null ? 0 : button.getIconTextGap());
                  Graphics2D g2d 
          = (Graphics2D) g.create();
                  View v 
          = (View) c.getClientProperty(BasicHTML.propertyKey);
                  g2d.setFont(f);    
                  
          //改變相應的背景顏色
                  updateBackground(g2d, button);        
                  
          if (model.isArmed() && model.isPressed()) {
                       paintButtonPressed(g,button); 
                  }

                  
          // Paint the Icon
                  if(button.getIcon() != null
                     paintIcon(g,c,iconRect);
                  }

                 
          if (text != null && !text.equals("")){           
                     
          if (v != null{
                         v.paint(g, textRect);
                     }
           else {
                         paintText(g, button, textRect, text);
                     }

                  }

                 
          if (button.isFocusPainted() && button.hasFocus()) {
                       paintFocus(g,button,viewRect,textRect,iconRect);
                  }

              }

             
          protected void paintButtonPressed(Graphics g, AbstractButton button) {
                   Graphics2D g2d 
          = (Graphics2D) g.create();   
                   
          int h = button.getHeight();
                   
          int w = button.getWidth();
                   g2d.setColor(BUTTON_COLOR);
                   RoundRectangle2D.Float r2d 
          = new RoundRectangle2D.Float(00,w, h , h, h);
                   Shape clip 
          = g2d.getClip();
                   g2d.clip(r2d);
                   g2d.fillRect(
          00, w, h);
                   g2d.setClip(clip);
                   g2d.drawRoundRect(
          00, w-1, h-1 , h, h);
               }


             
          public void updateBackground(Graphics g, AbstractButton button) {
                 Graphics2D g2d 
          = (Graphics2D) g.create();
                 
          int h = button.getHeight();
                 
          int w = button.getWidth();
                 
          float tran = 0.7F;
                 
          if (!button.getModel().isRollover()) {
                     tran 
          = 0.3F;
                 }

                 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,tran));
                 g2d.setColor(BUTTON_COLOR);
                 RoundRectangle2D.Float r2d 
          = new RoundRectangle2D.Float(00,w, h , h, h);
                 Shape clip 
          = g2d.getClip();
                 g2d.clip(r2d);
                 g2d.fillRect(
          00, w, h);
                 g2d.setClip(clip);
                 g2d.drawRoundRect(
          00, w-1, h-1 , h, h);      
              }

          }

          KJButton.java
          package org.kissjava;

          import javax.swing.Action;
          import javax.swing.Icon;
          import javax.swing.JButton;
          import org.kissjava.ui.KJButtonUI;
          public class KJButton extends JButton{
              
              
          public KJButton(){
                  
          this(null,null);
              }

              
              
          public KJButton(String text){
                  
          this(text,null);
              }

              
              
          public KJButton(Action a) {
                  
          this();
                  setAction(a);
              }

              
          public KJButton(Icon icon) {
                      
          this(null, icon);
              }

              
              
          public KJButton(String text, Icon icon) {         
                      
          super(text, icon);
              }

              @Override
              
          public void updateUI() {
                  
          //setUI((ButtonUI)UIManager.getUI(this));;
                  setUI(new KJButtonUI());
              }

          }

          KJButtonTest.java
          package test;
          import java.awt.Container;
          import java.awt.Dimension;
          import javax.swing.JButton;
          import javax.swing.JFrame;
          import javax.swing.SwingUtilities;
          import org.kissjava.KJButton;
          import org.kissjava.ui.KJButtonUI;
          public class KJButtonTest extends JFrame{
              
          public KJButtonTest(){
              
          //    UIManager.put("ButtonUI", "KJButtonUI");
                  this.setLayout(null);
                  KJButton btn1 
          = new KJButton("button 1");//JButton的子類
                  JButton btn2 = new JButton("button 2");
                  btn2.setUI(
          new KJButtonUI());//設置自定義的UI
                  btn1.setBounds(20208020);
                  btn2.setBounds(
          208018040);
                  Container contentPane 
          = getContentPane();    
                  contentPane.add(btn1);
                  contentPane.add(btn2);
                  
          this.setVisible(true);        
              }

              
          public static void main(String[] args) {        
                  SwingUtilities.invokeLater(
          new Runnable() {
                      
          public void run() {
                          KJButtonTest tb 
          = new KJButtonTest();
                          tb.setPreferredSize(
          new Dimension(300200));
                          tb.setSize(tb.getPreferredSize());
                          tb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                          tb.setVisible(
          true);
                      }

                  }
          );
              }

          }

          posted on 2008-05-11 13:44 kissjava 閱讀(1445) 評論(2)  編輯  收藏 所屬分類: swing

          評論

          # re: 自定義Button的使用效果為何不一樣?[未登錄] 2008-05-16 09:14 beansoft
          建議看看ButtonModel,里面有個方法:isRollover
          boolean isRollover()指示鼠標是否在按鈕上。

          返回:
          如果鼠標在按鈕上,則返回 true.

          button.getModel() -> ButtonModel

          然后根據這個狀態單獨繪制。  回復  更多評論
            

          # re: 自定義Button的使用效果為何不一樣? 2008-05-16 14:19 枯寬
          @beansoft
          在UI的updateBackground方法里面有這個判斷的。現在就是用
          JButton btn2 = new JButton("button 2");
          btn2.setUI(new KJButtonUI());//設置自定義的UI
          這種情況下button會有各個顏色狀態畫出
          但是用自定義的
          KJButton btn1 = new KJButton("button 1");//JButton的子類
          這樣就沒那個效果。。。  回復  更多評論
            

          主站蜘蛛池模板: 阿城市| 南岸区| 舒兰市| 河池市| 成安县| 阿拉善盟| 平山县| 汾阳市| 大港区| 惠来县| 昆山市| 肃宁县| 阿尔山市| 昭通市| 鄄城县| 永登县| 鲁山县| 六枝特区| 兖州市| 穆棱市| 修文县| 鞍山市| 古蔺县| 肃宁县| 兖州市| 来凤县| 合水县| 甘南县| 尤溪县| 宣威市| 华宁县| 萨嘎县| 壶关县| 定远县| 临猗县| 胶州市| 武安市| 林周县| 东乡县| 安新县| 贵州省|