【问题标题】:Change JTabbedPane active tab color dynamically动态更改 JTabbedPane 活动选项卡颜色
【发布时间】:2021-10-09 19:11:55
【问题描述】:

我的要求是动态更改 JTabbedPane 的标签页。就像单击按钮时更改选项卡和选定选项卡颜色一样。出于同样的原因,我不能使用 UIManager。但是一旦设置标签颜色并且更改标签无法正常工作。选中的选项卡始终显示默认颜色,而不是我设置的颜色。

下面是代码:

import javax.swing.*;
import java.awt.*;

public class TestJTP extends JFrame {

    JTabbedPane tabbedPane;
    public TestJTP() {
        init();
    }

    private void init() {

        tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Tab-1", null);
        tabbedPane.addTab("Tab-2", null);
        tabbedPane.addTab("Tab-3", null);
        tabbedPane.addTab("Tab-4", null);
        tabbedPane.addTab("Tab-5", null);

        JPanel panel = new JPanel();
        JButton b1 = new JButton("Orange,blue");
        b1.addActionListener(e -> {
            tabbedPane.setBackground(null);
            tabbedPane.setBackgroundAt(tabbedPane.getSelectedIndex(), null);
            tabbedPane.setBackground(Color.orange);
            tabbedPane.setBackgroundAt(tabbedPane.getSelectedIndex(), Color.blue);
        });
        JButton b2 = new JButton("Red,cyan");
        b2.addActionListener(e -> {
            tabbedPane.setBackground(null);
            tabbedPane.setBackground(Color.red);
            tabbedPane.setBackgroundAt(tabbedPane.getSelectedIndex(), null);
            tabbedPane.setBackgroundAt(tabbedPane.getSelectedIndex(), Color.cyan);
        });
        JButton b3 = new JButton("Green,magenta");
        b3.addActionListener(e -> {
            tabbedPane.setBackground(null);
            tabbedPane.setBackground(Color.green);
            tabbedPane.setBackgroundAt(tabbedPane.getSelectedIndex(), null);
            tabbedPane.setBackgroundAt(tabbedPane.getSelectedIndex(), Color.magenta);
        });
        panel.add(b1);
        panel.add(b2);
        panel.add(b3);
        getContentPane().add(panel, BorderLayout.NORTH);
        getContentPane().add(tabbedPane, BorderLayout.CENTER);
        pack();
        setVisible(true);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
    }

    public static void main(String[] args) {
        new TestJTP();
    }
}

【问题讨论】:

    标签: java swing tabs selected jtabbedpane


    【解决方案1】:

    如果我理解您的要求,请尝试:

    //tabbedPane.setBackground(null);
    tabbedPane.setBackground(Color.red);
    //tabbedPane.setBackgroundAt(tabbedPane.getSelectedIndex(), null);
    //tabbedPane.setBackgroundAt(tabbedPane.getSelectedIndex(), Color.cyan);
    UIManager.put("TabbedPane.selected", Color.CYAN);
    SwingUtilities.updateComponentTreeUI(tabbedPane);
    

    这应该重置所有选项卡的默认“选项卡选择颜色”和“选项卡颜色”。

    【讨论】:

    • 它在 SwingUtilities.updateComponentTreeUI(tabbedPane); 之后工作。谢谢。接受作为答案,虽然我需要更改文本颜色以及需要搜索,但如果您知道名称,请告诉我。类似于“TabbedPane.selected”
    • @shaILU,所选文本没有 UI 属性。请参阅:UIManager Defaults。正确的解决方案是创建自定义 UI。 UIManager 默认值将 UI 显示为属性之一。不幸的是,您需要为要支持的每个 LAF 创建自定义 UI。
    • 感谢@camickr - 我观察到另一个可能对某些人有所帮助的事实。而不是“SwingUtilities.updateComponentTreeUI(tabbedPane);”下面的行也将有助于“tabbedPane.updateUI();”
    猜你喜欢
    • 2014-06-19
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    • 2018-03-26
    相关资源
    最近更新 更多