Swing自定义JScrollPane的滚动条设置,重写BasicScrollBarUI方法
Swing自定义JScrollPane的滚动条设置,重写BasicScrollBarUI方法
摘自:https://blog.csdn.net/qq_31635851/article/details/80986870
1.自定义BasicScrollBarUI类
1 import java.awt.AlphaComposite; 2 import java.awt.Color; 3 import java.awt.Dimension; 4 import java.awt.GradientPaint; 5 import java.awt.Graphics; 6 import java.awt.Graphics2D; 7 import java.awt.Rectangle; 8 import java.awt.RenderingHints; 9 10 11 import javax.swing.ImageIcon; 12 import javax.swing.JButton; 13 import javax.swing.JComponent; 14 import javax.swing.JScrollBar; 15 import javax.swing.plaf.basic.BasicScrollBarUI; 16 17 18 19 /** 20 21 * 自定义滚动条UI 22 23 * 24 25 * @author zsg 26 27 */ 28 29 public class DemoScrollBarUI extends BasicScrollBarUI { 30 31 @Override 32 protected void configureScrollBarColors() { 33 34 // 把手 35 36 // thumbColor = Color.GRAY; 37 38 // thumbHighlightColor = Color.BLUE; 39 40 // thumbDarkShadowColor = Color.BLACK; 41 42 // thumbLightShadowColor = Color.YELLOW; 43 44 // 滑道 45 46 trackColor = Color.black; 47 48 setThumbBounds(0, 0, 3, 10); 49 50 // trackHighlightColor = Color.GREEN; 51 52 } 53 54 /** 55 56 * 设置滚动条的宽度 57 58 */ 59 60 @Override 61 public Dimension getPreferredSize(JComponent c) { 62 63 // TODO Auto-generated method stub 64 65 c.setPreferredSize(new Dimension(40, 0)); 66 67 return super.getPreferredSize(c); 68 69 } 70 71 72 // 重绘滑块的滑动区域背景 73 74 public void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) { 75 76 Graphics2D g2 = (Graphics2D) g; 77 78 GradientPaint gp = null; 79 80 //判断滚动条是垂直的 还是水平的 81 82 if (this.scrollbar.getOrientation() == JScrollBar.VERTICAL) { 83 84 //设置画笔 85 86 gp = new GradientPaint(0, 0, new Color(80, 80, 80), 87 88 trackBounds.width, 0, new Color(80, 80, 80)); 89 90 } 91 92 if (this.scrollbar.getOrientation() == JScrollBar.HORIZONTAL) { 93 94 gp = new GradientPaint(0, 0, new Color(80, 80, 80), 95 96 trackBounds.height, 0, new Color(80, 80, 80)); 97 98 } 99 100 101 g2.setPaint(gp); 102 103 //填充Track 104 105 g2.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, 106 107 trackBounds.height); 108 109 //绘制Track的边框 110 /* g2.setColor(new Color(175, 155, 95)); 111 g2.drawRect(trackBounds.x, trackBounds.y, trackBounds.width - 1, 112 trackBounds.height - 1); 113 */ 114 115 if (trackHighlight == BasicScrollBarUI.DECREASE_HIGHLIGHT) 116 117 this.paintDecreaseHighlight(g); 118 119 if (trackHighlight == BasicScrollBarUI.INCREASE_HIGHLIGHT) 120 121 this.paintIncreaseHighlight(g); 122 123 } 124 125 126 @Override 127 protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) { 128 129 // 把绘制区的x,y点坐标定义为坐标系的原点 130 131 // 这句一定一定要加上啊,不然拖动就失效了 132 133 g.translate(thumbBounds.x, thumbBounds.y); 134 135 // 设置把手颜色 136 137 g.setColor(new Color( 230,230,250)); 138 139 // 画一个圆角矩形 140 141 // 这里面前四个参数就不多讲了,坐标和宽高 142 143 // 后两个参数需要注意一下,是用来控制角落的圆角弧度 144 145 // g.drawRoundRect(0, 0, 5, thumbBounds.height - 1, 5, 5); 146 147 // 消除锯齿 148 149 Graphics2D g2 = (Graphics2D) g; 150 151 RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, 152 153 RenderingHints.VALUE_ANTIALIAS_ON); 154 155 g2.addRenderingHints(rh); 156 157 // 半透明 158 159 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 160 161 0.5f)); 162 163 // 设置填充颜色,这里设置了渐变,由下往上 164 165 // g2.setPaint(new GradientPaint(c.getWidth() / 2, 1, Color.GRAY, 166 167 // c.getWidth() / 2, c.getHeight(), Color.GRAY)); 168 169 // 填充圆角矩形 170 171 g2.fillRoundRect(0, 0, 40, thumbBounds.height - 1, 5, 5); 172 173 } 174 175 176 /** 177 178 * 创建滚动条上方的按钮 179 180 */ 181 182 @Override 183 184 protected JButton createIncreaseButton(int orientation) { 185 186 JButton button = new JButton(); 187 188 button.setBorderPainted(false); 189 190 button.setContentAreaFilled(false); 191 192 button.setBorder(null); 193 194 return button; 195 196 } 197 198 /** 199 200 * 创建滚动条下方的按钮 201 202 */ 203 204 @Override 205 206 protected JButton createDecreaseButton(int orientation) { 207 208 JButton button = new JButton(); 209 210 button.setBorderPainted(false); 211 212 button.setContentAreaFilled(false); 213 214 button.setFocusable(false); 215 216 button.setBorder(null); 217 218 return button; 219 220 } 221 222 }
2.使用自定义类,覆盖JScrollPane滚动条样式
1 JScrollPane scrollPane = new JScrollPane(); 2 scrollPane.getVerticalScrollBar().setUI(new DemoScrollBarUI());
1 import java.awt.AlphaComposite; 2 import java.awt.Color; 3 import java.awt.Dimension; 4 import java.awt.GradientPaint; 5 import java.awt.Graphics; 6 import java.awt.Graphics2D; 7 import java.awt.GridLayout; 8 import java.awt.Rectangle; 9 import java.awt.RenderingHints; 10 11 import javax.swing.JButton; 12 import javax.swing.JComponent; 13 import javax.swing.JFrame; 14 import javax.swing.JPanel; 15 import javax.swing.JScrollBar; 16 import javax.swing.JScrollPane; 17 import javax.swing.WindowConstants; 18 import javax.swing.plaf.basic.BasicScrollBarUI; 19 20 /** 21 22 * 自定义滚动条UI 23 24 * 25 26 * @author zsg 27 28 */ 29 30 public class DemoScrollBarUI extends BasicScrollBarUI { 31 32 // 手柄宽度 33 private static final int thumbWidth = 40; 34 35 //手柄透明度 36 private static final float opaque = 0.8f; 37 // 手柄边框颜色 38 private static final Color thumbColor = new Color(51, 47, 154); 39 40 // 手柄颜色 41 private static final Color thumbColorFrom = new Color(51, 47, 154); 42 private static final Color thumbColorTo = new Color(51, 47, 154); 43 44 // 滑道颜色 45 private static final Color backColorFrom = new Color(255, 255, 255); 46 private static final Color backColorTo = new Color(255, 255, 255); 47 48 @Override 49 protected void configureScrollBarColors() { 50 51 // 把手 52 53 // thumbColor = Color.GRAY; 54 55 // thumbHighlightColor = Color.BLUE; 56 57 // thumbDarkShadowColor = Color.BLACK; 58 59 // thumbLightShadowColor = Color.YELLOW; 60 61 // 滑道 62 63 // trackColor = Color.black; 64 65 setThumbBounds(0, 0, 3, 10); 66 67 // trackHighlightColor = Color.GREEN; 68 69 } 70 71 /** 72 73 * 设置滚动条的宽度 74 75 */ 76 77 @Override 78 public Dimension getPreferredSize(JComponent c) { 79 80 // TODO Auto-generated method stub 81 82 // c.setPreferredSize(new Dimension(thumbWidth, 0)); 83 c.setPreferredSize(new Dimension(thumbWidth, thumbWidth)); 84 85 return super.getPreferredSize(c); 86 87 } 88 89 // 重绘滑块的滑动区域背景 90 91 public void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) { 92 93 Graphics2D g2 = (Graphics2D) g; 94 95 GradientPaint gp = null; 96 97 //判断滚动条是垂直的 还是水平的 98 99 if (this.scrollbar.getOrientation() == JScrollBar.VERTICAL) { 100 //设置画笔 101 // 颜色渐变 102 gp = new GradientPaint(0, 0, backColorFrom, 0, trackBounds.height, backColorTo); 103 104 } 105 106 if (this.scrollbar.getOrientation() == JScrollBar.HORIZONTAL) { 107 gp = new GradientPaint(0, 0, backColorFrom, trackBounds.width, 0, backColorTo); 108 } 109 110 g2.setPaint(gp); 111 112 //填充Track 113 114 g2.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height); 115 116 //绘制Track的边框 117 /* g2.setColor(new Color(175, 155, 95)); 118 g2.drawRect(trackBounds.x, trackBounds.y, trackBounds.width - 1, 119 trackBounds.height - 1); 120 */ 121 122 if (trackHighlight == BasicScrollBarUI.DECREASE_HIGHLIGHT) 123 this.paintDecreaseHighlight(g); 124 125 if (trackHighlight == BasicScrollBarUI.INCREASE_HIGHLIGHT) 126 this.paintIncreaseHighlight(g); 127 128 } 129 130 @Override 131 protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) { 132 133 // 把绘制区的x,y点坐标定义为坐标系的原点 134 135 // 这句一定一定要加上啊,不然拖动就失效了 136 137 g.translate(thumbBounds.x, thumbBounds.y); 138 139 // 设置把手颜色 140 141 // g.setColor(new Color(230, 230, 250)); 142 g.setColor(thumbColor); 143 144 // 画一个圆角矩形 145 146 // 这里面前四个参数就不多讲了,坐标和宽高 147 148 // 后两个参数需要注意一下,是用来控制角落的圆角弧度 149 150 // g.drawRoundRect(0, 0, 5, thumbBounds.height - 1, 5, 5); 151 g.drawRoundRect(0, 0, thumbBounds.width - 1, thumbBounds.height - 1, 5, 5); 152 153 // 消除锯齿 154 155 Graphics2D g2 = (Graphics2D) g; 156 157 RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 158 159 g2.addRenderingHints(rh); 160 161 // 半透明 162 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opaque)); 163 164 // 设置填充颜色,这里设置了渐变,由下往上 165 g2.setPaint(new GradientPaint(c.getWidth() / 2, 1, thumbColorFrom, c.getWidth() / 2, c.getHeight(), 166 thumbColorTo)); 167 168 // 填充圆角矩形 169 // g2.fillRoundRect(0, 0, thumbWidth, thumbBounds.height - 1, 5, 5); 170 g2.fillRoundRect(0, 0, thumbBounds.width - 1, thumbBounds.height - 1, 5, 5); 171 172 } 173 174 /** 175 176 * 创建滚动条上方的按钮 177 178 */ 179 180 @Override 181 protected JButton createIncreaseButton(int orientation) { 182 183 JButton button = new JButton(); 184 185 button.setBorderPainted(true); 186 187 button.setContentAreaFilled(true); 188 189 // 设置为null, 禁止上方按钮 190 button.setBorder(null); 191 192 return button; 193 194 } 195 196 /** 197 198 * 创建滚动条下方的按钮 199 200 */ 201 202 @Override 203 protected JButton createDecreaseButton(int orientation) { 204 205 JButton button = new JButton(); 206 207 button.setBorderPainted(true); 208 209 button.setContentAreaFilled(true); 210 211 button.setFocusable(false); 212 213 // 设置为null, 禁止上方按钮 214 button.setBorder(null); 215 216 return button; 217 218 } 219 220 public static void main(String[] args) { 221 222 JFrame jf = new JFrame("测试窗口"); 223 jf.setSize(1500, 800); 224 jf.setLocationRelativeTo(null); 225 jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 226 227 JPanel jp = new JPanel(); 228 229 GridLayout gridLayout = new GridLayout(4, 0, 0, 0); 230 jp.setLayout(gridLayout); 231 232 for (int index = 0; index < 1000; index++) { 233 jp.add(new JButton("asssssssssssssssssssssssssssssssssssss")); 234 } 235 236 JScrollPane scrollPane = new JScrollPane(); 237 // scrollPane.getVerticalScrollBar().setUI(new DemoScrollBarUI()); 238 scrollPane.getHorizontalScrollBar().setUI(new DemoScrollBarUI()); 239 240 // scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 241 // scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 242 243 scrollPane.setViewportView(jp); 244 245 jf.add(scrollPane); 246 247 jf.setVisible(true); 248 249 } 250 }