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 JSlider

JSlider 類是一個組件,它可以讓用戶以圖形方式選擇一個值,有界區間內通過滑動旋鈕。

類聲明

以下是聲明 javax.swing.JSlider類:

public class JSlider extends JComponent implements SwingConstants, Accessible

字段域

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

  • protected ChangeEvent changeEvent -- 因爲事件的唯一(只讀)狀態是源屬性,只有一個ChangeEvent事件需要每個滑桿實例。

  • protected ChangeListener changeListener -- ChangeListener(沒有後綴)是我們添加監聽器滑塊模型。

  • protected int majorTickSpacing -- 數目之間的主要刻度標記 - 標記向上突破的次要刻度線的值。

  • protected int minorTickSpacing -- 數目的次要刻度線 - 主要刻度之間發生的小標記之間的值。

  • protected int orientation -- 滑動件是否是水平或垂直的默認值是水平的。

  • protected BoundedRangeModel sliderModel -- 該數據模型來處理數值的最大值,最小值,及滑塊的當前位置值。

  • protected boolean snapToTicks -- 如果爲true,旋鈕(和它所代表的數據值)解析到最接近用戶定位旋鈕旁的對勾。

類構造函數

S.N.

構造函數 & 描述

1

JSlider()
Creates a horizontal slider with the range 0 to 100 and an initial value of 50.

2

JSlider(BoundedRangeModel brm)
Creates a horizontal slider using the specified BoundedRangeModel.

3

JSlider(int orientation)
Creates a slider using the specified orientation with the range 0 to 100 and an initial value of 50.

4

JSlider(int min, int max)
Creates a horizontal slider using the specified min and max with an initial value equal to the average of the min plus max.

5

JSlider(int min, int max, int value)
Creates a horizontal slider using the specified min, max and value.

6

JSlider(int orientation, int min, int max, int value)
Creates a slider with the specified orientation and the specified minimum, maximum, and initial values.

類方法

S.N.

方法 & 描述

1

void addChangeListener(ChangeListener l)
Adds a ChangeListener to the slider.

2

protected ChangeListener createChangeListener()
Subclasses that want to handle ChangeEvents from the model differently can override this to return an instance of a custom ChangeListener implementation.

3

Hashtable createStandardLabels(int increment)
Creates a Hashtable of numerical text labels, starting at the slider minimum, and using the increment specified.

4

Hashtable createStandardLabels(int increment, int start)
Creates a Hashtable of numerical text labels, starting at the starting point specified, and using the increment specified.

5

protected void fireStateChanged()
Send a ChangeEvent, whose source is this JSlider, to all ChangeListeners that have registered interest in ChangeEvents.

6

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

7

ChangeListener[] getChangeListeners()
Returns an array of all the ChangeListeners added to this JSlider with addChangeListener().

8

int getExtent()
Returns the "extent" from the BoundedRangeModel.

9

boolean getInverted()
Returns true if the value-range shown for the slider is reversed.

10

Dictionary getLabelTable()
Returns the dictionary of what labels to draw at which values.

11

int getMajorTickSpacing()
This method returns the major tick spacing.

12

int getMaximum()
Returns the maximum value supported by the slider from the BoundedRangeModel.

13

int getMinimum()
Returns the minimum value supported by the slider from the BoundedRangeModel.

14

int getMinorTickSpacing()
This method returns the minor tick spacing.

15

BoundedRangeModel getModel()
Returns the BoundedRangeModel that handles the slider's three fundamental properties: minimum, maximum, value.

16

int getOrientation()
Return this slider's vertical or horizontal orientation.

17

boolean getPaintLabels()
Tells if labels are to be painted.

18

boolean getPaintTicks()
Tells if tick marks are to be painted.

19

boolean getPaintTrack()
Tells if the track (area the slider slides in) is to be painted.

20

boolean getSnapToTicks()
Returns true if the knob (and the data value it represents) resolve to the closest tick mark next to where the user positioned the knob.

21

SliderUI getUI()
Gets the UI object which implements the L&F for this component.

22

String getUIClassID()
Returns the name of the L&F class that renders this component.

23

int getValue()
Returns the slider's current value from the BoundedRangeModel.

24

boolean getValueIsAdjusting()
Returns the valueIsAdjusting property from the model.

25

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

26

void removeChangeListener(ChangeListener l)
Removes a ChangeListener from the slider.

27

void setExtent(int extent)
Sets the size of the range "covered" by the knob.

28

void setFont(Font font)
Sets the font for this component.

29

void setInverted(boolean b)
Specify true to reverse the value-range shown for the slider and false to put the value range in the normal order.

30

void setLabelTable(Dictionary labels)
Used to specify what label will be drawn at any given value.

31

void setMajorTickSpacing(int n)
This method sets the major tick spacing.

32

void setMaximum(int maximum)
Sets the slider's maximum value to maximum.

33

void setMinimum(int minimum)
Sets the slider's minimum value to minimum.

34

void setMinorTickSpacing(int n)
This method sets the minor tick spacing.

35

void setModel(BoundedRangeModel newModel)
Sets the BoundedRangeModel that handles the slider's three fundamental properties: minimum, maximum, value.

36

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

37

void setPaintLabels(boolean b)
Determines whether labels are painted on the slider.

38

void setPaintTicks(boolean b)
Determines whether tick marks are painted on the slider.

39

void setPaintTrack(boolean b)
Determines whether the track is painted on the slider.

40

void setSnapToTicks(boolean b)
Specifying true makes the knob (and the data value it represents) resolve to the closest tick mark next to where the user positioned the knob.

41

void setUI(SliderUI ui)
Sets the UI object which implements the L&F for this component.

42

void setValue(int n) Sets the slider's current value to n.

43

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

44

protected void updateLabelUIs()
Updates the UIs for the labels in the label table by calling updateUI on each label.

45

void updateUI()
Resets the UI property to a value from the current look and feel.

方法繼承

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

  • javax.swing.JComponent

  • java.awt.Container

  • java.awt.Component

  • java.lang.Object

JSlider 例子

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

SwingControlDemo.java

package com.yiibai.gui; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; 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.showSliderDemo(); } 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 showSliderDemo(){ headerLabel.setText("Control in action: JSlider"); JSlider slider= new JSlider(JSlider.HORIZONTAL,0,100,10); slider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { statusLabel.setText("Value : " + ((JSlider)e.getSource()).getValue()); } }); controlPanel.add(slider); mainFrame.setVisible(true); } }

編譯程序,使用命令提示符。到 D:/ > SWING 然後輸出以下命令。

D:SWING>javac comyiibaiguiSwingControlDemo.java

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

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

驗證下面的輸出

Swing