用了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(51, 154, 47);
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(0, 0,w, h , h, h);
Shape clip = g2d.getClip();
g2d.clip(r2d);
g2d.fillRect(0, 0, w, h);
g2d.setClip(clip);
g2d.drawRoundRect(0, 0, 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(0, 0,w, h , h, h);
Shape clip = g2d.getClip();
g2d.clip(r2d);
g2d.fillRect(0, 0, w, h);
g2d.setClip(clip);
g2d.drawRoundRect(0, 0, 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(20, 20, 80, 20);
btn2.setBounds(20, 80, 180, 40);
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(300, 200));
tb.setSize(tb.getPreferredSize());
tb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tb.setVisible(true);
}
});
}
}