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

JScrollPane spa = new JScrollPane(list); spa.getVerticalScrollBar().setUI(new CBScrollBarUI());

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

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); } }; } }

用BasicScrollBarUI实现一个简单的个性化滚动条皮肤相关推荐

  1. html简单响应式滚动条置顶

    简单响应式滚动条置顶 一般的,让页面出现滚动条的常见方法有: overflow:auto||overflow:scroll 或者overflow-x水平滚动条和overflow-y垂直滚动条 那么现在 ...

  2. 一个简单粗暴的前后端分离方案

    项目背景 刚刚参加完一个项目,背景:后端是用java,后端服务已经开发的差不多了,现在要通过web的方式对外提供服务,也就是B/S架构.后端专注做业务逻辑,不想在后端做页面渲染的事情,只向前端提供数据 ...

  3. 基于PHP实现一个简单的在线聊天功能(轮询ajax )

    基于PHP实现一个简单的在线聊天功能(轮询ajax ) 一.总结 1.用的轮询ajax 二.基于PHP实现一个简单的在线聊天功能 一直很想试着做一做这个有意思的功能,感觉复杂的不是数据交互和表结构,麻 ...

  4. 在Kubernetes上部署一个简单的、类PaaS的平台,原来这么容易!

    作者 | Bram Dingelstad 译者 | 弯月 责编 |徐威龙 封图| CSDN下载于视觉中国 我们都遇到过这种情况:有人发现了一个bug,然而这不是一般的软件bug,甚至都不是通常意义上的 ...

  5. 一个简单的PHP购物车系统

    1.需求分析 我们需要找到一种将数据库连接到用户的浏览器的方法.用户能够按目录浏览商品. 用户应该能够从商品目录中选取商品以便此后的购买.我们也要能够记录他们选中的物品. 当用户完成购买,要合计他们的 ...

  6. php自动发邮件系统,一个简单的自动发送邮件系统(二)_php基础

    一个简单的自动发送邮件系统(二)_php基础 发布时间:2016-06-17 来源: 点击: 次 这里介绍php和mysql结合起来实用. 基本上,可以说php是介于后台数据库和前台浏览器的一个中间层 ...

  7. [安卓] 18、一个简单的例子做自定义动画按钮和自定义Actionbar

    在做安卓UI的时候有时候需自定义具有动画效果的按钮或需要自定义一下actionbar~ 本节用一个简单的demo讲如何自定义具有动画效果的按钮,以及个性化的actionbar 下面是效果: 其中: △ ...

  8. 使用html 语言建立一个简单的网页,如何用记事本建立简单的网页(1).doc

    第九章 网页制作 实验一 用记事本建立简单的HTML文件 [实验目的] 学会用HTML语言建立一个简单的网页. [实验内容] 建立一个网页,布局自定,包括自我介绍.图片.自己的电子信箱地址等,要求在标 ...

  9. 手把手教你:CSS+JS 打造一个有个性的滚动条

    前言 每次浏览网页的时候,有个东西必不可少,那就是滚动条.这时候有的同学可能会说了:这句话不对,有的网页看不到滚动条.其实吧,那并不是看不到滚动条,而是滚动条被隐藏起来了.我们反过来想想,如果没有滚动 ...

最新文章

  1. Microsoft Dynamics CRM 2013升级2015(二)正式安装升级 2015及 Reporting Extensions安装
  2. 【我看Hibernate】Hibernate 介绍及其简单应用
  3. Activiti工作流实战-2
  4. attachment delete deletion commit work issue
  5. java+springmvc+vo,springmvc+mybatis的实例详解
  6. oracle安装,未找到文件 F:\app\Administrator\product\11.2.0\dbhome_2\owb\external\oc4j_ap
  7. 知乎热点:数学专业的学霸们毕业后都在做什么
  8. Windows 10 内测版:你有Edge了,不需要别的浏览器!
  9. 阿尔伯塔大学的计算机科学专业好吗,去阿尔伯塔大学留学这些专业千万不能错过!...
  10. java 读取Zip文件进行写入
  11. 3D Segmentation with Exponential LogarithmicLoss for Highly Unbalanced Object Sizes-MICCAI2018【论文理解】
  12. jenkins手把手教你从入门到放弃02-jenkins在Windows系统安装与配置(详解)
  13. Android studio环境变量配置及其作用(JDK与SDK配置)
  14. Keras:我的第一个LSTM二分类网络模型
  15. [PHPCMS]精美大气自适应资源模板下载网站源码
  16. 字节跳动笔试难题 扑克牌的移动
  17. java中implement_java中 implement和extends的作用和区别详细解释
  18. 给虚拟机下载安装jdk,hadoop等(非常详细的步骤)
  19. 单片机开发无线控制系列-手机无线超声波测距
  20. java around_Java逆向基础之AspectJ的Around方法修改方法体

热门文章

  1. 10-STM32F1-RTC and BKP
  2. ios点击推送闪退_关于苹果手机QQ闪退的问题
  3. AI绘图实战(十):制作线稿矢量图之包头巾的女人,画矢量图/生成矢量图/导出矢量图/直出svg/vector studio插件使用 | Stable Diffusion成为设计师生产力工具
  4. 囚犯排队红帽子和蓝帽子问题
  5. 在我们身边的交互式设计失败的例子
  6. Orcad与PADS交互设计技巧一
  7. 正则表达式-JavaScript
  8. 图像处理基础和OpenCV常用接口
  9. hdu 2298 Radar 重复覆盖
  10. Qt开源作品35-秘钥生成器