修改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[Theme.style]) {
			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[Theme.style].getColor());
			g.drawRect(1, 1, w - 3, h - 3);
			return;
		}
		else if(isDefault && b.isEnabled()) {
			g.setColor(Theme.buttonDefaultColor[Theme.style].getColor());
			g.drawRect(0, 0, w - 1, h - 1);
			x += 1; y += 1; w -= 1; h -= 1;
		}
		
		if(!b.isEnabled()) {
			g.setColor(Theme.buttonLightDisabledColor[Theme.style].getColor());
		}
		else {
			g.setColor(Theme.buttonLightColor[Theme.style].getColor());
		}
		g.drawLine(x, y, w - 2, y);
		g.drawLine(x, y + 1, x, h - 2);
		
		if(!b.isEnabled()) {
			g.setColor(Theme.buttonBorderDisabledColor[Theme.style].getColor());
		}
		else {
			g.setColor(Theme.buttonBorderColor[Theme.style].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[Theme.style].getColor());
		}
		else {
			g.setColor(Theme.buttonDarkColor[Theme.style].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[Theme.style].getColor(), x, y, w, h);
			}
			else {	
				DrawRoutines.drawRoundedBorder(
					g, Theme.comboBorderColor[Theme.style].getColor(), x, y, w, h);
	
				if(b.getModel().isPressed()) return;

				if(b.getModel().isRollover() && Theme.comboRollover[Theme.style]) {
					DrawRoutines.drawRolloverBorder(
						g, Theme.buttonRolloverColor[Theme.style].getColor(), x, y, w, h);
				}
			}
		}
		else {	// it's a normal JButton or a JSpinner button
			boolean isSpinnerButton =
				Boolean.TRUE.equals(b.getClientProperty("isSpinnerButton"));
			boolean paintRollover =
				(isSpinnerButton && Theme.spinnerRollover[Theme.style]) ||
				(!isSpinnerButton && Theme.buttonRollover[Theme.style]);
			
			if (!b.isEnabled()) {
				DrawRoutines.drawRoundedBorder(
					g, Theme.buttonBorderDisabledColor[Theme.style].getColor(), x, y, w, h);
			}
			else {	
				DrawRoutines.drawRoundedBorder(
					g, Theme.buttonBorderColor[Theme.style].getColor(), x, y, w, h);
	
				if(b.getModel().isPressed()) return;
				
				if(b.getModel().isRollover() && paintRollover) {
					DrawRoutines.drawRolloverBorder(
						g, Theme.buttonRolloverColor[Theme.style].getColor(), x, y, w, h);
				}
				else if(isDefault ||
					(Theme.buttonFocusBorder[Theme.style] && b.isFocusOwner()))
				{
					DrawRoutines.drawRolloverBorder(
						g, Theme.buttonDefaultColor[Theme.style].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;
	}
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值