What I want: the icons ALL in the LEFT, not based on the Text Size
[…]
选项卡的内容以典型的实现为中心,这是有道理的,因为在有效地渲染选项卡之前,适应此内容所需的区域是不可预测的.由于该区域取决于内容,并且不同的选项卡可能具有不同的标题长度,因此必须制定有关如何呈现这些选项卡的策略.标准是使标签内容居中,并使标签区域适合此内容.当我们有一个默认的选项卡式窗格,其顶部放置有选项卡时,我们不太在乎图标/文本的对齐方式:
唯一关心的可能是具有不同长度的标签,但是谁在乎呢?毕竟,图标和文本是可见的,并且选项卡式窗格看起来足够好.但是,将选项卡的位置设置为LEFT或RIGHT时,情况会有所不同,并且看起来不那么吸引人:
显然,这种默认行为是一个长期存在的问题,有一个非常有趣的讨论here.那里有一些SO成员:@ camickr,@ kleopatra,@ splungebob.如该文章中所讨论的,不可能有一个简单的解决方案,并且提出了几种解决方法:基本上是自定义UI实现或使用面板作为渲染器,并根据文本长度使用首选的宽度/高度进行播放.两种选择都涉及大量工作.
为了避免与UI委托打交道并利用setTabComponentAt(…)方法,我前段时间已开始在此处共享一个选项卡式窗格扩展.该方法基于Swing的renderer概念:必须生成一个组件以渲染另一个组件的零件的类,目标是提供一种灵活的机制来添加自定义选项卡组件.
我使用自定义选项卡式窗格在下面提供了一个示例,这是提供上述机制所需的所有接口/类的概述.
ITabRenderer界面
第一步是定义一个Iterface,以提供合同来渲染Tab组件.
AbstractTabRenderer类
提供基本方法以帮助实现getTabRendererComponent(…)方法的抽象类.该抽象类具有三个主要属性:
> prototypeText:用于定义原型文本以生成默认渲染器组件.
> prototypeIcon:用于定义原型图标以生成默认渲染器.
> horizo??ntalTextAlignment:制表符的文本水平对齐方式.
请注意,此类是抽象的,因为它没有实现getTabRendererComponent(…)方法.
DefaultTabRenderer类
通过扩展AbstractTabRenderer类的具体实现.请注意,如果您想包括一个如教程演示中所示的关闭按钮,那么在此类中进行一些工作就足够了.事实上,我已经这样做了,但是我不会包括不扩展此(已经很大)帖子的那一部分.
JXTabbedPane
最后,选项卡式窗格的扩展包括选项卡渲染器支持并覆盖addTab(…)方法.
例
我使用这些PLAF运行了这个示例,并获得了积极的结果:
> WindowsLookAndFeel
> WindowsClassicLookAndFeel
>雨云LookAndFeel
> MetalLookAndFeel
> SeaglassLookAndFeel
另外,如果将选项卡的位置从“左”切换到“顶部”(默认)或“底部”,则所有选项卡的宽度仍然相同,从而解决了此答案第二段中所述的问题.
import ponent;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class Demo {
private void createAndShowGUI() {
JXTabbedPane tabbedPane = new JXTabbedPane(JTabbedPane.LEFT);
AbstractTabRenderer renderer = (AbstractTabRenderer)tabbedPane.getTabRenderer();
renderer.setPrototypeText("This text is a prototype");
renderer.setHorizontalTextAlignment(SwingConstants.LEADING);
tabbedPane.addTab("Short", UIManager.getIcon("OptionPane.informationIcon"), createEmptyPanel(), "Information tool tip");
tabbedPane.addTab("Long text", UIManager.getIcon("OptionPane.warningIcon"), createEmptyPanel(), "Warning tool tip");
tabbedPane.addTab("This is a really long text", UIManager.getIcon("OptionPane.errorIcon"), createEmptyPanel(), "Error tool tip");
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(tabbedPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createEmptyPanel() {
JPanel dummyPanel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return isPreferredSizeSet() ?
super.getPreferredSize() : new Dimension(400, 300);
}
};
return dummyPanel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Demo().createAndShowGUI();
}
});
}
class JXTabbedPane extends JTabbedPane {
private ITabRenderer tabRenderer = new DefaultTabRenderer();
public JXTabbedPane() {
super();
}
public JXTabbedPane(int tabPlacement) {
super(tabPlacement);
}
public JXTabbedPane(int tabPlacement, int tabLayoutPolicy) {
super(tabPlacement, tabLayoutPolicy);
}
public ITabRenderer getTabRenderer() {
return tabRenderer;
}
public void setTabRenderer(ITabRenderer tabRenderer) {
this.tabRenderer = tabRenderer;
}
@Override
public void addTab(String title, Component component) {
this.addTab(title, null, component, null);
}
@Override
public void addTab(String title, Icon icon, Component component) {
this.addTab(title, icon, component, null);
}
@Override
public void addTab(String title, Icon icon, Component component, String tip) {
super.addTab(title, icon, component, tip);
int tabIndex = getTabCount() - 1;
Component tab = tabRenderer.getTabRendererComponent(this, title, icon, tabIndex);
super.setTabComponentAt(tabIndex, tab);
}
}
interface ITabRenderer {
public Component getTabRendererComponent(JTabbedPane tabbedPane, String text, Icon icon, int tabIndex);
}
abstract class AbstractTabRenderer implements ITabRenderer {
private String prototypeText = "";
private Icon prototypeIcon = UIManager.getIcon("OptionPane.informationIcon");
private int horizontalTextAlignment = SwingConstants.CENTER;
private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
public AbstractTabRenderer() {
super();
}
public void setPrototypeText(String text) {
String oldText = this.prototypeText;
this.prototypeText = text;
firePropertyChange("prototypeText", oldText, text);
}
public String getPrototypeText() {
return prototypeText;
}
public Icon getPrototypeIcon() {
return prototypeIcon;
}
public void setPrototypeIcon(Icon icon) {
Icon oldIcon = this.prototypeIcon;
this.prototypeIcon = icon;
firePropertyChange("prototypeIcon", oldIcon, icon);
}
public int getHorizontalTextAlignment() {
return horizontalTextAlignment;
}
public void setHorizontalTextAlignment(int horizontalTextAlignment) {
this.horizontalTextAlignment = horizontalTextAlignment;
}
public PropertyChangeListener[] getPropertyChangeListeners() {
return propertyChangeSupport.getPropertyChangeListeners();
}
public PropertyChangeListener[] getPropertyChangeListeners(String propertyName) {
return propertyChangeSupport.getPropertyChangeListeners(propertyName);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
}
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
PropertyChangeListener[] listeners = getPropertyChangeListeners();
for (int i = listeners.length - 1; i >= 0; i--) {
listeners[i].propertyChange(new PropertyChangeEvent(this, propertyName, oldValue, newValue));
}
}
}
class DefaultTabRenderer extends AbstractTabRenderer implements PropertyChangeListener {
private Component prototypeComponent;
public DefaultTabRenderer() {
super();
prototypeComponent = generateRendererComponent(getPrototypeText(), getPrototypeIcon(), getHorizontalTextAlignment());
addPropertyChangeListener(this);
}
private Component generateRendererComponent(String text, Icon icon, int horizontalTabTextAlignmen) {
JPanel rendererComponent = new JPanel(new GridBagLayout());
rendererComponent.setOpaque(false);
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(2, 4, 2, 4);
c.fill = GridBagConstraints.HORIZONTAL;
rendererComponent.add(new JLabel(icon), c);
c.gridx = 1;
c.weightx = 1;
rendererComponent.add(new JLabel(text, horizontalTabTextAlignmen), c);
return rendererComponent;
}
@Override
public Component getTabRendererComponent(JTabbedPane tabbedPane, String text, Icon icon, int tabIndex) {
Component rendererComponent = generateRendererComponent(text, icon, getHorizontalTextAlignment());
int prototypeWidth = prototypeComponent.getPreferredSize().width;
int prototypeHeight = prototypeComponent.getPreferredSize().height;
rendererComponent.setPreferredSize(new Dimension(prototypeWidth, prototypeHeight));
return rendererComponent;
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
String propertyName = evt.getPropertyName();
if ("prototypeText".equals(propertyName) || "prototypeIcon".equals(propertyName)) {
this.prototypeComponent = generateRendererComponent(getPrototypeText(), getPrototypeIcon(), getHorizontalTextAlignment());
}
}
}
}
截图
MetalLookAndFeel
NimbusLookAndFeel
SeaglassLookAndFeel
WindowsLookAndFeel