`

swing

 
阅读更多
JSCrollbar重绘
实现起来还是非常简单的,首先是从BasicScrollBarUI类派生出一个子类,然后重写其中的相关方法就行了。接着在需要使用滚动条的地方用setUI方法直接载入就行了。例如

view plaincopy to clipboardprint?
JScrollPane spa = new JScrollPane(list);   
spa.getVerticalScrollBar().setUI(new CBScrollBarUI());  
JScrollPane spa = new JScrollPane(list);
spa.getVerticalScrollBar().setUI(new CBScrollBarUI()); 

好了,不多说了,还是看代码吧,相关的内容我有做注释

view plaincopy to clipboardprint?
package ui.control;   
  
import images.ImageLoader;   
  
import java.awt.Color;   
import java.awt.Dimension;   
import java.awt.GradientPaint;   
import java.awt.Graphics;   
import java.awt.Graphics2D;   
import java.awt.Image;   
import java.awt.Insets;   
import java.awt.Rectangle;   
import java.awt.geom.Point2D;   
  
import javax.swing.ImageIcon;   
import javax.swing.JButton;   
import javax.swing.JComponent;   
import javax.swing.JScrollBar;   
import javax.swing.plaf.basic.BasicArrowButton;   
import javax.swing.plaf.basic.BasicScrollBarUI;   
  
public class CBScrollBarUI extends BasicScrollBarUI   
{   
    private Color frameColor = new Color(145, 105, 55);   
  
    public Dimension getPreferredSize(JComponent c)   
    {   
        return new Dimension(16, 16);   
    }   
  
    // 重绘滚动条的滑块   
    public void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)   
    {   
        super.paintThumb(g, c, thumbBounds);   
        int tw = thumbBounds.width;   
        int th = thumbBounds.height;   
        // 重定图形上下文的原点,这句一定要写,不然会出现拖动滑块时滑块不动的现象   
        g.translate(thumbBounds.x, thumbBounds.y);   
  
        Graphics2D g2 = (Graphics2D) g;   
        GradientPaint gp = null;   
        if (this.scrollbar.getOrientation() == JScrollBar.VERTICAL)   
        {   
            gp = new GradientPaint(0, 0, new Color(242, 222, 198), tw, 0, new Color(207, 190, 164));   
        }   
        if (this.scrollbar.getOrientation() == JScrollBar.HORIZONTAL)   
        {   
            gp = new GradientPaint(0, 0, new Color(242, 222, 198), 0, th, new Color(207, 190, 164));   
        }   
        g2.setPaint(gp);   
        g2.fillRoundRect(0, 0, tw - 1, th - 1, 5, 5);   
        g2.setColor(frameColor);   
        g2.drawRoundRect(0, 0, tw - 1, th - 1, 5, 5);   
    }   
  
    // 重绘滑块的滑动区域背景   
    public void paintTrack(Graphics g, JComponent c, Rectangle trackBounds)   
    {   
        Graphics2D g2 = (Graphics2D) g;   
        GradientPaint gp = null;   
        if (this.scrollbar.getOrientation() == JScrollBar.VERTICAL)   
        {   
            gp = new GradientPaint(0, 0, new Color(198, 178, 151), trackBounds.width, 0, new Color(234, 214, 190));   
        }   
        if (this.scrollbar.getOrientation() == JScrollBar.HORIZONTAL)   
        {   
            gp = new GradientPaint(0, 0, new Color(198, 178, 151), 0, trackBounds.height, new Color(234, 214, 190));   
        }   
        g2.setPaint(gp);   
        g2.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height);   
        g2.setColor(new Color(175, 155, 95));   
        g2.drawRect(trackBounds.x, trackBounds.y, trackBounds.width - 1, trackBounds.height - 1);   
        if (trackHighlight == BasicScrollBarUI.DECREASE_HIGHLIGHT)   
            this.paintDecreaseHighlight(g);   
        if (trackHighlight == BasicScrollBarUI.INCREASE_HIGHLIGHT)   
            this.paintIncreaseHighlight(g);   
    }   
  
    // 重绘当鼠标点击滑动到向上或向左按钮之间的区域   
    protected void paintDecreaseHighlight(Graphics g)   
    {   
        g.setColor(Color.green);   
        int x = this.getTrackBounds().x;   
        int y = this.getTrackBounds().y;   
        int w = 0, h = 0;   
        if (this.scrollbar.getOrientation() == JScrollBar.VERTICAL)   
        {   
            w = this.getThumbBounds().width;   
            h = this.getThumbBounds().y - y;   
  
        }   
        if (this.scrollbar.getOrientation() == JScrollBar.HORIZONTAL)   
        {   
            w = this.getThumbBounds().x - x;   
            h = this.getThumbBounds().height;   
        }   
        g.fillRect(x, y, w, h);   
    }   
  
    // 重绘当鼠标点击滑块至向下或向右按钮之间的区域   
    protected void paintIncreaseHighlight(Graphics g)   
    {   
        Insets insets = scrollbar.getInsets();   
        g.setColor(Color.blue);   
        int x = this.getThumbBounds().x;   
        int y = this.getThumbBounds().y;   
        int w = this.getTrackBounds().width;   
        int h = this.getTrackBounds().height;   
        g.fillRect(x, y, w, h);   
    }   
  
    protected JButton createIncreaseButton(int orientation)   
    {   
        return new BasicArrowButton(orientation)   
        {   
            // 重绘按钮的三角标记   
            public void paintTriangle(Graphics g, int x, int y, int size, int direction, boolean isEnabled)   
            {   
                Graphics2D g2 = (Graphics2D) g;   
                GradientPaint gp = null;   
                Image arrowImg = null;   
                switch (this.getDirection())   
                {   
                case BasicArrowButton.SOUTH:   
                    gp = new GradientPaint(0, 0, new Color(242, 222, 198), getWidth(), 0, new Color(207, 190, 164));   
                    arrowImg = ImageLoader.get("south.gif");   
                    break;   
                case BasicArrowButton.EAST:   
                    gp = new GradientPaint(0, 0, new Color(242, 222, 198), 0, getHeight(), new Color(207, 190, 164));   
                    arrowImg = ImageLoader.get("east.gif");   
                    break;   
                }   
                g2.setPaint(gp);   
                g2.fillRect(0, 0, getWidth(), getHeight());   
                g2.setColor(frameColor);   
                g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);   
                g2.drawImage(arrowImg, (getWidth() - 2) / 2 - 5, (getHeight() - 2) / 2 - 5, 13, 13, null);   
            }   
        };   
    }   
  
    protected JButton createDecreaseButton(int orientation)   
    {   
        return new BasicArrowButton(orientation)   
        {   
            public void paintTriangle(Graphics g, int x, int y, int size, int direction, boolean isEnabled)   
            {   
                Graphics2D g2 = (Graphics2D) g;   
                GradientPaint gp = null;   
                Image arrowImg = null;   
                switch (this.getDirection())   
                {   
                case BasicArrowButton.NORTH:   
                    gp = new GradientPaint(0, 0, new Color(242, 222, 198), getWidth(), 0, new Color(207, 190, 164));   
                    arrowImg = ImageLoader.get("north.gif");   
                    break;   
                case BasicArrowButton.WEST:   
                    gp = new GradientPaint(0, 0, new Color(242, 222, 198), 0, getHeight(), new Color(207, 190, 164));   
                    arrowImg = ImageLoader.get("west.gif");   
                    break;   
                }   
                g2.setPaint(gp);   
                g2.fillRect(0, 0, getWidth(), getHeight());   
                g2.setColor(frameColor);   
                g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);   
                g2.drawImage(arrowImg, (getWidth() - 2) / 2 - 5, (getHeight() - 2) / 2 - 5, 13, 13, null);   
            }   
        };   
    }   
}  
package ui.control;

import images.ImageLoader;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.geom.Point2D;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JScrollBar;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.plaf.basic.BasicScrollBarUI;

public class CBScrollBarUI extends BasicScrollBarUI
{
	private Color frameColor = new Color(145, 105, 55);

	public Dimension getPreferredSize(JComponent c)
	{
		return new Dimension(16, 16);
	}

	// 重绘滚动条的滑块
	public void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)
	{
		super.paintThumb(g, c, thumbBounds);
		int tw = thumbBounds.width;
		int th = thumbBounds.height;
		// 重定图形上下文的原点,这句一定要写,不然会出现拖动滑块时滑块不动的现象
		g.translate(thumbBounds.x, thumbBounds.y);

		Graphics2D g2 = (Graphics2D) g;
		GradientPaint gp = null;
		if (this.scrollbar.getOrientation() == JScrollBar.VERTICAL)
		{
			gp = new GradientPaint(0, 0, new Color(242, 222, 198), tw, 0, new Color(207, 190, 164));
		}
		if (this.scrollbar.getOrientation() == JScrollBar.HORIZONTAL)
		{
			gp = new GradientPaint(0, 0, new Color(242, 222, 198), 0, th, new Color(207, 190, 164));
		}
		g2.setPaint(gp);
		g2.fillRoundRect(0, 0, tw - 1, th - 1, 5, 5);
		g2.setColor(frameColor);
		g2.drawRoundRect(0, 0, tw - 1, th - 1, 5, 5);
	}

	// 重绘滑块的滑动区域背景
	public void paintTrack(Graphics g, JComponent c, Rectangle trackBounds)
	{
		Graphics2D g2 = (Graphics2D) g;
		GradientPaint gp = null;
		if (this.scrollbar.getOrientation() == JScrollBar.VERTICAL)
		{
			gp = new GradientPaint(0, 0, new Color(198, 178, 151), trackBounds.width, 0, new Color(234, 214, 190));
		}
		if (this.scrollbar.getOrientation() == JScrollBar.HORIZONTAL)
		{
			gp = new GradientPaint(0, 0, new Color(198, 178, 151), 0, trackBounds.height, new Color(234, 214, 190));
		}
		g2.setPaint(gp);
		g2.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height);
		g2.setColor(new Color(175, 155, 95));
		g2.drawRect(trackBounds.x, trackBounds.y, trackBounds.width - 1, trackBounds.height - 1);
		if (trackHighlight == BasicScrollBarUI.DECREASE_HIGHLIGHT)
			this.paintDecreaseHighlight(g);
		if (trackHighlight == BasicScrollBarUI.INCREASE_HIGHLIGHT)
			this.paintIncreaseHighlight(g);
	}

	// 重绘当鼠标点击滑动到向上或向左按钮之间的区域
	protected void paintDecreaseHighlight(Graphics g)
	{
		g.setColor(Color.green);
		int x = this.getTrackBounds().x;
		int y = this.getTrackBounds().y;
		int w = 0, h = 0;
		if (this.scrollbar.getOrientation() == JScrollBar.VERTICAL)
		{
			w = this.getThumbBounds().width;
			h = this.getThumbBounds().y - y;

		}
		if (this.scrollbar.getOrientation() == JScrollBar.HORIZONTAL)
		{
			w = this.getThumbBounds().x - x;
			h = this.getThumbBounds().height;
		}
		g.fillRect(x, y, w, h);
	}

	// 重绘当鼠标点击滑块至向下或向右按钮之间的区域
	protected void paintIncreaseHighlight(Graphics g)
	{
		Insets insets = scrollbar.getInsets();
		g.setColor(Color.blue);
		int x = this.getThumbBounds().x;
		int y = this.getThumbBounds().y;
		int w = this.getTrackBounds().width;
		int h = this.getTrackBounds().height;
		g.fillRect(x, y, w, h);
	}

	protected JButton createIncreaseButton(int orientation)
	{
		return new BasicArrowButton(orientation)
		{
			// 重绘按钮的三角标记
			public void paintTriangle(Graphics g, int x, int y, int size, int direction, boolean isEnabled)
			{
				Graphics2D g2 = (Graphics2D) g;
				GradientPaint gp = null;
				Image arrowImg = null;
				switch (this.getDirection())
				{
				case BasicArrowButton.SOUTH:
					gp = new GradientPaint(0, 0, new Color(242, 222, 198), getWidth(), 0, new Color(207, 190, 164));
					arrowImg = ImageLoader.get("south.gif");
					break;
				case BasicArrowButton.EAST:
					gp = new GradientPaint(0, 0, new Color(242, 222, 198), 0, getHeight(), new Color(207, 190, 164));
					arrowImg = ImageLoader.get("east.gif");
					break;
				}
				g2.setPaint(gp);
				g2.fillRect(0, 0, getWidth(), getHeight());
				g2.setColor(frameColor);
				g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
				g2.drawImage(arrowImg, (getWidth() - 2) / 2 - 5, (getHeight() - 2) / 2 - 5, 13, 13, null);
			}
		};
	}

	protected JButton createDecreaseButton(int orientation)
	{
		return new BasicArrowButton(orientation)
		{
			public void paintTriangle(Graphics g, int x, int y, int size, int direction, boolean isEnabled)
			{
				Graphics2D g2 = (Graphics2D) g;
				GradientPaint gp = null;
				Image arrowImg = null;
				switch (this.getDirection())
				{
				case BasicArrowButton.NORTH:
					gp = new GradientPaint(0, 0, new Color(242, 222, 198), getWidth(), 0, new Color(207, 190, 164));
					arrowImg = ImageLoader.get("north.gif");
					break;
				case BasicArrowButton.WEST:
					gp = new GradientPaint(0, 0, new Color(242, 222, 198), 0, getHeight(), new Color(207, 190, 164));
					arrowImg = ImageLoader.get("west.gif");
					break;
				}
				g2.setPaint(gp);
				g2.fillRect(0, 0, getWidth(), getHeight());
				g2.setColor(frameColor);
				g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
				g2.drawImage(arrowImg, (getWidth() - 2) / 2 - 5, (getHeight() - 2) / 2 - 5, 13, 13, null);
			}
		};
	}
}
 



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Greentea107/archive/2011/02/24/6206086.aspx

 

  • 大小: 215.4 KB
分享到:
评论

相关推荐

    swing-layout-1.0.3

    swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3...

    JavaSwing图形界面开发与案例详解源代码

    Java Swing是目前图形界面设计的主流开发工具,《Java Swing图形界面开发与案例详解[1]》从实用的角度出发,通过大量实例全面介绍Java Swing中各种组件的应用及图形界面的开发技术。《Java Swing图形界面开发与案例...

    基于Java Swing的物业管理系统源码+数据库(95分以上课程设计).zip

    基于Java Swing的物业管理系统源码+数据库(95分以上课程设计).zip 已获高分通过项目,代码完整下载即用,无需修改确保可以运行。 基于Java Swing的物业管理系统源码+数据库(95分以上课程设计).zip 已获高分...

    java swing漂亮界面 超酷 javaswing教程

    java swing漂亮界面 超酷 javaswing教程

    基于Java Swing + MySQL的图书管理系统,优秀Java毕业设计系统,小白必看!

    基于Java Swing + MySQL的图书管理系统,优秀Java毕业设计系统,小白必看! 基于Java Swing + MySQL的图书管理系统,优秀Java毕业设计系统,小白必看! 基于Java Swing + MySQL的图书管理系统,优秀Java毕业设计系统...

    swing计算器,swing计算器 swing计算器

    swing计算器 swing计算器swing计算器 swing计算器swing计算器 swing计算器swing计算器 swing计算器swing计算器 swing计算器 简易计算器

    java Swing mysql实现简单的购物系统【源码+数据库+运行指导视频】

    本项目是一套java Swing mysql实现简单的购物系统,主要针对计算机相关专业需要项目实战练习的Java学习者。 包含:项目源码、数据库脚本、运行指导视频。 项目都经过严格调试,确保可以运行! 二、技术实现 后端:...

    基于Swing技术实现“HoneyViewer”图片查看器

    HoneyViewer图片查看器是一个基于java Swing实现的图片查看器,主要实现了图片展示功能。 本实验手册根据需求完成图片读取、切换等功能,平台的全称为:基于java Swing实现的HoneyViewer图片查看器(后简称为: Keep-...

    Swing精美界面设计和动画制作教程+实例

    Swing精美界面设计和动画制作教程+实例Swing精美界面设计和动画制作教程+实例Swing精美界面设计和动画制作教程+实例Swing精美界面设计和动画制作教程+实例Swing精美界面设计和动画制作教程+实例Swing精美界面...

    Swing 学生管理系统

    Swing 是一个为Java设计的GUI工具包。 Swing是JAVA基础类的一部分。 Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表。 Swing提供许多比AWT更好的屏幕显示元素。它们用纯Java写成,所以同Java...

    (java swing毕业设计)学生信息管理(文档+视频+源码).zip

    (java swing毕业设计)学生信息管理(文档+视频+源码)(java swing毕业设计)学生信息管理(文档+视频+源码)(java swing毕业设计)学生信息管理(文档+视频+源码)(java swing毕业设计)学生信息管理(文档+视频+源码)(java ...

    (java swing毕业设计)酒店管理系统(ppt+ER图+流程图+需求背景数据库+架构说明有lun文视频源码齐全).zip

    (java swing毕业设计)酒店管理系统(ppt+ER图+流程图+需求背景数据库+架构说明有lun文视频源码齐全)(java swing毕业设计)酒店管理系统(ppt+ER图+流程图+需求背景数据库+架构说明有lun文视频源码齐全)(java swing毕业...

    Swing Hacks[电子书+书中源码]

    通过AWT和现在的Swing,Java为编写图形化用户界面提供了丰富的客户端API。不过想要让Java、AWT和Swing达到最佳性能并不容易(也不方便),特别是在模拟交互式Web站点或像Windows XP和Mac OS X这样的操作系统外观时更...

    Java Swing图形界面开发与案例详解

    java swing是目前图形界面设计的主流开发工具 本书从实用角度出发 通过大量实例介绍各种组件及图形界面的开发技术 几乎涵盖了图形开发必备的所有常用知识 丰富的实例 且实例均来自于工程实践 适合初学者阅读 也可供...

    swing教程 swing教程

    swing 教程swing 教程swing 教程swing 教程swing 教程swing 教程swing 教程swing 教程swing 教程

    java小游戏 (源码)swing五子棋源代码

    java小游戏 (源码)swing五子棋源代码java小游戏 (源码)swing五子棋源代码java小游戏 (源码)swing五子棋源代码java小游戏 (源码)swing五子棋源代码java小游戏 (源码)swing五子棋源代码java小游戏 (源码)swing五子棋源...

    基于Java Swing&Mysql;的图书管理系统

    参考书籍用Java Swing 和 Mysql编写的图书管理系统。 功能多,分类明确,代码编写风格也很好。附上MYSQL的数据库文件,导入即可以用!参考书籍用Java Swing 和 Mysql编写的图书管理系统。 功能多,分类明确,代码...

    Swing线程的最后讨论 -- 利用异步模型

    本文并不属于任何系列,但它是The Swing Connection中发表的第三篇关于在Swing中使用线程的文章。 第一篇文章《线程与Swing》,解释了Swing的单线程规则。这篇文章现在可以在The Swing Connection Archive找到。 第...

    java Swing 上传文件

    java Swing 上传文件-------------------------------------------------------------------------------------------------------------------------------------------------------

    SWING大刀系列源码

    主题:Swing是一把刀 http://www.iteye.com/topic/702804 主题:Swing第二刀:枝间新绿一重重 http://www.iteye.com/topic/707540 主题:Swing第二小刀刀:星星之火可以燎原 http://www.iteye.com/topic/707514 ...

Global site tag (gtag.js) - Google Analytics