62,616
社区成员
发帖
与我相关
我的任务
分享
protected void clearTextShiftOffset(){
this.shiftOffset = 0;
}
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicButtonUI;
public class MyButtonUI extends BasicButtonUI {
protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect){
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
Icon icon = b.getIcon();
Icon tmpIcon = null;
if(icon == null) {
return;
}
Icon selectedIcon = null;
if (model.isSelected()) {
selectedIcon = (Icon) b.getSelectedIcon();
if (selectedIcon != null) {
icon = selectedIcon;
}
}
if(model.isPressed() && model.isArmed()) {
tmpIcon = (Icon) b.getPressedIcon();
if(tmpIcon != null) {
clearTextShiftOffset();
}
} else if(b.isRolloverEnabled() && model.isRollover()) {
if(model.isSelected()) {
tmpIcon = (Icon) b.getRolloverSelectedIcon();
if (tmpIcon == null) {
tmpIcon = selectedIcon;
}
}
if (tmpIcon == null) {
tmpIcon = (Icon) b.getRolloverIcon();
}
}
if(tmpIcon != null) {
icon = tmpIcon;
}
if(model.isPressed() && model.isArmed()) {
icon.paintIcon(c, g, iconRect.x + getTextShiftOffset(),
iconRect.y + getTextShiftOffset());
} else {
icon.paintIcon(c, g, iconRect.x, iconRect.y);
}
}
}
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicButtonUI;
import sun.swing.SwingUtilities2;
public class MyButtonUI extends BasicButtonUI {
public void update(Graphics g, JComponent c) {
AbstractButton button = (AbstractButton)c;
if ((c.getBackground() instanceof UIResource) &&
button.isContentAreaFilled()) {
ButtonModel model = button.getModel();
if (MetalUtils.isToolBarButton(c)) {
if (!model.isArmed() && !model.isPressed() &&
MetalUtils.drawGradient(
c, g, "Button.gradient", 0, 0, c.getWidth(),
c.getHeight(), true)) {
paint(g, c);
return;
}
}
else if (MetalUtils.drawGradient(
c, g, "Button.gradient", 0, 0, c.getWidth(),
c.getHeight(), true)) {
paint(g, c);
return;
}
}
super.update(g, c);
}
protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect){
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
Icon icon = b.getIcon();
Icon tmpIcon = null;
if(icon == null) {
return;
}
Icon selectedIcon = null;
if (model.isSelected()) {
selectedIcon = (Icon) b.getSelectedIcon();
if (selectedIcon != null) {
icon = selectedIcon;
}
}
if(model.isPressed() && model.isArmed()) {
tmpIcon = (Icon) b.getPressedIcon();
if(tmpIcon != null) {
clearTextShiftOffset();
}
} else if(b.isRolloverEnabled() && model.isRollover()) {
if(model.isSelected()) {
tmpIcon = (Icon) b.getRolloverSelectedIcon();
if (tmpIcon == null) {
tmpIcon = selectedIcon;
}
}
if (tmpIcon == null) {
tmpIcon = (Icon) b.getRolloverIcon();
}
}
if(tmpIcon != null) {
icon = tmpIcon;
}
if(model.isPressed() && model.isArmed()) {
icon.paintIcon(c, g, iconRect.x + getTextShiftOffset(),
iconRect.y + getTextShiftOffset());
} else {
icon.paintIcon(c, g, iconRect.x, iconRect.y);
}
}
protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
AbstractButton b = (AbstractButton) c;
FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
int mnemIndex = b.getDisplayedMnemonicIndex();
g.setColor(b.getForeground());
SwingUtilities2.drawStringUnderlineCharAt(c, g,text,mnemIndex,
textRect.x, textRect.y + fm.getAscent());
}
}