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

时间:2015-08-05 05:12:19

标签: java swing jslider

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

http://localhost/project/news-and-events/dosarrest-strong-performer-in-2015-forrester-wave-for-ddos-service-providers-1

1 个答案:

答案 0 :(得分:2)

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

Sliders

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。这里的想法是当你附加一个监听器时,你可以为关键字整合滑块并确定它所代表的通道​​,作为一个想法