Swing
Swing教學首頁
Swing介紹
Swing開發環境安裝
Swing控件
Swing Component類
Swing Container類
Swing JComponent類
Swing JLabel
Swing JButton
Swing JColorChooser
Swing JCheckBox
Swing JRadioButton
Swing JList
Swing JComboBox
Swing JTextField
Swing JTextArea
Swing ImageIcon
Swing JScrollBar
Swing JOptionPane
Swing JFileChooser
Swing JProgressBar
Swing JSlider
Swing JSpinner
Swing事件處理
SWING Event事件類
SWING AWTEvent事件類
SWING ActionEvent事件類
SWING InputEvent事件類
SWING KeyEvent事件類
SWING MouseEvent事件類
SWING WindowEvent事件類
SWING AdjustmentEvent事件處理
SWING ComponentEvent處理類
SWING ContainerEvent事件處理類
SWING MouseMotionEvent事件處理
SWING PaintEvent事件類
Swing事件監聽器
SWING ActionListener接口
SWING ComponentListener接口
Swing ItemListener接口
Swing KeyListener接口
Swing MouseListener Interface
Swing WindowListener接口
Swing AdjustmentListener接口
Swing ContainerListener接口
Swing MouseMotionListener接口
Swing FocusListener接口
Swing事件適配器
Swing FocusAdapter類
Swing KeyAdapter類
Swing MouseAdapter類
Swing WindowAdapter類
Swing MouseMotionAdapter類
Swing Layout佈局
Swing LayoutManager接口
Swing LayoutManager2接口
Swing BorderLayout佈局
Swing CardLayout佈局
Swing FlowLayout佈局類
Swing GridLayout佈局類
Swing GridBagLayout佈局類
Swing GroupLayout佈局類
Swing SpringLayout佈局類
Swing Menu菜單類
Swing JMenuBar類
Swing JMenuItem類
Swing JMenu類
Swing JCheckboxMenuItem類及例子
Swing JRadioButtonMenuItem類及例子
Swing JPopupMenu類及實例
Swing容器
Swing JPanel類及實例
Swing JFrame類和實例
Swing JWindow類及實例

Swing JScrollBar

JScrollBar類實現滾動。

類聲明

以下是javax.swing.JScrollBar類的聲明:

public class JScrollBar extends JComponent implements Adjustable, Accessible

字段域

以下是javax.swing.ImageIcon類的字段:

  • protected int blockIncrement

  • protected BoundedRangeModel model -- 模型表示滾動條的最小,最大程度(又名「可視部分」)和當前值。

  • protected int orientation

  • protected int unitIncrement

類構造函數

S.N.

構造函數 & 描述

1

JScrollBar()
Creates a vertical scrollbar with the initial values.

2

JScrollBar(int orientation)
Creates a scrollbar with the specified orientation and the initial values.

3

JScrollBar(int orientation, int value, int extent, int min, int max)
Creates a scrollbar with the specified orientation, value, extent, minimum, and maximum.

類方法

S.N.

方法 & 描述

1

void addAdjustmentListener(AdjustmentListener l)
Adds an AdjustmentListener.

2

protected void fireAdjustmentValueChanged(int id, int type, int value)
Notify listeners that the scrollbar's model has changed.

3

AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JScrollBar.

4

AdjustmentListener[] getAdjustmentListeners()
Returns an array of all the AdjustmentListeners added to this JScrollBar with addAdjustmentListener().

5

int getBlockIncrement()
For backwards compatibility with java.awt.Scrollbar.

6

int getBlockIncrement(int direction)
Returns the amount to change the scrollbar's value by, given a block (usually "page") up/down request.

7

int getMaximum()
The maximum value of the scrollbar is maximum - extent.

8

Dimension getMaximumSize()
The scrollbar is flexible along it's scrolling axis and rigid along the other axis.

9

int getMinimum()
Returns the minimum value supported by the scrollbar (usually zero).

10

Dimension getMinimumSize()
The scrollbar is flexible along it's scrolling axis and rigid along the other axis.

11

BoundedRangeModel getModel()
Returns data model that handles the scrollbar's four fundamental properties: minimum, maximum, value, extent.

12

int getOrientation()
Returns the component's orientation (horizontal or vertical).

13

ScrollBarUI getUI()
Returns the delegate that implements the look and feel for this component.

14

String getUIClassID()
Returns the name of the LookAndFeel class for this component.

15

int getUnitIncrement()
For backwards compatibility with java.awt.Scrollbar.

16

int getUnitIncrement(int direction)
Returns the amount to change the scrollbar's value by, given a unit up/down request.

17

int getValue()
Returns the scrollbar's value.

18

boolean getValueIsAdjusting()
True if the scrollbar knob is being dragged.

19

int getVisibleAmount()
Returns the scrollbar's extent, aka its "visibleAmount".

20

protected String paramString()
Returns a string representation of this JScrollBar.

21

void removeAdjustmentListener(AdjustmentListener l)
Removes an AdjustmentEvent listener.

22

void setBlockIncrement(int blockIncrement)
Sets the blockIncrement property.

23

void setEnabled(boolean x)
Enables the component so that the knob position can be changed.

24

void setMaximum(int maximum)
Sets the model's maximum property.

25

void setMinimum(int minimum)
Sets the model's minimum property.

26

void setModel(BoundedRangeModel newModel)
Sets the model that handles the scrollbar's four fundamental properties: minimum, maximum, value, extent.

27

void setOrientation(int orientation)
Set the scrollbar's orientation to either VERTICAL or HORIZONTAL.

28

void setUI(ScrollBarUI ui)
Sets the L&F object that renders this component.

29

void setUnitIncrement(int unitIncrement)
Sets the unitIncrement property.

30

void setValue(int value)
Sets the scrollbar's value.

31

void setValueIsAdjusting(boolean b)
Sets the model's valueIsAdjusting property.

32

void setValues(int newValue, int newExtent, int newMin, int newMax)
Sets the four BoundedRangeModel properties after forcing the arguments to obey the usual constraints.

33

void setVisibleAmount(int extent)
Set the model's extent property.

34

void updateUI()
Overrides JComponent.updateUI.

方法繼承

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

  • java.lang.Object

JScrollBar 例子

選擇使用任何編輯器創建以下java程序在 D:/ > SWING > com > yiibai > gui >

SwingControlDemo.java

package com.yiibai.gui; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingControlDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingControlDemo(){ prepareGUI(); } public static void main(String[] args){ SwingControlDemo swingControlDemo = new SwingControlDemo(); swingControlDemo.showScrollbarDemo(); } private void prepareGUI(){ mainFrame = new JFrame("Java Swing 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 JLabel("", JLabel.CENTER); statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showScrollbarDemo(){ headerLabel.setText("Control in action: JScrollbar"); final JScrollBar horizontalScroller = new JScrollBar(JScrollBar.HORIZONTAL); final JScrollBar verticalScroller = new JScrollBar(); verticalScroller.setOrientation(JScrollBar.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:/ > SWING 然後輸出以下命令。

D:SWING>javac comyiibaiguiSwingControlDemo.java

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

D:SWING>java com.yiibai.gui.SwingControlDemo

驗證下面的輸出

Swing