0

我有 10 个 JSlider、10 个 JLabels 和 10 个 JToggleButton。当用户拖动滑块时,我需要更新其相应的标签并将所有滑块移动到相同的值,并切换相应的按钮。现在,我的问题不是如何做到这一点,而是如何在同一个事件中拥有这一切。

在我美好的 VB 时代(;))我只需要分配具有相同名称的滑块,以及按钮和标签,并且我将自动拥有可由索引访问的滑块、标签和按钮数组,都触发相同的事件。

在我目前的 C++Builder 时代,我必须为它们中的每一个设置 Tag 属性(一个通用的 int 属性),选择所有滑块并为它们声明相同的事件,然后从 sender 对象中取回标签并将其用作索引。然后我将声明一个标签和按钮指针数组,以便我可以通过索引访问控件。

在 Java 中,如何为 IDE 中的所有滑块设置相同的事件(我使用 NetBeans)?以及如何将标签和切换按钮作为数组访问?

我见过一些例子,但它们都在运行时构建控件,我想以这种方式设计我的表单。

谢谢

4

2 回答 2

1

ChangeListener为每个滑块添加一个。侦听器应该有一个它需要更新的所有其他滑块的集合。

沿着这些思路;

public void stateChanged(ChangeEvent e) {
  JSlider source = (JSlider)e.getSource();
  int val = (int)source.getValue();

  for (JSlider slider : otherSliders) {
    slider.setValue(val);
  }
}
于 2013-02-18T11:12:13.407 回答
1

好的,非常快,这大致是我的想法(可以改进很多,使用一些快捷方式可以更快):

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.AbstractButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TestMVC {

    public static class ModelObject {
        private PropertyChangeSupport pcs;

        public ModelObject() {
            pcs = new PropertyChangeSupport(this);
        }

        public PropertyChangeSupport getPropertyChangeSupport() {
            return pcs;
        }
    }

    public static class Model extends ModelObject {

        private List<Item> items;

        public Model() {
            items = new ArrayList<TestMVC.Item>();
        }

        public List<Item> getItems() {
            return items;
        }

        public void addToItems(Item item) {
            item.setModel(this);
            items.add(item);
            getPropertyChangeSupport().firePropertyChange("item", null, item);
        }

        public void removeFromItems(Item item) {
            items.add(item);
            item.setModel(null);
            getPropertyChangeSupport().firePropertyChange("item", item, null);
        }

    }

    public static class Item extends ModelObject {
        public static final String VALUE = "value";

        public static final String LABEL = "label";

        public static final String ENABLED = "enabled";

        private Model model;

        private boolean enabled;
        private String label;
        private int value;

        public Item() {
        }

        public Item(boolean enabled, String label, int value) {
            super();
            this.enabled = enabled;
            this.label = label;
            this.value = value;
        }

        public Model getModel() {
            return model;
        }

        public void setModel(Model model) {
            this.model = model;
        }

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            boolean old = this.enabled;
            this.enabled = enabled;
            getPropertyChangeSupport().firePropertyChange(ENABLED, old, enabled);
        }

        public String getLabel() {
            return label;
        }

        public void setLabel(String label) {
            String old = this.label;
            this.label = label;
            getPropertyChangeSupport().firePropertyChange(LABEL, old, enabled);
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            _internalSetValue(value);
            if (this.isEnabled()) {
                for (Item item : model.getItems()) {
                    if (item != this && item.isEnabled()) {
                        item._internalSetValue(value);
                    }
                }
            }
        }

        private void _internalSetValue(int value) {
            int old = this.value;
            this.value = value;
            getPropertyChangeSupport().firePropertyChange(VALUE, old, value);
        }
    }

    public static class ItemView implements PropertyChangeListener {
        private Item item;

        private JLabel label;
        private JSlider slider;
        private AbstractButton button;

        public ItemView(Item item) {
            super();
            this.label = new JLabel(item.getLabel());
            this.slider = new JSlider(0, 100);
            this.slider.setValue(item.getValue());
            this.slider.setEnabled(item.isEnabled());
            this.button = new JCheckBox((String) null, item.isEnabled());
            this.item = item;
            item.getPropertyChangeSupport().addPropertyChangeListener(this);
        }

        public JLabel getLabel() {
            return label;
        }

        public JSlider getSlider() {
            return slider;
        }

        public AbstractButton getButton() {
            return button;
        }

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals(Item.LABEL)) {
                label.setText(item.getLabel());
            } else if (evt.getPropertyName().equals(Item.ENABLED)) {
                button.setSelected(item.isEnabled());
                slider.setEnabled(item.isEnabled());
            } else if (evt.getPropertyName().equals(Item.VALUE)) {
                slider.setValue(item.getValue());
            }
        }
    }

    private Model model;

    public TestMVC(Model model) {
        super();
        this.model = model;
    }

    protected void initUI() {
        JFrame frame = new JFrame(TestMVC.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc1 = new GridBagConstraints();
        GridBagConstraints gbc3 = new GridBagConstraints();
        gbc3.weightx = 1.0;
        gbc3.fill = GridBagConstraints.HORIZONTAL;
        gbc3.gridwidth = GridBagConstraints.REMAINDER;
        for (final Item item : model.getItems()) {
            final ItemView view = new ItemView(item);
            view.getButton().addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    item.setEnabled(view.getButton().isSelected());
                }
            });
            view.getSlider().addChangeListener(new ChangeListener() {

                @Override
                public void stateChanged(ChangeEvent e) {
                    item.setValue(view.getSlider().getValue());
                }
            });
            panel.add(view.getButton(), gbc1);
            panel.add(view.getLabel(), gbc1);
            panel.add(view.getSlider(), gbc3);
        }
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    private static Model loadModel() {
        Random random = new Random();
        Model model = new Model();
        for (int i = 0; i < 10; i++) {
            Item item = new Item(random.nextBoolean(), "Some label " + random.nextLong(), random.nextInt(100));
            model.addToItems(item);
        }
        return model;
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Model model = loadModel();
                new TestMVC(model).initUI();
            }
        });
    }

}
于 2013-02-18T17:19:41.410 回答