这是我所拥有的。
在这段代码中,我创建了一个包含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;
}
当您在组合框中选择正弦,Fsaw,FSquare项目时,需要禁用“曲线”,“宽度”和“坡度”滑块,当您选择“自定义”项时,需要重新启用它们。
这是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的有效性和礼仪 - 是否发布了这些大块代码的良好方式并且有效?还是让它更难回答这个问题?
答案 0 :(得分:2)
我个人会创建一个自定义组件,而不是从头开始构建滑块面板。这将使您有机会提供"控制"可以使用的方法更改整个组件的启用状态,并提供可用于根据需要检索滑块值的getter和setter