0

这就是我所拥有的。

在这段代码中,我创建了一个包含 7 个滑块和一个组合框的面板。

public JPanel createOscPanel(final Osc o)
{
      //create a panel of 2 rows, 4 columns*/
    JPanel panel = gc.createComponentPanel(o.NAME, 2, 4);                   

    /*set up combobox panel*/
    JPanel cbPanel = new JPanel(); 
    cbPanel.setLayout(new BoxLayout(cbPanel, BoxLayout.Y_AXIS));

        /*label*/
    JPanel lbPanel = new JPanel();
    lbPanel.setLayout(new BoxLayout(lbPanel, BoxLayout.X_AXIS)); 
    lbPanel.setAlignmentY(LEFT_ALIGNMENT); 
    JLabel cbLabel = new JLabel("Wave Type:");
    lbPanel.add(cbLabel);
    cbPanel.add(lbPanel);

        /*combobox*/
    final String[] comboItems = {"Sine", "Custom", "FSaw", "FSquare"};
    JComboBox cb = new JComboBox(comboItems);
    cb.setMaximumSize(new Dimension (100, 20)); 

    cbPanel.add(cb); 

    /*add sliders*/
    panel.add(gc.createSliderPanel(o.getAmp(), DEFAULT_SLIDER_GRAINS)); 
    panel.add(gc.createSliderPanel(o.getFreq(), DEFAULT_SLIDER_GRAINS)); 
    panel.add(gc.createSliderPanel(o.getPhase(), DEFAULT_SLIDER_GRAINS));
    panel.add(cbPanel);
    panel.add(gc.createSliderPanel(o.getWidth(), DEFAULT_SLIDER_GRAINS));
    panel.add(gc.createSliderPanel(o.getSlope(), DEFAULT_SLIDER_GRAINS));
    panel.add(gc.createSliderPanel(o.getCurve(), DEFAULT_SLIDER_GRAINS));   
    panel.add(gc.createSliderPanel(o.getFourier(), (o.getFourier().MAX - o.getFourier().MIN))); 

    final Runnable disableCustomSliders = new Runnable() /*this is how do a method-in-method in Java*/
    {
        public void run()
        {
                  //I need to disable sliders here.                     

        }
    };

    final Runnable enableCustomSliders = new Runnable() /*this is how do a method-in-method in Java*/
    {
        public void run()
        {

               //I need to enable sliders here. 
        }
    };

    cb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox)e.getSource();
            int value = cb.getSelectedIndex();

            switch(value)
            {
            case 0: {o.setWaveShape(Osc.WaveShape.SINE); disableCustomSliders.run();} break;
            case 1: {o.setWaveShape(Osc.WaveShape.CUSTOM); enableCustomSliders.run();} break;
            case 2: {o.setWaveShape(Osc.WaveShape.FSAW); disableCustomSliders.run();} break;
            case 3: {o.setWaveShape(Osc.WaveShape.FSQUARE);disableCustomSliders.run();} break;

            }

            refresh();
        }
    });

    return panel; 

}

当您在组合框中选择 Sine、Fsaw、FSquare 项目时,您需要禁用 Curve、Width 和 Slope 滑块,而当您选择 Custom 项目时,您需要重新启用它们。

这是createSliderPanel代码。

public JPanel createSliderPanel(
        final Parameter parameter,
        final int grains)
{       
    /*create and assign components*/ 
    final JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
    panel.setBorder(BorderFactory.createRaisedBevelBorder()); 

    final JLabel label = new JLabel(parameter.NAME); 
    final JLabel valueLabel = new JLabel();

    /*set up slider*/
    int curr = valueToSlider(parameter.MIN.doubleValue(), parameter.MAX.doubleValue(),
            parameter.getValue().doubleValue(), parameter.SCALETYPE, parameter.Q, grains);
    final JSlider slider = new JSlider(JSlider.VERTICAL, 0, grains, curr);

    slider.setPreferredSize(new Dimension(SLIDER_WIDTH, SLIDER_HEIGHT));

    slider.setAlignmentX(Component.LEFT_ALIGNMENT);



    panel.add(valueLabel); 
    panel.add(slider);
    panel.add(label);

    /*slider move event*/
    slider.addChangeListener(new ChangeListener() {
        /* (non-Javadoc)
         * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
         */
        public void stateChanged(ChangeEvent event) {

            double value = sliderToValue(parameter.MIN.doubleValue(), parameter.MAX.doubleValue(),slider.getValue(), parameter.SCALETYPE, parameter.Q, grains); 

            updateSliderLabel(parameter, valueLabel, parameter.getValue());  

            /*set the parameter value*/
            if (parameter.CLASSTYPE.equals(Double.class))
                     parameter.setValue(value);
            else if (parameter.CLASSTYPE.equals((Integer.class)))
                    parameter.setValue((int)value); 

            gui.refresh(); 
        }


    });

    /*need to reset the valueLabel to it's correct format*/
    updateSliderLabel(parameter, valueLabel, parameter.getValue()); 

    return panel; 
}

所以问题是 - 我如何引用这些滑块以便对它们执行 .setEnabled(false) ?

我能想到的直接且相当简单的解决方案是重构createSliderPanel,例如传入父面板,并将滑块面板添加​​到方法内的父级,然后返回滑块。

有没有更好的遵循 OO 和 MVC 的概念?

最后一个问题是:Stack Overflow 上的有效性和礼仪 - 发布这些大块代码是否礼貌且有效?还是让回答问题变得更加困难?

4

1 回答 1

2

我个人会创建一个自定义组件,而不是从头开始构建滑块面板。这将使您有机会提供可用于更改整个组件的启用状态的“控制”方法,并提供可用于根据需要检索滑块值的 getter 和 setter

于 2012-11-20T00:15:06.037 回答