gpt4 book ai didi

java - 在 Nimbus Look and Feel 的 JTabbedPane 中将图标左对齐

转载 作者:搜寻专家 更新时间:2023-11-01 01:43:35 24 4
gpt4 key购买 nike

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

我使用这段代码来放置标签:

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

我想让图标出现在左边

screenshot of the application

最佳答案

您可以通过JTabbedPane.setTabComponentAt(int index, Component component) 设置自定义组件来呈现选项卡标题。方法:

Sets the component that is responsible for rendering the title for the specified tab. A null value means JTabbedPane will render the title and/or icon for the specified tab. A non-null value means the component will render the title and JTabbedPane will not render the title and/or icon.

Note: The component must not be one that the developer has already added to the tabbed pane.

例如你可以这样做:

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

屏幕截图 1:

enter image description here


注意:使用此功能,您可以根据需要设置任何Component。例如,您可以制作一个带有 JButtonJPanel 来关闭选项卡:

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

截图2:

enter image description here


更新

您可能还想查看此主题:JTabbedPane: tab placement set to LEFT but icons are not aligned

关于java - 在 Nimbus Look and Feel 的 JTabbedPane 中将图标左对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19787146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com