Java Swing JSlider: Slider component

In the previous chapters, we introduced the windows and layout components required to design a simple interface in Swing, and how to respond to events. Swing also provides many advanced components, such as menu bars, toolbars, file selectors, tables, and trees. Using these advanced components can achieve more complex layouts and make the program interface more user-friendly to increase program flexibility. In later chapters, we'll start detailing these high-level components.

Before learning other advanced components, we first introduce some layout components, including sliders, progress bars, timers, menu bars, and toolbars. In this section, we first introduce sliders.

A slider (JSlider) is a component that allows users to select values ​​within a limited range by moving the slider. The common construction methods of the JSlider class are shown in Table 1.

Construction method illustrate
JSlider() Create a horizontal slider with a range of 0~100 and an initial value of 50
JSlider(BoundedRangeModel brm) Creates a horizontal slider using the specified BoundedRangeModel
JSlider(int orientation) Create a slider using the specified direction, ranging from 0 to 100 and with an initial value of 50
JSlider(int min,int max) Creates a horizontal slider using the specified minimum and maximum values, with an initial value equal to the average of the minimum value plus the maximum value
JSlider(int min,int max,int value) Creates a horizontal slider with specified minimum, maximum, and initial values

For example, the statement to create a horizontal slider with a minimum value of 30, a maximum value of 120, and an initial value of 55 is as follows.

  1. JSIider slider=new JSIider(30,120,55);

The slider can display major tick marks as well as minor tick marks between the major ticks. The number of values ​​between tick marks is controlled by the setMajorTickSpacing() and setMinorTickSpacing() methods. The drawing of tick marks is controlled by the setPaintTicks() method.

The slider can also print text labels along the slider scale at fixed intervals (or at any position). The drawing of the labels is controlled by the setLabelTable() method and setPaintLabels() method.

Common methods of the JSIider class are shown in Table 2.

method name illustrate
createStandardLabels(int increment) Creates a Hashtable of numeric text labels, starting at the minimum value of the slider and increasing using the specified increment
getLabelTable() Returns a dictionary of which labels are drawn at which ticks
getMaj orTickSpacing() Returns the interval between major tick marks
getMaximum() Returns the maximum value supported by the slider from BoundedRangeModel
getMinimum() Returns the minimum value supported by the slider from BoundedRangeModel
getMinorTickSpacing() Returns the interval between minor tick marks
getSnapToTicks() Returns true if the slider (and the value it represents) resolves to the value closest to the tick mark where the user placed the slider
getValue() Returns the slider's current value from BoundedRangeModel
setLabelTable(Dictionary labels) Used to specify which label will be drawn at a given value
setMaj orTickSpacing(int n) This method sets the interval between major tick marks
setMaximum(int maximum) Set the maximum value of the slider to maximum
setMinimum(int minimum) Set the minimum value of the slider to minimum
setMinorTickSpacing(int n) Sets the interval between minor tick marks
setOrientation(int orientation) Set the slider's direction to SwingConstants.VERTICAL or SwingConstants.HORIZONTAL
setPaintLabels(boolean b) Determines whether to draw labels on the slider
setPaintTicks(boolean b) Determines whether to draw tick marks on the slider
setPaintTrack(boolean b) Determines whether to draw slide rails on the slider
setSnapToTicks(boolean b) Specified as true, the slider (and the value it represents) resolves to the value closest to the tick mark where the user places the slider
setValue(int n) Set the slider's current value to n

Example 1

After understanding the basic syntax of the JSIider class, let's create a slider instance. The example is very simple and only contains a slider. The source code of the example is as follows.

  1. package ch18;
  2. import java.awt.Container;
  3. import javax.swing.JFrame;
  4. import javax.swing.JSlider;
  5. public class JSliderDemo
  6. {
  7. public static void main(String[] agrs)
  8. {
  9. JFrame frame=new JFrame("滑块组件示例");
  10. frame.setSize(100,100);
  11. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12. Container contentPane=frame.getContentPane();
  13. JSlider slider=new JSlider(0,100);
  14. slider.setMajorTickSpacing(10);
  15. slider.setMinorTickSpacing(5);
  16. slider.setPaintLabels(true);
  17. slider.setPaintTicks(true);
  18. contentPane.add(slider);
  19. frame.setVisible(true);
  20. }
  21. }

The above code first creates a JFrame window and sets the necessary properties, then creates a JSIider object, sets the minimum value to 0, the maximum value to 100, and then sets the scale value of the slider object. Run the example program at this time, and the effect is shown in Figure 1.

If you need to add scales or labels to the slider, you can add the following two lines of statements before "contentPane.add(slider);":

  1. slider.setPaintLabels(true);
  2. slider.setPaintTicks(true);

Run the program again, and the running effect of the slider is shown in Figure 2.


Figure 2 Slider with scales and labels

Guess you like

Origin blog.csdn.net/unbelievevc/article/details/128404099#comments_26863714