在Java选项卡窗格中控制颜色

Ell*_*ott 6 java swing jtabbedpane

我一直在努力解决这个问题.

我试图在JTabbedPane中出现一个淡蓝色背景.我已经尝试了一切,似乎没有任何工作.

以下是我的代码.如果您运行它,它将显示选项卡,当选择浅蓝色背景和顶部的东西蓝色边框.我想控制这种颜色.但是怎么样?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.ColorUIResource;
public class Main extends JFrame {
  JTabbedPane tab=new JTabbedPane();
  public Main() {
     setSize(300,300);
     setTitle("Test Tab pane");
     tab.add("First",new myPanel("First"));
     tab.add("Second",new myPanel("Second"));
     tab.add("Third",new myPanel("Third"));
     tab.add("Fourth",new myPanel("Fourth"));
     tab.addChangeListener(new ChangeTab());
     getContentPane().add(tab,BorderLayout.CENTER);
     setVisible(true);
     for(int i=0;i<tab.getTabCount();i++){
          if(i != tab.getSelectedIndex())
            tab.setBackgroundAt(i,Color.orange);
            tab.setForeground(Color.BLACK);
     }
     tab.setOpaque(true);
     UIManager.put("TabbedPane.contentAreaColor ",ColorUIResource.GREEN);
     UIManager.put("TabbedPane.selected",ColorUIResource.GREEN);
     UIManager.put("TabbedPane.background",ColorUIResource.GREEN);
     UIManager.put("TabbedPane.shadow",ColorUIResource.GREEN);
  }

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

  class ChangeTab implements ChangeListener{
    public void stateChanged(ChangeEvent e){
        tab.validate();
        System.out.println(tab.getSelectedIndex());
        for(int i=0;i<tab.getTabCount();i++){
          if(i != tab.getSelectedIndex())
            tab.setBackgroundAt(i,Color.orange);
        }

    }
  }

  class myPanel extends JPanel{
    public myPanel(String str){
       add(new JLabel(str));
    }
  }

}
Run Code Online (Sandbox Code Playgroud)

Gru*_*eck 9

我使用了您的示例代码,对我来说有用的是将调用移动到UIManager.put()执行JTabbedPane构造函数之前执行它们的位置.

public class Main extends JFrame {
    JTabbedPane tab;

    public Main() {
       // ... other stuff
       UIManager.put("TabbedPane.contentAreaColor ",ColorUIResource.GREEN);
       UIManager.put("TabbedPane.selected",ColorUIResource.GREEN);
       UIManager.put("TabbedPane.background",ColorUIResource.GREEN);
       UIManager.put("TabbedPane.shadow",ColorUIResource.GREEN);

       // now construct the tabbed pane
       tab=new JTabbedPane();

       // ... other stuff
 }
Run Code Online (Sandbox Code Playgroud)

还有其他一些属性(至少金属L&F):

UIManager.put("TabbedPane.borderColor", Color.RED);
UIManager.put("TabbedPane.darkShadow", ColorUIResource.RED);
UIManager.put("TabbedPane.light", ColorUIResource.RED);
UIManager.put("TabbedPane.highlight", ColorUIResource.RED);
UIManager.put("TabbedPane.focus", ColorUIResource.RED);
UIManager.put("TabbedPane.unselectedBackground", ColorUIResource.RED);
UIManager.put("TabbedPane.selectHighlight", ColorUIResource.RED);
UIManager.put("TabbedPane.tabAreaBackground", ColorUIResource.RED);
UIManager.put("TabbedPane.borderHightlightColor", ColorUIResource.RED);
Run Code Online (Sandbox Code Playgroud)

这些可让您控制选项卡区域中的大多数颜色.

我发现在这些设置下,内容周围仍然有一个非常小的蓝色灰色边框.我搜索了如何设置这种颜色无济于事.我能找到解决这个问题的唯一解决方案是:

UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0));
Run Code Online (Sandbox Code Playgroud)

这是次优解决方案.