0
我製作了自己的ScrollbarUI,並且它與JScrollbar完美協作。問題是我必須連接這個滾動條和一個JPanel來滾動它。我已經嘗試過使用JScrollPane,但是當我嘗試接近垂直滾動條時,它只是不會迴應。我試圖改變一些簡單的東西,如光標,寬度,高度,背景來測試如果用戶界面是問題,但也沒有工作。JScrollpane中的自定義滾動條// Java Swing
JPanel panel_1 = new JPanel();
JScrollPane scrollpane = new JScrollPane(panel_1);
scrollpane.setBorder(null);
scrollpane.getVerticalScrollBar().setUI(new MyScrollbarUI());
JTextArea txtrLorem = new JTextArea();
txtrLorem.setPreferredSize(new Dimension(400, 1500));
txtrLorem.setMaximumSize(new Dimension(400, 2147483647));
txtrLorem.setText("Lorem ipsum...");
panel_1.add(txtrLorem);
scrollpane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollpane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
GridBagConstraints gbc_panel_1 = new GridBagConstraints();
gbc_panel_1.gridwidth = 5;
gbc_panel_1.insets = new Insets(0, 0, 0, 5);
gbc_panel_1.fill = GridBagConstraints.BOTH;
gbc_panel_1.gridx = 1;
gbc_panel_1.gridy = 4;
add(scrollpane, gbc_panel_1);
JScrollBar scrollBar = new JScrollBar();
scrollBar.setPreferredSize(new Dimension(20, 96));
scrollBar.setMinimumSize(new Dimension(20, 5));
scrollBar.setMaximumSize(new Dimension(20, 32767));
scrollBar.setUI(new MyScrollbarUI());
GridBagConstraints gbc_scrollBar = new GridBagConstraints();
gbc_scrollBar.fill = GridBagConstraints.VERTICAL;
gbc_scrollBar.insets = new Insets(0, 0, 0, 5);
gbc_scrollBar.gridx = 6;
gbc_scrollBar.gridy = 4;
add(scrollBar, gbc_scrollBar);
JScrollBar有正確的設計,但JScrollPane的滾動條沒有。 這裏是UI:
公共類MyScrollbarUI擴展BasicScrollBarUI {
@Override
protected JButton createDecreaseButton(int orientation) {
JButton btnL = new JButton("");
btnL.setPressedIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/top_active.png")));
btnL.setBorderPainted(false);
btnL.setIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/top_on.png")));
btnL.setRolloverIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/top.png")));
btnL.setRolloverEnabled(true);
return btnL;
}
@Override
protected JButton createIncreaseButton(int orientation) {
JButton btnL = new JButton("");
btnL.setPressedIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/down_active.png")));
btnL.setBorderPainted(false);
btnL.setIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/down_on.png")));
btnL.setRolloverIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/down.png")));
btnL.setRolloverEnabled(true);
return btnL;
}
@Override
protected void paintDecreaseHighlight(Graphics g)
{
Insets insets = scrollbar.getInsets();
Rectangle thumbR = getThumbBounds();
g.setColor(new Color(137,144,144));
if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
int x = insets.left+decrButton.getWidth()/2-2;
int y = decrButton.getY() + decrButton.getHeight();
int w = 4;
int h = thumbR.y - y;
g.fillRect(x, y, w, h);
}
else {
int x, w;
if (scrollbar.getComponentOrientation().isLeftToRight()) {
x = decrButton.getX() + decrButton.getWidth();
w = thumbR.x - x;
} else {
x = thumbR.x + thumbR.width;
w = decrButton.getX() - x;
}
int y = insets.top;
int h = scrollbar.getHeight() - (insets.top + insets.bottom);
g.fillRect(x, y, w, h);
}
}
@Override
protected void paintIncreaseHighlight(Graphics g) {
Insets insets = scrollbar.getInsets();
Rectangle thumbR = getThumbBounds();
g.setColor(new Color(202,207,203));
if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
int x = insets.left+decrButton.getWidth()/2-2;
int y = thumbR.y;
int w = 4;
int h = incrButton.getY() - y;
g.fillRect(x, y, w, h);
}
}
@Override
protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds)
{
g.setColor(Color.WHITE);
g.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height);
paintDecreaseHighlight(g);
paintIncreaseHighlight(g);
}
@Override
protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)
{
if(thumbBounds.isEmpty() || !scrollbar.isEnabled()) {
return;
}
int w = 16;
int h = 16;
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.translate(thumbBounds.x, thumbBounds.y);
GradientPaint gr = new GradientPaint(2, 0, new Color(158,161,162), 18, 16, new Color(96,99,98));
g2.setPaint(gr);
g2.fill(new Ellipse2D.Double(2, 0, w-1, h-1));
g2.setPaint(new Color(203,207,203));
g2.fill(new Ellipse2D.Double(6, 4, 7, 7));
g.translate(-thumbBounds.x, -thumbBounds.y);
}
}
你應該張貼的[SSCCE](HTTP ://sscce.org)以獲得更快速的幫助 –
我嘗試了您所描述的內容,並且它沒有問題,所以您的問題在於在你沒有發佈的代碼中。順便說一下,我不明白你爲什麼需要實現一個用戶界面。您可以通過調用自定義UI將調用的非常相同的方法來改變JScrollBar的屬性和行爲,就像使用其他組件一樣,不需要實現UI。 – Holger
我添加了UI的代碼,因爲有可能在那裏找到問題。 – user3172673