Feel中的JTabbedPane中向左对齐图标

Feel中的JTabbedPane中向左对齐图标

本文介绍了在Nimbus Look and Feel中的JTabbedPane中向左对齐图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Nimbus外观使用 JTabbedPane 创建一个应用程序

我已使用以下代码放置标签:

  pane.addTab("Welcome",new ImageIcon("resources \\ 1.png"),mainPanel,进入欢迎页面"); 

我希望图标显示在左侧,并且

解决方案

您可以通过

I was creating an application with JTabbedPane using Nimbus look and feel

I have used this code to place tabs:

pane.addTab("Welcome",new ImageIcon("resources\\1.png"),mainPanel,"Takes to the welcome page");

I want the icon to appear on the left and

解决方案

You can set a custom component for rendering the tab title, through JTabbedPane.setTabComponentAt(int index, Component component) method:

For instance you can do this:

JLabel label = new JLabel("Tab1");
label.setHorizontalTextPosition(JLabel.TRAILING); // Set the text position regarding its icon
label.setIcon(UIManager.getIcon("OptionPane.informationIcon"));

JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
tabbedPane.addTab(null, new JPanel());
tabbedPane.setTabComponentAt(0, label); // Here set the custom tab component

Screenshot 1:


Note: using this feature you can set any Component as you wish. For instance you can make a JPanel with a JButton to close the tab:

final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);

ActionListener actionListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton)e.getSource();
        for(int i = 0; i < tabbedPane.getTabCount(); i++) {
            if(SwingUtilities.isDescendingFrom(button, tabbedPane.getTabComponentAt(i))) {
                tabbedPane.remove(i);
                break;
            }
        }
    }
};

JLabel label = new JLabel("Tab1", UIManager.getIcon("OptionPane.informationIcon"), JLabel.RIGHT);
JButton closeButton = new JButton("X");
closeButton.addActionListener(actionListener);

JPanel tabComponent = new JPanel(new BorderLayout());
tabComponent.add(label, BorderLayout.WEST);
tabComponent.add(closeButton, BorderLayout.EAST);

tabbedPane.addTab(null, new JPanel());
tabbedPane.setTabComponentAt(0, tabComponent); // Here set the custom tab component

Screenshot 2:


Update

You might want to see this topic as well: JTabbedPane: tab placement set to LEFT but icons are not aligned

这篇关于在Nimbus Look and Feel中的JTabbedPane中向左对齐图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:38