---
title: "Java自定义JSlider UI"
author: "mmdev"
date: "2007-12-18T14:05:00Z"
tags: []
slug: "java-jslider-ui"
draft: false
lastmod: "2012-04-14T20:14:13Z"
id: 1486369
url: "https://mmdev.iteye.com/blog/1486369"
---

JSlider作为Swing中提供的滑标组件， 以图形方式在有界区间内通过移动滑块来选择值，滑块可以显示主刻度标记和次刻度标记。大量应用于如播放器中的音量设定等领域中。

但是JSlider本身提供的UI样式很单调，不足以满足用户的审美需求，往往需要我们自行重构其UI。

下面我给出一个简单的例子。

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packageorg.loon.framework.swing.test;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.BorderLayout;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Color;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Component;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Dimension;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.EventQueue;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.GradientPaint;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Graphics;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Graphics2D;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Polygon;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Rectangle;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.RenderingHints;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.Box;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.JFrame;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.JLabel;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.JPanel;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.JSlider;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.WindowConstants;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.event.ChangeEvent;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.event.ChangeListener;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Title:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Description:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Copyright:Copyright(c)2007  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Company:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchenpeng  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@email：ceponline@yahoo.com.cn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@version0.1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassExampleSliderextendsJPanel...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinallongserialVersionUID=1L;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicExampleSlider()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设定布局器  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)super(newBorderLayout());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设定监听器  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)ChangeListenerlistener=newChangeListener()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidstateChanged(ChangeEvente)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(e.getSource()instanceofJSlider)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.out.println("刻度:"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)+((JSlider)e.getSource()).getValue());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设定JSlider1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)JSliders1=newJSlider(0,100,0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//注入自定义ui  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)s1.setUI(newMySliderUI());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//主刻度  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)s1.setMajorTickSpacing(10);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//次刻度  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)s1.setMinorTickSpacing(5);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设定为显示  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)s1.setPaintTicks(true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)s1.setPaintLabels(true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//监听slider1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)s1.addChangeListener(listener);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设定JSlider2  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)JSliders2=newJSlider(0,100,0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//使用MetalSliderUI为ui  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)s2.setUI(newjavax.swing.plaf.metal.MetalSliderUI()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)protectedvoidpaintHorizontalLabel(Graphicsg,intv,Componentl)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)JLabellbl=(JLabel)l;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)lbl.setForeground(Color.green);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)super.paintHorizontalLabel(g,v,lbl);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)});  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)s2.setForeground(Color.BLUE);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)s2.setMajorTickSpacing(10);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)s2.setMinorTickSpacing(5);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)s2.setPaintTicks(true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)s2.setPaintLabels(true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)s2.addChangeListener(listener);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//使用盒式容器  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Boxbox=Box.createVerticalBox();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)box.add(Box.createVerticalStrut(5));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)box.add(s1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)box.add(Box.createVerticalStrut(5));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)box.add(s2);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)box.add(Box.createVerticalGlue());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)add(box,BorderLayout.CENTER);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)add(Box.createHorizontalStrut(5),BorderLayout.WEST);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)add(Box.createHorizontalStrut(5),BorderLayout.EAST);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设定窗体大小  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)setPreferredSize(newDimension(240,100));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticvoidmain(String\[]args)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)EventQueue.invokeLater(newRunnable()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidrun()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)createUI();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)});  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticvoidcreateUI()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)JFrameframe=newJFrame("音量刻度设置");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frame.setDefaultCloseOperation(WindowConstants.EXIT\_ON\_CLOSE);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frame.getContentPane().add(newExampleSlider());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frame.setResizable(false);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frame.pack();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//居中  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frame.setLocationRelativeTo(null);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frame.setVisible(true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)classMySliderUIextendsjavax.swing.plaf.metal.MetalSliderUI...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*绘制指示物  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidpaintThumb(Graphicsg)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Graphics2Dg2d=(Graphics2D)g;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g2d.setRenderingHint(RenderingHints.KEY\_ANTIALIASING,RenderingHints.VALUE\_ANTIALIAS\_ON);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//填充椭圆框为当前thumb位置  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g2d.fillOval(thumbRect.x,thumbRect.y,thumbRect.width,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)thumbRect.height);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//也可以帖图(利用鼠标事件转换image即可体现不同状态)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//g2d.drawImage(image,thumbRect.x,thumbRect.y,thumbRect.width,thumbRect.height,null);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*绘制刻度轨迹  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidpaintTrack(Graphicsg)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intcy,cw;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)RectangletrackBounds=trackRect;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(slider.getOrientation()==JSlider.HORIZONTAL)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Graphics2Dg2=(Graphics2D)g;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cy=(trackBounds.height/2)-2;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cw=trackBounds.width;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g2.setRenderingHint(RenderingHints.KEY\_ANTIALIASING,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)RenderingHints.VALUE\_ANTIALIAS\_ON);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g2.translate(trackBounds.x,trackBounds.y+cy);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//背景设为灰色  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g2.setPaint(Color.GRAY);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g2.fillRect(0,-cy,cw,cy\*2);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)inttrackLeft=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)inttrackRight=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)trackRight=trackRect.width-1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intmiddleOfThumb=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intfillLeft=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intfillRight=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//坐标换算  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)middleOfThumb=thumbRect.x+(thumbRect.width/2);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)middleOfThumb-=trackRect.x;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(!drawInverted())...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)fillLeft=!slider.isEnabled()?trackLeft:trackLeft+1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)fillRight=middleOfThumb;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}else...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)fillLeft=middleOfThumb;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)fillRight=!slider.isEnabled()?trackRight-1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif):trackRight-2;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设定渐变  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g2.setPaint(newGradientPaint(0,0,newColor(0,100,100),cw,0,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)newColor(0,255,100),true));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g2.fillRect(0,-cy,fillRight-fillLeft,cy\*2);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g2.setPaint(slider.getBackground());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Polygonpolygon=newPolygon();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)polygon.addPoint(0,cy);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)polygon.addPoint(0,-cy);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)polygon.addPoint(cw,-cy);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g2.fillPolygon(polygon);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)polygon.reset();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g2.setPaint(Color.WHITE);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g2.drawLine(0,cy,cw-1,cy);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g2.setRenderingHint(RenderingHints.KEY\_ANTIALIASING,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)RenderingHints.VALUE\_ANTIALIAS\_OFF);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g2.translate(-trackBounds.x,-(trackBounds.y+cy));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}else...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)super.paintTrack(g);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

效果图：  
![](http://p.blog.csdn.net/images/p_blog_csdn_net/cping1982/Slider.gif)