[英]Aligning icon to the left in JTabbedPane in Nimbus Look and Feel
我使用Nimbus的外观和感觉创建了一个使用JTabbedPane
的应用程序
我已使用此代码放置标签:
pane.addTab("Welcome",new ImageIcon("resources\\1.png"),mainPanel,"Takes to the welcome page");
我希望图标显示在左侧和
您可以通过JTabbedPane.setTabComponentAt(int index,Component component)方法设置用于呈现选项卡标题的自定义组件:
设置负责呈现指定选项卡标题的组件 。 空值表示
JTabbedPane
将呈现指定选项卡的标题和/或图标。 非null值表示组件将呈现标题,JTabbedPane
将不呈现标题和/或图标。注意:组件不能是开发人员已添加到选项卡式窗格的组件。
例如,你可以这样做:
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:
注意:使用此功能,您可以根据需要设置任何Component
。 例如,您可以创建一个带有JButton
的JPanel
来关闭选项卡:
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:
您可能也希望看到此主题: JTabbedPane:选项卡位置设置为LEFT但图标未对齐
使用HTML格式有一个更简单的解决方案。 这是使用html代码格式化文本的示例,但您也可以格式化选项卡中的其他元素:
final String PRE_HTML = "<html><p style=\"text-align: left; width: 230px\">";
final String POST_HTML = "</p></html>";
tabbedpane.setTitleAt(0, PRE_HTML + "your title" + POST_HTML);
tabbedpane.setTitleAt(2, PRE_HTML + "your title 2" + POST_HTML);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.