`

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...

    java swing漂亮界面 超酷 javaswing教程

    Java Swing是Java编程语言中的一个图形用户界面(GUI)工具包,它是Java Foundation Classes (JFC)的一部分,用于帮助开发者创建桌面应用程序。Swing提供了一系列组件,如按钮、文本框、菜单等,允许开发者构建功能...

    java swing漂亮界面(超酷) javaswing教程

    Java Swing 是Java编程语言中用于构建桌面应用程序用户界面的一个库,它是Java Foundation Classes (JFC) 的一部分。Swing 提供了一系列组件,如按钮、文本框、菜单等,用于创建功能丰富的图形用户界面(GUI)。在...

    java+swing漂亮软件界面+超酷+以及javaswing教程.7z

    Java Swing 是Java平台上用于构建图形用户界面(GUI)的一个库,它是Java Foundation Classes (JFC)的一部分。在Java中,Swing提供了丰富的组件集,允许开发者创建美观且功能强大的桌面应用程序。"java+swing漂亮...

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

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

    java swing mysql实现的酒店管理系统项目源码_swing_JavaSwing_酒店管理系统_

    Java Swing MySQL实现的酒店管理系统项目源码是一款基于Java图形用户界面(GUI)库Swing和关系型数据库管理系统MySQL开发的应用程序。此系统旨在为酒店提供一套全面的后台管理解决方案,涵盖了酒店日常运营中的核心...

    spring+swing的例子

    Spring 和 Swing 是两个在 Java 开发中非常重要的库。Spring 是一个开源的框架,主要用于构建企业级的 Java 应用程序,它强调依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented Programming,...

    swing开发的源代码

    Swing是Java编程语言中的一个图形用户界面(GUI)工具包,它是Java Foundation Classes (JFC)的一部分。Swing提供了一套丰富的组件,用于创建桌面应用程序,包括按钮、文本框、菜单、滚动面板等。Swing是完全由Java...

    基于java swing实现答题系统

    基于Java Swing实现答题系统的技术要点 本文将详细介绍基于Java Swing实现答题系统的技术要点,涵盖了GUI设计、事件处理、swing组件使用、Java图形化编程等多个方面的知识点。 一、GUI设计 在本文中,我们使用了...

    Java编程实现swing圆形按钮实例代码

    Java编程实现swing圆形按钮实例代码 Java编程实现swing圆形按钮实例代码主要介绍了Java编程实现swing圆形按钮实例代码,涉及两个简单的Java实现按钮的代码,其中一个具有侦测点击事件的简单功能,具有一定借鉴价值...

    swing界面美化包

    Swing是Java提供的一种用于构建图形用户界面(GUI)的库,它允许开发者创建美观、交互性强的应用程序。在默认情况下,Swing组件的外观可能显得较为朴素,但通过一些特殊的技术,我们可以对其进行美化,使其更具吸引...

    javaSwing皮肤大全.rar

    Java Swing 是Java GUI(图形用户界面)库的一部分,它提供了丰富的组件和工具,用于构建桌面应用程序。Swing 提供了一种可定制外观和感觉(LookAndFeel)的方法,使得开发者可以改变应用的视觉风格,以满足不同用户...

    解决swing白屏问题的jjre

    Swing是Java GUI库的一部分,用于构建桌面应用程序。在某些情况下,用户可能会遇到“白屏”问题,这通常是由于Swing组件渲染不正确或者与Java运行时环境(JRE)的兼容性问题导致的。标题提到的“解决swing白屏问题的...

    Swing显示Html网页

    Swing显示HTML网页是Java开发GUI应用程序时的一个常见需求,主要涉及到Java的Swing库以及相关的第三方库,如JDIC(Java Desktop Integration Components)。本文将详细介绍如何在Swing界面中集成HTML网页显示功能。 ...

    The Definitive Guide to Java Swing Third Edition

    ### 《Java Swing 终极指南》第三版关键知识点概览 #### 一、书籍基本信息与版权信息 - **书名**:《Java Swing 终极指南》第三版 - **作者**:John Zukowski - **出版社**:本书由Springer-Verlag New York, Inc....

    swing教程 swing教程

    Swing 教程 Swing 简介 Swing 是 Java 的一个图形用户界面(GUI)工具包,用于创建跨平台的 GUI 应用程序。Swing 是 Java Foundation Classes(JFC)的一个组件,提供了大量的轻量级 GUI 组件,可以用来创建复杂的...

    eclipse配置(安装)swing插件

    ### Eclipse配置Swing插件详解 #### 一、Eclipse简介与下载 Eclipse是一款流行的开源集成开发环境(IDE),支持多种编程语言如Java、C++等。它因其高度可扩展性和强大的功能集而受到开发者们的青睐。对于Java...

    graalvm编译swing失败问题处理

    标题 "graalvm编译swing失败问题处理" 涉及到的是在使用GraalVM进行Java Swing应用的编译时遇到的问题及其解决方法。GraalVM是一款高性能的运行时环境,支持多种语言,并且对于Java,它提供了一个先进的即时编译器,...

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

    Swing计算器是一款基于Java Swing库开发的简单计算工具。Swing是Java的一种图形用户界面(GUI)工具包,它是Java Foundation Classes (JFC)的一部分,主要用于构建桌面应用程序。本项目的核心在于利用Swing组件来...

Global site tag (gtag.js) - Google Analytics