AWT Scrollbar類


介紹

捲動條控制捲動條元件以讓使用者選擇一個範圍內的值。

類的宣告

以下是的宣告類java.awt.Scrollbar:

public class Scrollbar
   extends Component
      implements Adjustable, Accessible

欄位域

以下java.awt.image類的欄位:

  • static int HORIZONTAL -- 一個常數,表示一個水平捲動條。

  • static int VERTICAL -- 一個常數,表示一個垂直捲動條。

類別建構函式

S.N. 建構函式&說明
1 Scrollbar() 
Constructs a new vertical scroll bar.
2 Scrollbar(int orientation) 
Constructs a new scroll bar with the specified orientation.
3 Scrollbar(int orientation, int value, int visible, int minimum, int maximum) 
Constructs a new scroll bar with the specified orientation, initial value, visible amount, and minimum and maximum values.

類方法

AccessibleContext getAccessibleContext()
獲取的AccessibleContext和這個捲動條。 AdjustmentListener[] getAdjustmentListeners() 
返回一個陣列,在這個捲動條調整監聽器註冊。 <T extends EventListener>T[] getListeners(Class<T> listenerType) 
返回一個陣列,目前註冊作為FooListeners在這捲動的所有物件。
S.N. 方法& 描述
1 void addAdjustmentListener(AdjustmentListener l) 
Adds the specified adjustment listener to receive instances of AdjustmentEvent from this scroll bar.
2 void addNotify() 
Creates the Scrollbar's peer.
3 int getBlockIncrement() 
Gets the block increment of this scroll bar.
4 int getLineIncrement() 
Deprecated. As of JDK version 1.1, replaced by getUnitIncrement().
5 int getMaximum() 
Gets the maximum value of this scroll bar.
6 int getMinimum() 
Gets the minimum value of this scroll bar.
7 int getOrientation()
Returns the orientation of this scroll bar.
8 int getPageIncrement() 
Deprecated. As of JDK version 1.1, replaced by getBlockIncrement().
9 int getUnitIncrement()
Gets the unit increment for this scrollbar.
10 int getValue() 
Gets the current value of this scroll bar.
11 boolean
getValueIsAdjusting() 
Returns true if the value is in the process of changing as a result of actions being taken by the user.
12 int getVisible() 
Deprecated. As of JDK version 1.1, replaced by getVisibleAmount().
13 int getVisibleAmount() 
Gets the visible amount of this scroll bar.
14 protected String paramString() 
Returns a string representing the state of this Scrollbar.
15 protected void processAdjustmentEvent(AdjustmentEvent e) 
Processes adjustment events occurring on this scrollbar by dispatching them to any registered AdjustmentListener objects.
16 protected
1 void processEvent(AWTEvent e) 
Processes events on this scroll bar.
17 void removeAdjustmentListener(AdjustmentListener l) 
Removes the specified adjustment listener so that it no longer receives instances of AdjustmentEvent from this scroll bar.
18 void setBlockIncrement(int v) 
Sets the block increment for this scroll bar.
19 void setLineIncrement(int v) 
Deprecated. As of JDK version 1.1, replaced by setUnitIncrement(int).
20 void setMaximum(int newMaximum) 
Sets the maximum value of this scroll bar.
21 void setMinimum(int newMinimum) 
Sets the minimum value of this scroll bar.
22 void setOrientation(int orientation) 
Sets the orientation for this scroll bar.
23 void setPageIncrement(int v) 
Deprecated. As of JDK version 1.1, replaced by setBlockIncrement().
24 void setUnitIncrement(int v) 
Sets the unit increment for this scroll bar.
25 void setValue(int newValue) 
Sets the value of this scroll bar to the specified value.
26 void setValueIsAdjusting(boolean b) 
Sets the valueIsAdjusting property.
27 void setValues(int value, int visible, int minimum, int maximum) 
Sets the values of four properties for this scroll bar: value, visibleAmount, minimum, and maximum.
28 void setVisibleAmount(int newAmount) 
Sets the visible amount of this scroll bar.

繼承的方法

這個類繼承的方法從以下類:

  • java.awt.Component

  • java.lang.Object

選擇範例

選擇使用任何編輯器建立以下java程式 D:/ > AWT > com > yiibai > gui >

AwtControlDemo
package com.yiibai.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showScrollbarDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showScrollbarDemo(){                                       
      headerLabel.setText("Control in action: Scrollbar"); 

      final Scrollbar horizontalScroller = new Scrollbar(Scrollbar.HORIZONTAL);
      final Scrollbar verticalScroller = new Scrollbar();
      verticalScroller.setOrientation(Scrollbar.VERTICAL);
      horizontalScroller.setMaximum (100);
      horizontalScroller.setMinimum (1);
      verticalScroller.setMaximum (100);
      verticalScroller.setMinimum (1);

      horizontalScroller.addAdjustmentListener(new AdjustmentListener() {

         @Override
         public void adjustmentValueChanged(AdjustmentEvent e) {
            statusLabel.setText("Horozontal: "
               +horizontalScroller.getValue() 
               +" ,Vertical: "
               + verticalScroller.getValue());
            }
         });

      verticalScroller.addAdjustmentListener(new AdjustmentListener() {

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
               statusLabel.setText("Horozontal: "
               +horizontalScroller.getValue() 
               +" ,Vertical: "+ verticalScroller.getValue());
            }
         });

      controlPanel.add(horizontalScroller);
      controlPanel.add(verticalScroller);

      mainFrame.setVisible(true);  
   }
}

編譯程式,使用命令提示字元。到 D:/ > AWT 然後鍵入以下命令。

D:AWT>javac comyiibaiguiAwtControlDemo.java

如果沒有錯誤出現,這意味著編譯成功。使用下面的命令來執行程式。

D:AWT>java com.yiibai.gui.AwtControlDemo

驗證下面的輸出

Image Button