Java开发GUI之滚动条
滚动条组件可以实现用户拖动调整效果,示例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12
| static void ScrollBarTest(){ Frame frame = new Frame("Label"); Panel pannel = new Panel(); Scrollbar scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 5, 2, 0, 20); scrollbar.setUnitIncrement(1); scrollbar.setBlockIncrement(5); scrollbar.addAdjustmentListener(new ScrollBarListener()); pannel.add(scrollbar); frame.add(pannel); frame.pack(); frame.show(); }
|
AdjustmentListener用来监听滚动条的值变化,其实现如下:
1 2 3 4 5 6 7 8 9
| class ScrollBarListener implements AdjustmentListener{
@Override public void adjustmentValueChanged(AdjustmentEvent e) { System.out.println(e.getValue()); } }
|
ScrollBar组件中常用方法列举如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| public Scrollbar();
public Scrollbar(int orientation);
public Scrollbar(int orientation, int value, int visible, int minimum, int maximum);
public int getOrientation();
public void setOrientation(int orientation);
public int getValue();
public void setValue(int newValue);
public int getMinimum();
public void setMinimum(int newMinimum);
public int getMaximum();
public void setMaximum(int newMaximum);
public int getVisibleAmount();
public int getVisible();
public void setVisibleAmount(int newAmount);
public void setUnitIncrement(int v);
public int getUnitIncrement();
public void setBlockIncrement(int v);
public int getBlockIncrement() ;
public void setValues(int value, int visible, int minimum, int maximum);
public boolean getValueIsAdjusting();
public void setValueIsAdjusting(boolean b);
public synchronized void addAdjustmentListener(AdjustmentListener l) ;
public synchronized void removeAdjustmentListener(AdjustmentListener l);
public synchronized AdjustmentListener[] getAdjustmentListeners();
|