【问题标题】:Controlling Color in Java Tabbed Pane在 Java 选项卡式窗格中控制颜色
【发布时间】:2011-01-08 09:48:41
【问题描述】:

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

我正在尝试消除出现在 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));
    }
  }

}

【问题讨论】:

  • 在显示 JFrame 之前,您是否尝试过使用不同的 LookAndFeel 或使用 UIManager.put

标签: java swing jtabbedpane


【解决方案1】:

使用这些值检查结果。

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

如您所见,要在面板顶部获得所需颜色的深色边框的唯一方法是将“borderHightlightColor”设置为所需的颜色。不幸的是,这具有可以看到的副作用(所有选项卡周围的绿色边框)。尽管如此,在来自背景的绿色线条之间还是有一条灰线。

我认为唯一真正的解决方案是覆盖您的 MetalTabbedPaneUI。如果您只设置 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); 

结果应该接近我怀疑你想要得到的结果。

【讨论】:

    【解决方案2】:

    尝试 2: 我修复了我的边界问题并更改了外观和感觉管理器。不过,这仍然不是您要寻找的东西..

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

    【讨论】:

    • setBorder() 采用 javax.swing.border.Border,而不是 Color 对象。颜色是否取自 Border 对象?
    • 有人发布了答案,但现在它已经消失了。为什么? Stackoverflow 中的错误?答案是先在 UIManager 中设置参数,然后再创建对象。反过来做,代码就不行了。
    • @Elliot:我自己删除了它,因为它不能解决您的问题,只能解决一部分问题。我仍然看到内面板周围的蓝灰色边框。即使使用这个答案也是如此。
    • 不确定这有什么帮助。如果在 myPanel 方法下将背景设置为绿色,还是有问题。
    【解决方案3】:

    我使用了您的示例代码,对我有用的是将调用移动到 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
     }
    

    还有一些其他可用的属性(至少对于金属 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);
    

    这些让您可以控制选项卡区域中的大部分颜色。

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

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

    这是一个次优的解决方案。

    【讨论】:

    • 实际上,这对我有用。为什么称它为次优?
    • 因为,我不确定这一点,它会使选项卡的内容正好靠在边缘。例如。如果您在其中有一个组件,例如北部区域的按钮,则按钮的顶部将接触选项卡的顶部,这可能不是您想要控制颜色时想要的。
    • 是的。我考虑过这一点,但能够通过在 TabbedPane 访问的容器的内容区域周围放置一个边框来解决它。边框不是 JTabbedPane 本身的属性,而是容器的属性。我完全可以控制该边界,从而解决了问题(尽管不可否认,这不是最优雅的解决方案)。但是话又说回来,这个 Swing 控件的实现也不是。
    猜你喜欢
    • 2010-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多