【问题标题】:JTabbedPane - set default border around tabs..?JTabbedPane - 在选项卡周围设置默认边框..?
【发布时间】:2010-06-26 15:55:13
【问题描述】:

我在我的应用程序中使用了 JTabbedPane。我添加了两个选项卡,它们是自定义类“ContentPanel”的实例。这扩展了 JPanel 并设置了背景、边框等。基本上这意味着我不必设置要应用此配色方案的每个 JPanel 的属性。我注意到不仅它们的边框出现,而且另一个边框(我认为是蓝色的 - 至少在我的屏幕上)出现在这个边框周围,连接到选项卡“选择器”本身(即您单击以获取的按钮适当的观点)。我想改变这个边框,因为它在金色/棕色配色方案中看起来很奇怪。有谁知道如何做到这一点?我试过 JTabbedPane.setBorder(Border b) 但这不起作用。这只是在整个事物周围设置了一个边框,包括选项卡选择器......不是我想要的。

对此的任何帮助将不胜感激。

【问题讨论】:

    标签: java swing border jtabbedpane


    【解决方案1】:

    使用“UIManager”改变外观

                UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Enabled].backgroundPainter", new BackgroundPainter(Color.white));
                UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Enabled+MouseOver].backgroundPainter", new BackgroundPainter(Color.white));
                UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Enabled+Pressed].backgroundPainter", new BackgroundPainter(Color.white));
                UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Focused+MouseOver+Selected].backgroundPainter", new BackgroundPainter(Color.white));
                UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Focused+Pressed+Selected].backgroundPainter", new BackgroundPainter(Color.white));
                UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Focused+Selected].backgroundPainter", new BackgroundPainter(Color.GRAY));
                UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[MouseOver+Selected].backgroundPainter", new BackgroundPainter(Color.white));
                UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Pressed+Selected].backgroundPainter", new BackgroundPainter(Color.white));
                UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Selected].backgroundPainter", new BackgroundPainter(Color.white));
    

    BackgroundPainter 类

    public class BackgroundPainter implements Painter<JComponent> {
    
    private Color color = null;
    
    BackgroundPainter(Color c) {
        color = c;
    }
    
    @Override
    public void paint(Graphics2D g, JComponent object, int width, int height) {
        if (color != null) {
            g.setColor(color);
            g.fillRect(0, 0, width - 1, height - 1);
        }
    }
    

    }

    【讨论】:

      【解决方案2】:

      这些颜色在外观中定义。如果您查看BasicTabbedPaneUI 的代码,您会注意到installDefaults() 设置了一堆protected Color 实例变量。它们在 L&F 中定义的键也可在此处获得。

      protected void installDefaults() {
          LookAndFeel.installColorsAndFont(tabPane, "TabbedPane.background",
                                      "TabbedPane.foreground", "TabbedPane.font");     
          highlight = UIManager.getColor("TabbedPane.light");
          lightHighlight = UIManager.getColor("TabbedPane.highlight");
          shadow = UIManager.getColor("TabbedPane.shadow");
          darkShadow = UIManager.getColor("TabbedPane.darkShadow");
          //...
          // a lot more stuff
          //...
      }
      

      如果您不想定义自己的 L&F,您可以在选项卡式窗格中设置自定义 UI 委托:

      myTabbedPane.setUI(new BasicTabbedPaneUI() {
         @Override
         protected void installDefaults() {
             super.installDefaults();
             highlight = Color.pink;
             lightHighlight = Color.green;
             shadow = Color.red;
             darkShadow = Color.cyan;
             focus = Color.yellow;
         }
      });
      

      您当然可能想要更改这些颜色设置。设置后,您将看到在哪里使用了哪些变量。

      【讨论】:

      • 是的,这看起来像我需要的......谢谢。如果我有任何问题,将再次发表评论。理查德
      • hmm 似乎工作但不完全。我查看了 BasicTabbedPaneUI 的所有变量并设置了那里列出的所有颜色,但那里仍然有一个稍微薄一点的浅蓝色/青绿色边框。它与选项卡“选择器”本身的颜色相同..
      • 解决了——我找到了别人的帖子,说要使用 UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0));就像你放的一样。现在完全摆脱了边界。再次感谢。理查德
      【解决方案3】:

      不影响 L&F 和 JVM 运行时系统范围的设置代码解决方案。

      创建您自己的选项卡式窗格类和嵌套的选项卡式窗格-UI 类来处理“特定”类选项卡式窗格的问题。下面的代码是原始的:(最后一个答案是 2010 年,但这也可能有用。)

      public class DisplayTabbedPane extends JTabbedPane implements 
           MouseListener, ChangeListener {
      
          public DisplayTabbedPane() {
      
              setTabPlacement(SwingConstants.BOTTOM);
      
              // UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0)); 
              // works but is a JVM system wide change rather than a specific change
              NoInsetTabbedPaneUI ui = new NoInsetTabbedPaneUI();
      
              // this will build the L&F settings for various tabbed UI components.
              setUI( ui );
      
              // override the content border insets to remove the tabbed-pane
              // blue border around the pane
              ui.overrideContentBorderInsetsOfUI();
      
          }
      
          /**
           * Class to modify the UI layout of tabbed-pane which we wish to override
           * in some way. This modification only applies to objects of this class.
           * Doing UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0)); 
           * would affect all tabbed-panes in the JVM run-time.
           * 
           * This is free to use, no copyright but is "AS IS".
           */
          class NoInsetTabbedPaneUI extends MetalTabbedPaneUI {
              /**
               * Create tabbed-pane-UI object to allow fine control of the
               * L&F of this specific object.
               */
              NoInsetTabbedPaneUI(){
                  super();
              }
              /**
               * Override the content border insets of the UI which represent
               * the L&F of the border around the pane. In this case only care
               * about having a bottom inset.
               */
              public void overrideContentBorderInsetsOfUI(){
                  this.contentBorderInsets.top = 0;
                  this.contentBorderInsets.left = 0;
                  this.contentBorderInsets.right = 0;
                  this.contentBorderInsets.bottom = 2;        
              }
          }
          ........
      
      }
      

      【讨论】:

        猜你喜欢
        • 2015-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-18
        • 2012-09-16
        • 1970-01-01
        相关资源
        最近更新 更多