longyi_java 发表于 2013-1-27 04:35:59

修改Button的边框

在TinyLookAndFeel.java 的 protected void initComponentDefaults(UIDefaults table)  方法中设置
 
table.put("Button.border",   new BorderUIResource.CompoundBorderUIResource(    new TinyButtonBorder(),    new BasicBorders.MarginBorder())); 
 
 
TinyButtonBorder是继承AbstractBorder.java和实现UIResource.java 的类
 
 
public class TinyButtonBorder extends AbstractBorder implements UIResource {protected final Insets borderInsets = new Insets(2, 2, 2, 2);/** * Draws the button border for the given component. * * @param mainColor The component to draw its border. * @param g The graphics context. * @param x The x coordinate of the top left corner. * @param y The y coordinate of the top left corner. * @param w The width. * @param h The height. */public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {switch (Theme.derivedStyle) {case Theme.TINY_STYLE :drawTinyBorder(c, g, x, y, w, h);break;case Theme.W99_STYLE :drawWinBorder(c, g, x, y, w, h);break;case Theme.YQ_STYLE :drawXpBorder(c, g, x, y, w, h);break;}}private void drawTinyBorder(Component c, Graphics g, int x, int y, int w, int h) {}private void drawWinBorder(Component c, Graphics g, int x, int y, int w, int h) {AbstractButton b = (AbstractButton)c;boolean isDefault = (c instanceof JButton) && (((JButton)c).isDefaultButton());if(b.getModel().isPressed() ||((b instanceof JToggleButton) && b.isSelected())){g.setColor(Color.BLACK);g.drawRect(0, 0, w - 1, h - 1);g.setColor(Theme.buttonDarkColor.getColor());g.drawRect(1, 1, w - 3, h - 3);return;}else if(isDefault && b.isEnabled()) {g.setColor(Theme.buttonDefaultColor.getColor());g.drawRect(0, 0, w - 1, h - 1);x += 1; y += 1; w -= 1; h -= 1;}if(!b.isEnabled()) {g.setColor(Theme.buttonLightDisabledColor.getColor());}else {g.setColor(Theme.buttonLightColor.getColor());}g.drawLine(x, y, w - 2, y);g.drawLine(x, y + 1, x, h - 2);if(!b.isEnabled()) {g.setColor(Theme.buttonBorderDisabledColor.getColor());}else {g.setColor(Theme.buttonBorderColor.getColor());}g.drawLine(x, h - 1, w - 1, h - 1);g.drawLine(w - 1, y, w - 1, h - 1);if(!b.isEnabled()) {g.setColor(Theme.buttonDarkDisabledColor.getColor());}else {g.setColor(Theme.buttonDarkColor.getColor());}g.drawLine(x + 1, h - 2, w - 2, h - 2);g.drawLine(w - 2, y + 1, w - 2, h - 2);}private void drawXpBorder(Component c, Graphics g, int x, int y, int w, int h) {AbstractButton b = (AbstractButton)c;boolean isDefault = (c instanceof JButton) && (((JButton)c).isDefaultButton());boolean isComboBoxButton =Boolean.TRUE.equals(b.getClientProperty("isComboBoxButton"));if(isComboBoxButton) {if (!b.isEnabled()) {DrawRoutines.drawRoundedBorder(g, Theme.comboBorderDisabledColor.getColor(), x, y, w, h);}else {DrawRoutines.drawRoundedBorder(g, Theme.comboBorderColor.getColor(), x, y, w, h);if(b.getModel().isPressed()) return;if(b.getModel().isRollover() && Theme.comboRollover) {DrawRoutines.drawRolloverBorder(g, Theme.buttonRolloverColor.getColor(), x, y, w, h);}}}else {// it's a normal JButton or a JSpinner buttonboolean isSpinnerButton =Boolean.TRUE.equals(b.getClientProperty("isSpinnerButton"));boolean paintRollover =(isSpinnerButton && Theme.spinnerRollover) ||(!isSpinnerButton && Theme.buttonRollover);if (!b.isEnabled()) {DrawRoutines.drawRoundedBorder(g, Theme.buttonBorderDisabledColor.getColor(), x, y, w, h);}else {DrawRoutines.drawRoundedBorder(g, Theme.buttonBorderColor.getColor(), x, y, w, h);if(b.getModel().isPressed()) return;if(b.getModel().isRollover() && paintRollover) {DrawRoutines.drawRolloverBorder(g, Theme.buttonRolloverColor.getColor(), x, y, w, h);}else if(isDefault ||(Theme.buttonFocusBorder && b.isFocusOwner())){DrawRoutines.drawRolloverBorder(g, Theme.buttonDefaultColor.getColor(), x, y, w, h);}}}}/** * Gets the border insets for a given component. * * @param c The component to get its border insets. * @return Always returns the same insets as defined in <code>insets</code>. */public Insets getBorderInsets(Component c) {return borderInsets;}} 
 
 
页: [1]
查看完整版本: 修改Button的边框