[英]How to set UI for all Components of a type in java swing?
I created my own MyScrollbarUI
class to have a custom scrollbar look in my application. 我创建了自己的
MyScrollbarUI
类,以便在我的应用程序中显示自定义滚动条。 Now I have to do 现在我必须这样做
scrollPane.getHorizontalScrollBar().setUI(new MyScrollbarUI());
scrollPane.getVerticalScrollBar().setUI(new MyScrollbarUI());
on any ScrollPane
I use. 在我使用的任何
ScrollPane
。
Is it somehow possible to tell Swing
that it should use MyScrollbarUI
on any scrollbar. 是否有可能告诉
Swing
它应该在任何滚动条上使用MyScrollbarUI
。 Maybe via the UIManager
? 也许通过
UIManager
?
UIManager.put("ScrollBarUI", MyScrollbarUI.class.getName());
should do the trick. 应该做的伎俩。
You need to have a public static ComponentUI createUI(JComponent c)
method in your UI class, returning an instance of your UI. 您需要在UI类中使用
public static ComponentUI createUI(JComponent c)
方法,返回UI的实例。
technically, it's as easy as to tell the UIManager which delegate to use (as @Harry Joy already mentioned), like 从技术上讲,告诉UIManager哪个代表使用(如@Harry Joy已经提到过的那样)就好了,就像
UIManager.put("ScrollBarUI", "fully-qualified-className-of-customUI")
This will effectively install the same delegate class for all LAFs which might result less than optimal visuals in all except the one you extended your custom ui-class from. 这将有效地为所有LAF安装相同的委托类,这可能导致除了扩展自定义ui-class之外的所有视觉效果都不是最佳视觉效果。 Strictly, you need one custom class per LAF
严格来说,每个LAF需要一个自定义类
Make new ScrollPane component by extending JScrollPane and tell your custom ScrollPane to use MyScrollbarUI. 通过扩展JScrollPane创建新的ScrollPane组件,并告诉您的自定义ScrollPane使用MyScrollbarUI。 Then modify all your code to use your custom ScrollPane instead of JScrollPane.
然后修改所有代码以使用自定义ScrollPane而不是JScrollPane。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.