How to change tabs location in JTabbedPane?

╄→尐↘猪︶ㄣ 提交于 2019-12-02 18:11:44

问题


I want to change tabs location from left to center. How can I do this? I think I must change Look&Feel, but I don't know how.

From this:

To this:


回答1:


As you already pointed out, you have to use an LookAndFeel which supports this design (centered Tab-Button).
When your selected LaF does not support this, you have to write your own TabbedPaneUI.
(But this may not be very easy.)

If you do not want to create your own TabbedPaneUI, you have to look for an existing custom TabbedPaneUI or TabbedPane-Component, which support this kind of layout.

You can take a look at this article, to get started:
http://www.javaworld.com/article/2072927/swing-gui-programmingloseandmaxtabbedpane--an-enha/swing-gui-programming/closeandmaxtabbedpane--an-enhanced-jtabbedpane.html




回答2:


The placement of tabs is determined by the JTabbedPane UI delegate, typically based on BasicTabbedPaneUI. Not every Look & Feel implementation supports centered tabs, so there's no property that will work by default across platforms.

As a concrete example, com.apple.laf.AquaLookAndFeel supports centered tabs, as shown below. The class com.apple.laf.AquaTabbedPaneUI, which implements the effect, is shown here.

Because the implementation is non-trivial, a better choice is to support the user's Look & Feel choice using Preferences. A suitable Look & Feel selection control is shown here and here.

The source for the example above is seen here.



来源:https://stackoverflow.com/questions/25721354/how-to-change-tabs-location-in-jtabbedpane

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