有才华的人,别忘记给滋润你的那块土壤施肥

  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 阅读(1418) 评论(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的子类
这样就没那个效果。。。  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: