代码之家  ›  专栏  ›  技术社区  ›  Elliott

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

  •  6
  • Elliott  · 技术社区  · 16 年前

    我一直在想方设法搞清楚这件事。

    我试图消除出现在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));
        }
      }
    
    }
    
    3 回复  |  直到 16 年前
        1
  •  9
  •   Grundlefleck anujkk    16 年前

    我使用了您的示例代码,对我有用的是将调用转移到 UIManager.put() 在执行jtabedpane构造函数之前执行它们。

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

    这是一个次优解。

        2
  •  2
  •   Mathias Weyel    16 年前

    用这些值检查结果。

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

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

        3
  •  0
  •   AJ.    16 年前

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