简体   繁体   English

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

[英]Controlling Color in Java Tabbed Pane

I have been going nuts trying to figure this out. 我一直在努力解决这个问题。

I am trying to elimenate a light blue background that appears in a JTabbedPane. 我试图在JTabbedPane中出现一个淡蓝色背景。 I've tried everything and nothing seems to work. 我已经尝试了一切,似乎没有任何工作。

Below is my code. 以下是我的代码。 If you run it, it will show the tab, when selected with a light blue background and a thing blue border at the top. 如果您运行它,它将显示选项卡,当选择浅蓝色背景和顶部的东西蓝色边框。 I want to control this color. 我想控制这种颜色。 But how? 但是怎么样?

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));
    }
  }

}

I used your example code, and what worked for me was moving the calls to UIManager.put() to a point where they would be executed before the JTabbedPane constructor was executed. 我使用了您的示例代码,对我来说有用的是将对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
 }

There's also some other properties available (for the Metal L&F, at least): 还有其他一些属性(至少金属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);

These let you control most of the colours in the tab area. 这些可让您控制选项卡区域中的大多数颜色。

I found with these settings there was still a very small blue-ish grey border around the content. 我发现在这些设置下,内容周围仍然有一个非常小的蓝色灰色边框。 I have searched for how to set this colour to no avail. 我搜索了如何设置这种颜色无济于事。 The only solution I could find to get rid of this was: 我能找到解决这个问题的唯一解决方案是:

UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0));

Which is a sub-optimal solution. 这是次优解决方案。

Check the results with these values. 使用这些值检查结果。

UIManager.put("TabbedPane.contentAreaColor", Color.GREEN);
UIManager.put("TabbedPane.light", ColorUIResource.GREEN);
UIManager.put("TabbedPane.highlight", ColorUIResource.GREEN);
UIManager.put("TabbedPane.shadow", ColorUIResource.GREEN);
UIManager.put("TabbedPane.darkShadow", ColorUIResource.GREEN);
UIManager.put("TabbedPane.selected", ColorUIResource.GREEN);
UIManager.put("TabbedPane.borderHightlightColor", ColorUIResource.GREEN);

As you can see, the only way, to get the dark border on top of the panel the color you want is to set "borderHightlightColor" to the desired Color. 如您所见,唯一的方法是,在面板顶部获得所需颜色的暗边框,将“borderHightlightColor”设置为所需的颜色。 Unfortunately, this has the side effects that can be seen (green border around all tabs). 不幸的是,这有副作用(所有标签周围的绿色边框)。 And still, there is this gray line between the green ones that comes from the background. 而且,来自背景的绿色线条之间存在这条灰色线条。

I think the only real solution to this is to override your MetalTabbedPaneUI. 我认为唯一真正的解决方案是覆盖你的MetalTabbedPaneUI。 If you only set contentAreaColor and do empty overrides for the methods 如果只设置contentAreaColor并为方法执行空覆盖

paintContentBorderTopEdge(g, tabPlacement, selectedIndex, x, y, w, h);
paintContentBorderLeftEdge(g, tabPlacement, selectedIndex, x, y, w, h); 
paintContentBorderBottomEdge(g, tabPlacement, selectedIndex, x, y, w, h);
paintContentBorderRightEdge(g, tabPlacement, selectedIndex, x, y, w, h); 

the result should be near the one I suspect you want to get. 结果应该接近我怀疑你想得到的那个。

Attempt 2: I fixed my border problem and changed look and feel manager. 尝试2:我修复了我的边框问题,改变了外观和感觉经理。 This still is not exactly what you are looking for though.. 这仍然不是你想要的,但..

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() { 

     setBackground(Color.white);
     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()); 
     tab.setBackground(Color.white);
     tab.setForeground(Color.black);
     tab.setBorder(BorderFactory.createEmptyBorder());


     getContentPane().add(tab,BorderLayout.CENTER); 
     setVisible(true); 

  } 

  public static void main (String[] args) throws Exception { 
                           UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
    Main main = new Main(); 
  } 

  class ChangeTab implements ChangeListener{ 
    public void stateChanged(ChangeEvent e){ 
        tab.validate(); 
        System.out.println(tab.getSelectedIndex()); 

    } 
  } 

  class myPanel extends JPanel{ 
    public myPanel(String str){ 
       setBackground(Color.white); 
       setBorder(BorderFactory.createEmptyBorder());
       add(new JLabel(str)); 
    } 
  } 

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM