- 浏览: 61522 次
- 性别:
- 来自: 深圳
最新评论
-
zhylandroid:
...
JTable( 放在JScrollPane中 )中加入一行后, 然后让其获得焦点且滚动条自动下来 -
dkeagle:
太感谢了,太有用了,在网上查了两天资料了...
JTable( 放在JScrollPane中 )中加入一行后, 然后让其获得焦点且滚动条自动下来
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
发表评论
-
curtain
2013-04-01 16:45 0curtain -
jlayer
2013-03-26 18:13 0jlayer pane. -
swing网站
2013-03-01 15:44 0(2009年4月2日更新) 最近在网上找到了一些比 ... -
aasss
2012-08-03 18:09 0aasss -
关于swing的文章
2012-07-30 15:50 0http://blog.csdn.net/mylxiaoyi/ ... -
vista button
2012-06-12 20:14 0import java.awt.AlphaComposite; ... -
database demo
2012-06-09 17:17 0kkk -
全局键盘事件
2012-06-06 10:19 1253KeyboardFocusManager manager = ... -
sssfw
2012-05-04 20:56 0http://www.byywee.com/page/M0/S ... -
swingtexiao
2012-05-04 20:00 0swingtexiao -
sss
2012-04-28 18:11 0sss -
使用annotation获取实体类注释
2012-04-11 18:57 0使用annotation获取实体类注释 -
自定义scrollbar
2012-04-11 18:49 0自定义scrollbar -
自定义绘制按钮
2012-04-11 18:47 0自定义绘制按钮 -
2012年4月6日
2012-04-06 18:48 02012年4月6日 -
批量缩放图片工具
2012-03-28 19:13 0挺好用 -
工作文档
2012-03-24 18:17 0import java.util.ArrayList; im ... -
test
2012-03-23 17:45 1tset -
A星算法
2012-03-23 15:20 1203A星算法 -
aasss
2012-03-21 18:58 1aaaaa
相关推荐
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...
Java Swing是Java编程语言中的一个图形用户界面(GUI)工具包,它是Java Foundation Classes (JFC)的一部分,用于帮助开发者创建桌面应用程序。Swing提供了一系列组件,如按钮、文本框、菜单等,允许开发者构建功能...
Java Swing 是Java编程语言中用于构建桌面应用程序用户界面的一个库,它是Java Foundation Classes (JFC) 的一部分。Swing 提供了一系列组件,如按钮、文本框、菜单等,用于创建功能丰富的图形用户界面(GUI)。在...
Java Swing 是Java平台上用于构建图形用户界面(GUI)的一个库,它是Java Foundation Classes (JFC)的一部分。在Java中,Swing提供了丰富的组件集,允许开发者创建美观且功能强大的桌面应用程序。"java+swing漂亮...
Swing精美界面设计和动画制作教程+实例Swing精美界面设计和动画制作教程+实例Swing精美界面设计和动画制作教程+实例Swing精美界面设计和动画制作教程+实例Swing精美界面设计和动画制作教程+实例Swing精美界面...
Java Swing MySQL实现的酒店管理系统项目源码是一款基于Java图形用户界面(GUI)库Swing和关系型数据库管理系统MySQL开发的应用程序。此系统旨在为酒店提供一套全面的后台管理解决方案,涵盖了酒店日常运营中的核心...
Spring 和 Swing 是两个在 Java 开发中非常重要的库。Spring 是一个开源的框架,主要用于构建企业级的 Java 应用程序,它强调依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented Programming,...
Swing是Java编程语言中的一个图形用户界面(GUI)工具包,它是Java Foundation Classes (JFC)的一部分。Swing提供了一套丰富的组件,用于创建桌面应用程序,包括按钮、文本框、菜单、滚动面板等。Swing是完全由Java...
基于Java Swing实现答题系统的技术要点 本文将详细介绍基于Java Swing实现答题系统的技术要点,涵盖了GUI设计、事件处理、swing组件使用、Java图形化编程等多个方面的知识点。 一、GUI设计 在本文中,我们使用了...
Java编程实现swing圆形按钮实例代码 Java编程实现swing圆形按钮实例代码主要介绍了Java编程实现swing圆形按钮实例代码,涉及两个简单的Java实现按钮的代码,其中一个具有侦测点击事件的简单功能,具有一定借鉴价值...
Swing是Java提供的一种用于构建图形用户界面(GUI)的库,它允许开发者创建美观、交互性强的应用程序。在默认情况下,Swing组件的外观可能显得较为朴素,但通过一些特殊的技术,我们可以对其进行美化,使其更具吸引...
Java Swing 是Java GUI(图形用户界面)库的一部分,它提供了丰富的组件和工具,用于构建桌面应用程序。Swing 提供了一种可定制外观和感觉(LookAndFeel)的方法,使得开发者可以改变应用的视觉风格,以满足不同用户...
Swing是Java GUI库的一部分,用于构建桌面应用程序。在某些情况下,用户可能会遇到“白屏”问题,这通常是由于Swing组件渲染不正确或者与Java运行时环境(JRE)的兼容性问题导致的。标题提到的“解决swing白屏问题的...
Swing显示HTML网页是Java开发GUI应用程序时的一个常见需求,主要涉及到Java的Swing库以及相关的第三方库,如JDIC(Java Desktop Integration Components)。本文将详细介绍如何在Swing界面中集成HTML网页显示功能。 ...
### 《Java Swing 终极指南》第三版关键知识点概览 #### 一、书籍基本信息与版权信息 - **书名**:《Java Swing 终极指南》第三版 - **作者**:John Zukowski - **出版社**:本书由Springer-Verlag New York, Inc....
Swing 教程 Swing 简介 Swing 是 Java 的一个图形用户界面(GUI)工具包,用于创建跨平台的 GUI 应用程序。Swing 是 Java Foundation Classes(JFC)的一个组件,提供了大量的轻量级 GUI 组件,可以用来创建复杂的...
### Eclipse配置Swing插件详解 #### 一、Eclipse简介与下载 Eclipse是一款流行的开源集成开发环境(IDE),支持多种编程语言如Java、C++等。它因其高度可扩展性和强大的功能集而受到开发者们的青睐。对于Java...
标题 "graalvm编译swing失败问题处理" 涉及到的是在使用GraalVM进行Java Swing应用的编译时遇到的问题及其解决方法。GraalVM是一款高性能的运行时环境,支持多种语言,并且对于Java,它提供了一个先进的即时编译器,...
Swing计算器是一款基于Java Swing库开发的简单计算工具。Swing是Java的一种图形用户界面(GUI)工具包,它是Java Foundation Classes (JFC)的一部分,主要用于构建桌面应用程序。本项目的核心在于利用Swing组件来...