What is AttributeSet and what is it used for?

喜欢而已 提交于 2019-12-25 07:42:20

问题


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:

  1. What exactly is the AttributeSet interface? What is the purpose of instances of classes implementing this interface?

  2. Also, the Style interface is a subinterface of the AttributeSet interface. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!