The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
A JSlider
component is intended to let the user easily enter a numeric value bounded by a minimum and maximum value. JSlider
组件旨在让用户轻松输入由最小值和最大值限定的数值。If space is limited, a spinner is a possible alternative to a slider.如果空间有限,微调器是滑块的可能替代方案。
The following picture shows an application that uses a slider to control animation speed:下图显示了使用滑块控制动画速度的应用程序:
Below is the code from the 下面是在前面示例中创建滑块的SliderDemo.java
file that creates the slider in the previous example.SliderDemo.java
文件中的代码。
static final int FPS_MIN = 0; static final int FPS_MAX = 30; static final int FPS_INIT = 15; //initial frames per second . . . JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL, FPS_MIN, FPS_MAX, FPS_INIT); framesPerSecond.addChangeListener(this); //Turn on labels at major tick marks. framesPerSecond.setMajorTickSpacing(10); framesPerSecond.setMinorTickSpacing(1); framesPerSecond.setPaintTicks(true); framesPerSecond.setPaintLabels(true);
By default, spacing for major and minor tick marks is zero. 默认情况下,主刻度线和次刻度线的间距为零。To see tick marks, you must explicitly set the spacing for either major or minor tick marks (or both) to a non-zero value and call the 要查看刻度线,必须将主刻度线或次刻度线(或两者)的间距显式设置为非零值,并调用setPaintTicks(true)
method. setPaintTicks(true)
方法。However, you also need labels for your tick marks. 但是,您还需要标记记号的标签。To display standard, numeric labels at major tick mark locations, set the major tick spacing, then call the 要在主刻度标记位置显示标准数字标签,请设置主刻度间距,然后调用setPaintLabels(true)
method. setPaintLabels(true)
方法。The example program provides labels for its slider in this way. 示例程序以这种方式为其滑块提供标签。But you are not constrained to using only these labels. 但您不限于仅使用这些标签。Customizing Labels on a Slider shows you how to customize slider labels. 自定义滑块上的标签显示了如何自定义滑块标签。In addition, a slider feature allows you to set a font for the 此外,滑块功能允许您为JSlider
component.JSlider
组件设置字体。
Font font = new Font("Serif", Font.ITALIC, 15); framesPerSecond.setFont(font);
When you move the slider's knob, the 移动滑块的旋钮时,将调用滑块的stateChanged
method of the slider's ChangeListener
is called. ChangeListener
的stateChanged
方法。For information about change listeners, refer to How to Write a Change Listener. 有关更改侦听器的信息,请参阅如何编写更改侦听器。Here is the change listener code that reacts to slider value changes:以下是对滑块值更改做出反应的更改侦听器代码:
public void stateChanged(ChangeEvent e) { JSlider source = (JSlider)e.getSource(); if (!source.getValueIsAdjusting()) { int fps = (int)source.getValue(); if (fps == 0) { if (!frozen) stopAnimation(); } else { delay = 1000 / fps; timer.setDelay(delay); timer.setInitialDelay(delay * 10); if (frozen) startAnimation(); } } }
Notice that the 请注意,只有当stateChanged
method changes the animation speed only if the getValueIsAdjusting
method returns false
. getValueIsAdjusting
方法返回false
时,stateChanged
方法才会更改动画速度。Many change events are fired as the user moves the slider knob. 当用户移动滑块旋钮时,会触发许多更改事件。This program is interested only in the final result of the user's action.该程序只对用户操作的最终结果感兴趣。
The demo below is a modified version of the SliderDemo that uses a slider with custom labels:下面的演示是SliderDemo的修改版本,使用带有自定义标签的滑块:
The source for this program can be found in 该程序的源代码可以在SliderDemo2.java
. SliderDemo2.java
中找到。Click the Launch button to run SliderDemo2 using Java™ Web Start (download JDK 7 or later). 单击启动按钮,使用Java™Web启动运行SliderDemo2(下载JDK 7或更高版本)。Alternatively, to compile and run the example yourself, consult the example index.或者,要自己编译和运行示例,请参考示例索引。
The following code creates the slider and customizes its labels:以下代码创建滑块并自定义其标签:
//Create the slider JSlider framesPerSecond = new JSlider(JSlider.VERTICAL, FPS_MIN, FPS_MAX, FPS_INIT); framesPerSecond.addChangeListener(this); framesPerSecond.setMajorTickSpacing(10); framesPerSecond.setPaintTicks(true); //Create the label table Hashtable labelTable = new Hashtable(); labelTable.put( new Integer( 0 ), new JLabel("Stop") ); labelTable.put( new Integer( FPS_MAX/10 ), new JLabel("Slow") ); labelTable.put( new Integer( FPS_MAX ), new JLabel("Fast") ); framesPerSecond.setLabelTable( labelTable ); framesPerSecond.setPaintLabels(true);
Each key-value pair in the hashtable specified with the 使用setLabelTable
method gives the position and the value of one label. setLabelTable
方法指定的哈希表中的每个键值对都给出一个标签的位置和值。The hashtable key must be of an 哈希表键必须是Integer
type and must have a value within the slider's range at which to place the label. Integer
类型,并且必须在滑块的范围内有一个值来放置标签。The hashtable value associated with each key must be a 与每个键关联的哈希表值必须是Component
object. Component
对象。This demo uses 此演示使用仅包含文本的JLabel
instances with text only. JLabel
实例。An interesting modification would be to use 一个有趣的修改是使用带有图标或按钮的JLabel
instances with icons or buttons that move the knob to the label's position.JLabel
实例,将旋钮移动到标签的位置。
Use the 使用createStandardLabels
method of the JSlider
class to create a set of numeric labels positioned at a specific interval. JSlider
类的createStandardLabels
方法创建一组以特定间隔定位的数字标签。You can also modify the table returned by the 您还可以修改createStandardLabels
method in order to customize it.createStandardLabels
方法返回的表,以便对其进行自定义。
The following tables list the commonly used 下表列出了常用的JSlider
constructors and methods. JSlider
构造函数和方法。See The JComponent Class for tables of commonly used inherited methods.有关常用继承方法的表,请参阅JComponent
类。
The API for using sliders is divided into these categories:使用滑块的API分为以下几类:
JSlider() |
|
JSlider(int min, int max) JSlider(int min, int max, int value) |
int argument, when present, specifies the slider's initial value.int 参数(如果存在)指定滑块的初始值。 |
JSlider(int orientation) JSlider(int orientation, int min, int max, int value) |
JSlider.HORIZONTAL or JSlider.VERTICAL . JSlider.HORIZONTAL 或JSlider.VERTICAL 。int arguments, when present, specify the slider's minimum, maximum, and initial values, respectively.int 参数(如果存在)分别指定滑块的最小值、最大值和初始值。 |
JSlider(BoundedRangeModel) |
void setValue(int) int getValue() |
|
void setOrientation(int) int getOrientation() |
JSlider.HORIZONTAL or JSlider.VERTICAL .JSlider.HORIZONTAL 或JSlider.VERTICAL 。 |
void setInverted(boolean) boolean getInverted() |
|
void setMinimum(int) int getMinimum() void setMaximum(int) int getMaximum() |
|
void setMajorTickSpacing(int) int getMajorTickSpacing() void setMinorTickSpacing(int) int getMinorTickSpacing() |
setPaintTicks(true) for the tick marks to appear.setPaintTicks(true) 才能显示记号。 |
void setPaintTicks(boolean) boolean getPaintTicks() |
|
void setPaintLabels(boolean) boolean getPaintLabels() |
setLabelTable or get automatic labels by setting the major tick spacing to a non-zero value.setLabelTable 的自定义标签,或者通过将主刻度间距设置为非零值来获取自动标签。 |
void setLabelTable(Dictionary) Dictionary getLabelTable() |
setPaintLabels(true) for the labels to appear.setPaintLabels(true) 才能显示标签。 |
Hashtable createStandardLabels(int) Hashtable createStandardLabels(int, int) |
int argument specifies the increment, the second int argument specifies the starting point. int 参数指定增量,第二个int 参数指明起点。 |
setFont(java.awt.Font) |
void addChangeListener(ChangeListener) |
|
boolean getValueIsAdjusting() |
BoundedRangeModel |
|
DefaultBoundedRangeModel |
BoundedRangeModel interface.BoundedRangeModel 接口的实现。 |
void setModel() getModel() (in JSlider ) |
BoundedRangeModel .BoundedRangeModel 类型的单个参数。 |
This table shows the examples that use 下表显示了使用JSlider
and where those examples are described.JSlider
的示例以及这些示例的描述位置。
SliderDemo |
||
SliderDemo2 |
||
Converter |
BoundedRangeModel s.BoundedRangeModel 的滑块。 |
If you are programming in JavaFX, see Slider.如果您使用JavaFX编程,请参阅滑块。