控制 Java 选项卡式窗格中的颜色

发布于 2024-08-18 16:15:38 字数 1749 浏览 4 评论 0原文

我一直在疯狂地试图解决这个问题。

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

}

I have been going nuts trying to figure this out.

I am trying to elimenate a light blue background that appears in a 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));
    }
  }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月下伊人醉 2024-08-25 16:15:38

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

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

这是一个次优解决方案。

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.

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

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.

沧桑㈠ 2024-08-25 16:15:38

使用这些值检查结果。

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

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

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. 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. If you only set contentAreaColor and do empty overrides for the methods

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.

怪我鬧 2024-08-25 16:15:38

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

} 

Attempt 2:
I fixed my border problem and changed look and feel manager. 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)); 
    } 
  } 

} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文