Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Nimbus Look and Feel中,在JTabbedPane中将图标向左对齐_Java_Swing_User Interface_Jtabbedpane_Nimbus - Fatal编程技术网

Java 在Nimbus Look and Feel中,在JTabbedPane中将图标向左对齐

Java 在Nimbus Look and Feel中,在JTabbedPane中将图标向左对齐,java,swing,user-interface,jtabbedpane,nimbus,Java,Swing,User Interface,Jtabbedpane,Nimbus,我正在使用Nimbus look and feel创建一个带有JTabbedPane的应用程序 我已使用此代码放置选项卡: pane.addTab("Welcome",new ImageIcon("resources\\1.png"),mainPanel,"Takes to the welcome page"); 我希望图标显示在左侧和右侧 您可以通过以下方法设置用于呈现选项卡标题的自定义组件: 设置负责呈现文档标题的组件 指定的选项卡。空值表示JTabbedPane将呈现标题 和/或指定选项

我正在使用Nimbus look and feel创建一个带有
JTabbedPane
的应用程序

我已使用此代码放置选项卡:

pane.addTab("Welcome",new ImageIcon("resources\\1.png"),mainPanel,"Takes to the welcome page");
我希望图标显示在左侧和右侧


您可以通过以下方法设置用于呈现选项卡标题的自定义组件:

设置负责呈现文档标题的组件 指定的选项卡。空值表示
JTabbedPane
将呈现标题 和/或指定选项卡的图标。非空值表示 组件将呈现标题,
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:


注意:使用此功能,您可以根据需要设置任何
组件。例如,您可以使用
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
final JTabbedPane选项卡bedpane=新JTabbedPane(JTabbedPane.LEFT);
ActionListener ActionListener=新建ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
JButton按钮=(JButton)e.getSource();
对于(int i=0;i
屏幕截图2:


更新
您可能也想看看这个主题:

有一个使用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);
final String PRE\u HTML=“

”; 最后一个字符串POST_HTML=“

”; tabbedpane.setTitleAt(0,前HTML+你的标题+后HTML); tabbedpane.setTitleAt(2,前HTML+你的标题2+后HTML);
所以。。。你试过我的答案了吗?有用吗?注:此解决方案与L&F无关。yupp。我没有将它应用到我的应用程序中,但我尝试了你的代码并理解了这个概念。我喜欢带有关闭按钮的那个(类似于google chrome tab和其他选项卡式应用程序),是的,您的解决方案与外观无关,因为您在面板中对齐它,然后添加itTab组件(无论JPanel是如何构造的)。您知道如何在不重写UI的情况下使内部面板(选项卡组件)左对齐或右对齐吗?最好的解决方案是LAF安全。