實現起來還是非常簡單的,首先是從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); } }; } }
代碼: package MyJava.Base; import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.text.*;impo
1.Swing與Awt的最大的區別: Swing爲急於窗體的GUI應用開發設計,爲java跨平臺特性提供了卓越的支持.它完全沒有本地代碼,不受操作系統的影響,做到了真正的跨平臺應用,甚至能夠提供本地窗口系統不支持的其它特性.因此比AWT具
代碼: package MyJava.Base; import java.awt.*;import java.awt.event.*;import javax.swing.*; class JRadioButtonFrame extend
代碼: package MyJava.Base; import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*; class JList
程序示例: package MyJava.Base; import java.awt.*;import java.awt.event.*;import javax.swing.*; class JToggleButtonFrame ext
/** * 方式一:通過添加鍵盤事件,判斷鍵入的是否爲數字 aacc * 不足:通過 複製粘貼 的方式可以寫入英文字母 */ JTextField textField = new JTextField(); textField.ad
SetUndecorated(true); SetBackground(new Color(255, 255, 255, 125); SetUndecorated 源碼中的註解: This method can only be
Java Swing程序不像J2EE程序那樣有框架有模式可遵,反倒顯得很自由無所拘束,但如些一來反而顯得程序代碼雜亂無章,如果只是寫個示例程序的話倒也無所謂,但隨着代碼的增多或過段時間在回首時就發現代碼很不容易看懂,不以維護。我雖無大型S