Технологии Java
JavaBeans
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
private final PropertyChangeSupport pcs =
new PropertyChangeSupport(this);
public void setTitle(String title) {
String oldValue = this.title;
this.title = title;
pcs.firePropertyChange("title",
oldValue, this.title);
}
private final VetoableChangeSupport vcs =
new VetoableChangeSupport(this);
public void setTitle(String title) throws
PropertyVetoException {
String oldValue = this.title;
vcs.fireVetoableChange("title", oldValue, title);
this.title = title;
pcs.firePropertyChange("title", oldValue, title);
}
JButton button = new JButton("Hello");
button.setAlignmentX(1);
XMLEncoder encoder = new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream("button.xml")));
encoder.writeObject(button);
encoder.close();
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.8.0_40"
class="java.beans.XMLDecoder">
<object class="javax.swing.JButton">
<string>Hello</string>
<void property="alignmentX">
<float>1.0</float>
</void>
</object>
</java>
XMLDecoder decoder = new XMLDecoder(
new BufferedInputStream(
new FileInputStream("button.xml")));
JButton button = (JButton) decoder.readObject();
decoder.close();
