将滑块组件的多个实例添加到 Jpanel

Add multiple instances of slider component to Jpanel

正在开发具有多个滑块的混音器应用程序。我想为我确定的多个通道并排创建同一滑块的多个实例 (1-16)。这将非常有用,所以我只想从左到右创建新的 VolumeControl 并排实例。现在我只是他们都可以从相同的输入、听众等工作。我只是在努力让他们显示。这是我创建 JSlider 的单个实例所拥有的(主要来自演示)。注意:我来自 Android 开发所以我可能会完全错误,复杂的方式!

public class VolumeControl extends JPanel implements ChangeListener {

    ...

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(VolumeControl::createAndShowGUI);
    }

    public VolumeControl () {

        super(new BorderLayout());

        ...

        JSlider volumeControl = new JSlider(JSlider.VERTICAL,
                VOLUME_MIN, VOLUME_MAX, currentVolume);

        volumeControl.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10));
        add(volumeControl, BorderLayout.LINE_START);
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    }

    private static void createAndShowGUI() {

        JFrame frame = new JFrame("Volume Control");
        VolumeControl animator = new VolumeControl();
        frame.add(animator, BorderLayout.CENTER);

        frame.pack();
        frame.setVisible(true);
    }

}

只需创建一个或多个工厂方法,您就可以使用这些方法以您想要的方式创建和配置滑块...

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new VolmeControlPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class VolmeControlPane extends JPanel {

        protected static final int VOLUME_MIN = 0;
        protected static final int VOLUME_MAX = 100;
        protected static final String SLIDER_CONTROL_KEY = "Slider.controlKey";

        public VolmeControlPane() {
            setLayout(new GridBagLayout());
            JSlider[] sliders = makeSliders(16);
            for (JSlider slider : sliders) {
                add(slider);
            }
        }

        public JSlider[] makeSliders(int count) {

            JSlider[] sliders = new JSlider[count];
            for (int index = 0; index < count; index++) {
                sliders[index] = makeSlider(VOLUME_MIN, VOLUME_MAX, VOLUME_MIN + (VOLUME_MAX / 2));
                sliders[index].putClientProperty(SLIDER_CONTROL_KEY, index);
            }

            return sliders;

        }

        public JSlider makeSlider(int min, int max, int value) {
            JSlider volumeControl = new JSlider(JSlider.VERTICAL,
                            min, max, value);

            volumeControl.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10));

            return volumeControl;
        }

    }

}

我添加的一个小技巧是使用 putClientProperty 提供一个键来识别滑块,您也可以使用 setName。这里的想法是当你附加一个监听器时,你可以将滑块与键交互并确定它代表哪个通道,作为一个想法