我如何更改JTabbedPane选项卡后面面板的颜色?

问题描述 投票:0回答:1

问题中提到了问题。我打算在下图中更改标有“ X”的区域的颜色,但我不知道该怎么做。我试图更改JTabbedPane背景,但是没有用。

我对我可能犯的任何英语错误表示歉意。

最小可复制示例:

public class MainClass {

    public static void main(String[] args) {
        JFrame frame = new JFrame("TabbedPane Erorr"); // creates 2. JFrame
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setBackground(Color.RED);
        tabbedPane.addTab("Browser",new JPanel());
        tabbedPane.addTab("Asset Notifications",new JPanel());
        frame.setSize(new Dimension(500,200));
        frame.add(tabbedPane);
        frame.setVisible(true);
    }
}

“示例”

java swing jtabbedpane
1个回答
1
投票

好,我知道了。您需要指定自己的自定义UI。

  1. 您需要扩展BasicTabbedPaneUI类并覆盖paint
  2. 然后您可以设置UI:
tabbedPane.setUI(new CustomTabbedUI(Color.RED));

我挖出的这个old answer确实是部分正确的。显然它是不完整的。


Launcher.java

package q60855752;

import javax.swing.*;

public class Launcher {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new App());
    }
}

App.java

package q60855752;

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

public class App extends JPanel implements Runnable {
    public App() {
        setLayout(new BorderLayout());

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setUI(new CustomTabbedUI(Color.RED));

        // Modified: https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html
        addPanel(tabbedPane, "Tab 1", "Panel #1", "Does nothing", KeyEvent.VK_1, null);
        addPanel(tabbedPane, "Tab 2", "Panel #2", "Does twice as much nothing", KeyEvent.VK_2, null);
        addPanel(tabbedPane, "Tab 3", "Panel #3", "Still does nothing", KeyEvent.VK_3, null);
        addPanel(tabbedPane, "Tab 4", "Panel #4", "Panel #4 (has a preferred size of 410 x 50).", KeyEvent.VK_4, new Dimension(410, 50));

        add(tabbedPane, BorderLayout.CENTER);
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("App");
        frame.setPreferredSize(new Dimension(500, 200));
        frame.setContentPane(new App());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }

    protected void addPanel(JTabbedPane tabbedPane, String title, String content, String tooltip, int mnemonic, Dimension dimensions) {
        JComponent panel = createPanel(title);
        if (dimensions != null) panel.setPreferredSize(dimensions);
        tabbedPane.addTab(content, null, panel, tooltip);
        tabbedPane.setMnemonicAt(tabbedPane.getTabCount() - 1, mnemonic);
    }

    protected JComponent createPanel(String text) {
        JPanel panel = new JPanel(false);
        JLabel filler = new JLabel(text);
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);
        return panel;
    }
}

CustomTabbedUI.java

package q60855752;

import javax.swing.*;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import java.awt.*;

public class CustomTabbedUI extends BasicTabbedPaneUI {
    private Color backgroundColor;

    public CustomTabbedUI(Color backgroundColor) {
        super();
        this.backgroundColor = backgroundColor;
    }

    public void setBackgroundColor(Color backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    @Override
    public void paint(Graphics g, JComponent c) {
        Rectangle bounds = tabPane.getBounds();
        g.setColor(this.backgroundColor);
        g.fillRect(0, 0, bounds.width, bounds.height);

        super.paint(g, c); // Call parent...
    }
}
© www.soinside.com 2019 - 2025. All rights reserved.