下面列出了java.awt.Adjustable#HORIZONTAL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public int getScrollDirection() {
int sp = (orientation == Adjustable.HORIZONTAL)
? (int) getScrollPosition().getX()
: (int) getScrollPosition().getY();
Point pnt = SwingUtilities.convertPoint(comp, x, y, ((Container) getSource()).getComponents()[0]);
int cp = (orientation == Adjustable.HORIZONTAL)
? pnt.x
: pnt.y;
int sl = (orientation == Adjustable.HORIZONTAL)
? (int) getViewportSize().getWidth()
: (int) getViewportSize().getHeight();
int cl = (orientation == Adjustable.HORIZONTAL)
? width
: height;
if (cp <= sp) {
return ScrollAdjuster.DECREASE_SCROLL_DIRECTION;
} else if ((cp + cl) > (sp + sl)
&& cp > sp) {
return ScrollAdjuster.INCREASE_SCROLL_DIRECTION;
} else {
return ScrollAdjuster.DO_NOT_TOUCH_SCROLL_DIRECTION;
}
}
@Override
protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
int x = (int) trackBounds.getX();
int y = (int) trackBounds.getY();
int w = (int) trackBounds.getWidth();
int h = (int) trackBounds.getHeight();
g.setColor(Colors.SCROLLBAR_TRACK_BACKGROUND);
g.fillRect(x - 1, y - 1, w + 2, h + 2);
g.setColor(Colors.SCROLLBAR_TRACK_BORDER);
if (this.scrollbar.getOrientation() == Adjustable.HORIZONTAL) {
g.drawLine(x, y, x + w, y);
} else {
g.drawLine(x, y, x, y + h);
}
}
@Override
protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
int x = (int) thumbBounds.getX();
int y = (int) thumbBounds.getY();
int w = (int) thumbBounds.getWidth();
int h = (int) thumbBounds.getHeight();
if (c.isEnabled() && w > 0 && h > 0) {
if (this.scrollbar.getOrientation() == Adjustable.HORIZONTAL) {
h -= 1;
y++;
drawHorizThumb(g, x, y, w, h);
} else {
w -= 1;
x++;
drawVertThumb(g, x, y, w, h);
}
}
}
private static int getButton(int direction, int orientation) {
if (direction == ScrollAdjuster.DECREASE_SCROLL_DIRECTION) {
return (orientation == Adjustable.HORIZONTAL) ? KeyEvent.VK_LEFT : KeyEvent.VK_DOWN;
} else {
return (orientation == Adjustable.HORIZONTAL) ? KeyEvent.VK_RIGHT : KeyEvent.VK_UP;
}
}
/**
* Tells if a scrollbar is visible.
*
* @param orientation {@code Adjustable.HORIZONTAL} or
* {@code Adjustable.VERTICAL}
* @return trus if the bar is visible.
*/
public boolean isScrollbarVisible(int orientation) {
if (orientation == Adjustable.HORIZONTAL) {
return getViewportSize().getHeight() < getHeight() - getHScrollbarHeight();
} else if (orientation == Adjustable.VERTICAL) {
return getViewportSize().getWidth() < getWidth() - getVScrollbarWidth();
} else {
return false;
}
}
@Override
protected JButton createDecreaseButton(int orientation) {
if (this.scrollbar.getOrientation() == Adjustable.HORIZONTAL) {
this.decreaseButton = new GenericArrowButton(orientation, this.scrollbar.getHeight(), SCROLLBAR_WIDTH - 1);
} else {
this.decreaseButton = new GenericArrowButton(orientation, SCROLLBAR_WIDTH - 1, this.scrollbar.getWidth());
}
return this.decreaseButton;
}
@Override
protected JButton createIncreaseButton(int orientation) {
if (this.scrollbar.getOrientation() == Adjustable.HORIZONTAL) {
this.increaseButton = new GenericArrowButton(orientation, this.scrollbar.getHeight(), SCROLLBAR_WIDTH - 1);
} else {
this.increaseButton = new GenericArrowButton(orientation, SCROLLBAR_WIDTH - 1, this.scrollbar.getWidth());
}
return this.increaseButton;
}
private Point increasePoint(ComponentOperator oper, Point pnt, ScrollAdjuster adj, int direction) {
return ((adj.getScrollOrientation() == Adjustable.HORIZONTAL)
? new Point(pnt.x + ((direction == 1) ? 1 : -1) * getDragAndDropStepLength(oper), pnt.y)
: new Point(pnt.x, pnt.y + ((direction == 1) ? 1 : -1) * getDragAndDropStepLength(oper)));
}
@Override
protected int position(ComponentOperator oper, int orientation) {
return (orientation == Adjustable.HORIZONTAL)
? ((ScrollPaneOperator) oper).getScrollPosition().x
: ((ScrollPaneOperator) oper).getScrollPosition().y;
}
@Override
public void adjustmentValueChanged(AdjustmentEvent evt) {
Adjustable source = evt.getAdjustable();
// getValueIsAdjusting() returns true if the user is currently
// dragging the scrollbar's knob and has not picked a final value
if (evt.getValueIsAdjusting()) {
// The user is dragging the knob
return;
} else {
((javax.swing.JScrollBar) source).getParent().repaint();
}
// Determine which scrollbar fired the event
int orient = source.getOrientation();
if (orient == Adjustable.HORIZONTAL) {
// Event from horizontal scrollbar
} else {
// Event from vertical scrollbar
}
// Determine the type of event
int type = evt.getAdjustmentType();
switch (type) {
case AdjustmentEvent.UNIT_INCREMENT:
// Scrollbar was increased by one unit
break;
case AdjustmentEvent.UNIT_DECREMENT:
// Scrollbar was decreased by one unit
break;
case AdjustmentEvent.BLOCK_INCREMENT:
// Scrollbar was increased by one block
break;
case AdjustmentEvent.BLOCK_DECREMENT:
// Scrollbar was decreased by one block
break;
case AdjustmentEvent.TRACK:
// The knob on the scrollbar was dragged
break;
}
// Get current value
//int value = evt.getValue();
}
public void adjustmentValueChanged(AdjustmentEvent evt) {
Adjustable source = evt.getAdjustable();
// getValueIsAdjusting() returns true if the user is currently
// dragging the scrollbar's knob and has not picked a final value
if (evt.getValueIsAdjusting()) {
// The user is dragging the knob
return;
} else {
((javax.swing.JScrollBar) source).getParent().repaint();
}
// Determine which scrollbar fired the event
int orient = source.getOrientation();
if (orient == Adjustable.HORIZONTAL) {
// Event from horizontal scrollbar
} else {
// Event from vertical scrollbar
}
// Determine the type of event
int type = evt.getAdjustmentType();
switch (type) {
case AdjustmentEvent.UNIT_INCREMENT:
// Scrollbar was increased by one unit
break;
case AdjustmentEvent.UNIT_DECREMENT:
// Scrollbar was decreased by one unit
break;
case AdjustmentEvent.BLOCK_INCREMENT:
// Scrollbar was increased by one block
break;
case AdjustmentEvent.BLOCK_DECREMENT:
// Scrollbar was decreased by one block
break;
case AdjustmentEvent.TRACK:
// The knob on the scrollbar was dragged
break;
}
// Get current value
//int value = evt.getValue();
}