问题
I have read the docs, but the docs seem vague to me and I'm having a hard time understanding what it is exactly and what it's used for.
Two question regarding this:
What exactly is the
AttributeSetinterface? What is the purpose of instances of classes implementing this interface?Also, the
Styleinterface is a subinterface of theAttributeSetinterface. What exactly is this interface? What is the purpose of instances of classes implementing it?
回答1:
An AttributeSet implementation is supposed to hold a set of Key,Value pair objects.
You can use HashMap/HashTable etc for you own implementation of AttributeSet.
AttributeSet doesnt have any method for adding key values. There comes MutableAttributeSet
Style
is a
MutableAttributeSet which provides Listener support. Which means you can change the key-value based on some changeListener.
回答2:
Can you give me an example of using Style or MutableAttributeSet in practice? For example when creating a word processor?
Start with the section from the Swing tutorial on General Text Component features. It contains a working example that allows you to play with attributes like font, color, italic, bold...
来源:https://stackoverflow.com/questions/23430031/what-is-attributeset-and-what-is-it-used-for