package com.been; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.IndexedPropertyChangeEvent; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.HashMap; import javax.swing.JButton; import javax.swing.JFrame; public class FrameChange extends JFrame implements PropertyChangeListener { boolean beanValue = false; private HashMap<FrameChange, BeanTest> hashMap = new HashMap<>(); int i =1; int j = 1; private BeanTest testBean1; private BeanTest testBean2; public FrameChange() { // testBean.id = 500; // testBean.addPropertyChangeListener(this); testBean1 = new BeanTest(); testBean2 = new BeanTest(); BeanTest testBean3 = new BeanTest(); // BeanTest.isfalse = false; // testBean.removePropertyChangeListener(this); testBean2.id = 200; testBean2.addPropertyChangeListener(this); testBean1.id = 900; testBean1.addPropertyChangeListener(this); hashMap.put(this, testBean2); if (!hashMap.containsKey(this)){ hashMap.put(this, testBean2); }else{ hashMap.get(this).removePropertyChangeListener(this); hashMap.put(this, testBean1); } PropertyChangeListener[] listeners = testBean2.getPropertyChangeListeners(); for (PropertyChangeListener listener : listeners){ System.out.println(listener); } JButton jb = new JButton("change"); this.setTitle(""); this.setBounds(100, 100, 100, 100); this.setLayout(new FlowLayout()); this.add(jb); jb.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { testBean2.setProperty(j++, testBean2.id); testBean1.setProperty(j++, testBean1.id); } }); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String args[]) { new FrameChange(); } // @Override // public void propertyChange(PropertyChangeEvent evt) { // if (evt.getSource() == testBean) { // beanValue = !beanValue; // System.out.println("變化" + beanValue ); // // } // } @Override public void propertyChange(PropertyChangeEvent evt) { // if (evt.getSource() == testBean) { // beanValue = !beanValue; IndexedPropertyChangeEvent changeEvent = (IndexedPropertyChangeEvent) evt; // System.out.println("變化" + beanValue + " "+changeEvent.getIndex()); // } // if (changeEvent.getIndex() == ((BeanTest)evt.getSource()).id) { // System.out.println("change-----100-------"); // } // if (((BeanTest)evt.getSource()).flag){ // System.out.println("change-----100-------"); // } System.out.println("change--------------"+((BeanTest)evt.getSource()).id); } }
package com.been; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; public class BeanTest { private int property; public int id ; public boolean flag = true; private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this); public static boolean isfalse = true; public void setProperty(int newValue, int index) { int oldValue = property; property = newValue; // changeSupport.firePropertyChange("property", oldValue, newValue); changeSupport.fireIndexedPropertyChange("property", index, oldValue, newValue); } public void addPropertyChangeListener(PropertyChangeListener l) { PropertyChangeListener[] listeners = getPropertyChangeListeners(); for(PropertyChangeListener listener : listeners){ removePropertyChangeListener(listener); } if (isfalse){ changeSupport.addPropertyChangeListener(l); } System.out.println("isfalse-------"+isfalse); } public void removePropertyChangeListener(PropertyChangeListener l) { changeSupport.removePropertyChangeListener(l); } public PropertyChangeListener[] getPropertyChangeListeners(){ if (changeSupport == null) return null; return changeSupport.getPropertyChangeListeners(); } }