Colors 使用LookAndFeel Nimbus将背景/前景更改为选项卡窗格和表头的缺陷

Colors 使用LookAndFeel Nimbus将背景/前景更改为选项卡窗格和表头的缺陷,colors,look-and-feel,jtabbedpane,nimbus,jtableheader,Colors,Look And Feel,Jtabbedpane,Nimbus,Jtableheader,我有一个功能代码,但我想知道我的代码的缺陷 我的问题不是怎么做?(因为有多个问题) 我的问题是:我的方法有缺陷吗? 当执行setBackground或setForeground方法时,是否可以从basictabbedpaneuiColor调用repaint 出于奇怪的原因,我需要使用BasicTableHeaderUIColored类需要吗? 一些更改不需要重新绘制调用 不需要BasicTableHeaderUIColored类,只使用BasicTableHeaderUI类 构造函数的更改

我有一个功能代码,但我想知道我的代码的缺陷

我的问题不是怎么做?(因为有多个问题)

我的问题是:我的方法有缺陷吗?

当执行
setBackground
setForeground
方法时,是否可以从
basictabbedpaneuiColor
调用
repaint

出于奇怪的原因,我需要使用
BasicTableHeaderUIColored
需要吗?


一些更改不需要重新绘制调用

不需要
BasicTableHeaderUIColored
类,只使用
BasicTableHeaderUI

构造函数的更改

  public Table_TabbedPane_Nimbus_ChangeColor() {

    JTable table = new  JTable(new DefaultTableModel(
        new Object [][] { {null, null}, {null, null}, {null, null} },
        new String [] { "Title 1", "Title 2" }
    )) {
      @Override public void updateUI() {
        this.getTableHeader().setUI(new BasicTableHeaderUI());
      }
    };
    //Alternative to assign in instantiation time
    //table.getTableHeader().setUI(new BasicTableHeaderUI());

    JScrollPane scroll = new JScrollPane();
    scroll.setViewportView(table);
    scroll.setPreferredSize(new Dimension(80,80));

    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JTabbedPane tabbedPane = new JTabbedPane() {
      @Override public void updateUI() {
        setUI(new BasicTabbedPaneUIColored(this));
      }
    };
    //Alternative to assign in instantiation time
    //tabbedPane.setUI(new BasicTabbedPaneUIColored(tabbedPane));

    tabbedPane.setOpaque(true);
    tabbedPane.addTab("panel1", panel1);
    tabbedPane.addTab("panel2", panel2);

    JButton buttonBlue = new JButton("Blue");
    JButton buttonGreen = new JButton("Green");
    JButton buttonRed = new JButton("Red");

    buttonBlue.addActionListener(new ActionListener() {
      @Override public void actionPerformed( ActionEvent evt) {
        table.getTableHeader().setBackground(Color.blue);
        table.getTableHeader().setForeground(Color.yellow);
        tabbedPane.setBackground(Color.blue);
        tabbedPane.setForeground(Color.yellow);
      }
    });
    buttonGreen.addActionListener(new ActionListener() {
      @Override public void actionPerformed( ActionEvent evt) {
        table.getTableHeader().setBackground(Color.green);
        table.getTableHeader().setForeground(Color.magenta);
        tabbedPane.setBackground(Color.green);
        tabbedPane.setForeground(Color.magenta);
      }
    });
    buttonRed.addActionListener(new ActionListener() {
      @Override public void actionPerformed( ActionEvent evt) {
        table.getTableHeader().setBackground(Color.red);
        table.getTableHeader().setForeground(Color.cyan);
        tabbedPane.setBackground(Color.red);
        tabbedPane.setForeground(Color.cyan);
      }
    });

    JPanel buttonsPanel = new JPanel();
    buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.PAGE_AXIS));
    buttonsPanel.add(buttonBlue);
    buttonsPanel.add(buttonGreen);
    buttonsPanel.add(buttonRed);

    JPanel outer = new JPanel();
    outer.setLayout(new BoxLayout(outer, BoxLayout.LINE_AXIS));
    outer.add(buttonsPanel);
    outer.add(scroll);
    outer.add(tabbedPane);
    add(outer);

    setSize(600, 300);
    setVisible(true);
  }
basicTabbedPaneUIColor
类的一些更改

  public class BasicTabbedPaneUIColored extends BasicTabbedPaneUI {
    private final JTabbedPane tabbedPane;

    BasicTabbedPaneUIColored(JTabbedPane tabbedPane) {
      super();
      this.tabbedPane = tabbedPane;
    }

    @Override protected void paintTabBackground(Graphics g, int tabPlacement,
        int tabIndex, int x, int y, int w, int h, boolean isSelected) {
      g.setColor(tabbedPane.getBackground());
      switch (tabPlacement) {
        case SwingConstants.TOP:
          g.fillRect(x + 1, y + 1, w - 1, h - 1);
          break;
        case SwingConstants.BOTTOM:
          g.fillRect(x, y, w - 1, h - 1);
          break;
        case SwingConstants.LEFT:
          g.fillRect(x + 1, y + 1, w - 1, h - 2);
          break;
        case SwingConstants.RIGHT:
          g.fillRect(x, y + 1, w - 1, h - 2);
          break;
      }
    }

    @Override protected void paintText(Graphics g, int tabPlacement, Font font,
        FontMetrics metrics, int tabIndex, String title, Rectangle textRect,
        boolean isSelected) {
      g.setFont(font);
      View v = getTextViewForTab(tabIndex);
      if (v != null) {
          // html
          v.paint(g, textRect);
      } else {
        // plain text
        int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);

        if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
          g.setColor(tabbedPane.getForeground());
          SwingUtilities2.drawStringUnderlineCharAt(tabPane, g, title,
              mnemIndex, textRect.x, textRect.y + metrics.getAscent());
        } else { // tab disabled
          g.setColor(tabPane.getBackgroundAt(tabIndex).brighter());
          SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,title,
              mnemIndex, textRect.x, textRect.y + metrics.getAscent());
          g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
          SwingUtilities2.drawStringUnderlineCharAt(tabPane, g, title,
              mnemIndex, textRect.x - 1, textRect.y + metrics.getAscent() - 1);
        }
      }
    }
  }
如果您不想使用Swingutilities 2

JTabbedPane tabbedPane = new JTabbedPane() {
  @Override public void updateUI() {
    setUI(new BasicTabbedPaneUIColored(this, new JPanel().getBackground(), 
        new JPanel().getForeground()));
  }
};
//Alternative to assign in instantiation time
//tabbedPane.setUI(new BasicTabbedPaneUIColored(tabbedPane, tabbedPane.getBackground(), tabbedPane.getForeground()));
按钮上的更改

buttonBlue.addActionListener(new ActionListener() {
  @Override public void actionPerformed( ActionEvent evt) {
    if (tabbedPane.getUI() instanceof BasicTabbedPaneUIColored) {
      ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setBackground(Color.blue);
      ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setForeground(Color.yellow);
    } else {
      tabbedPane.setBackground(Color.blue);
      tabbedPane.setForeground(Color.yellow);
    }
    table.getTableHeader().setBackground(Color.blue);
    table.getTableHeader().setForeground(Color.yellow);
  }
});
buttonGreen.addActionListener(new ActionListener() {
  @Override public void actionPerformed( ActionEvent evt) {
    if (tabbedPane.getUI() instanceof BasicTabbedPaneUIColored) {
      ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setBackground(Color.green);
      ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setForeground(Color.magenta);
    } else {
      tabbedPane.setBackground(Color.green);
      tabbedPane.setForeground(Color.magenta);
    }
    table.getTableHeader().setBackground(Color.green);
    table.getTableHeader().setForeground(Color.magenta);
  }
});
buttonRed.addActionListener(new ActionListener() {
  @Override public void actionPerformed( ActionEvent evt) {
    if (tabbedPane.getUI() instanceof BasicTabbedPaneUIColored) {
      ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setBackground(Color.red);
      ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setForeground(Color.cyan);
    } else {
      tabbedPane.setBackground(Color.red);
      tabbedPane.setForeground(Color.cyan);
    }
    table.getTableHeader().setBackground(Color.red);
    table.getTableHeader().setForeground(Color.cyan);
  }
});
以及
basicTableHeaderUI类的更改

  public class BasicTabbedPaneUIColored extends BasicTabbedPaneUI {
    private Color background;
    private Color foreground;
    private final JTabbedPane tabbedPane;

    BasicTabbedPaneUIColored(JTabbedPane tabbedPane, Color background, Color foreground) {
      super();
      this.tabbedPane = tabbedPane;
      this.background = background;
      this.foreground = foreground;
    }

    @Override protected void paintTabBackground(Graphics g, int tabPlacement,
        int tabIndex, int x, int y, int w, int h, boolean isSelected) {
      g.setColor(this.background);
      switch (tabPlacement) {
        case SwingConstants.TOP:
          g.fillRect(x + 1, y + 1, w - 1, h - 1);
          break;
        case SwingConstants.BOTTOM:
          g.fillRect(x, y, w - 1, h - 1);
          break;
        case SwingConstants.LEFT:
          g.fillRect(x + 1, y + 1, w - 1, h - 2);
          break;
        case SwingConstants.RIGHT:
          g.fillRect(x, y + 1, w - 1, h - 2);
          break;
      }
    }

    @Override protected void paintText(Graphics g, int tabPlacement, Font font,
        FontMetrics metrics, int tabIndex, String title, Rectangle textRect,
        boolean isSelected) {
      g.setFont(font);
      View v = getTextViewForTab(tabIndex);
      if (v != null) {
          // html
          v.paint(g, textRect);
      } else {
        // plain text
        int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);


      if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
        g.setColor(this.foreground);
        BasicGraphicsUtils.drawStringUnderlineCharAt(g, title,
            mnemIndex, textRect.x, textRect.y + metrics.getAscent());
      } else { // tab disabled
        g.setColor(tabPane.getBackgroundAt(tabIndex).brighter());
        BasicGraphicsUtils.drawStringUnderlineCharAt(g,title,
            mnemIndex, textRect.x, textRect.y + metrics.getAscent());
        g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
        BasicGraphicsUtils.drawStringUnderlineCharAt(g, title,
            mnemIndex, textRect.x - 1, textRect.y + metrics.getAscent() - 1);
      }
      }
    }

    public Color getBackground() {
      return background;
    }

    public void setBackground(Color background) {
      this.background = background;
      this.tabbedPane.repaint();
    }

    public Color getForeground() {
      return foreground;
    }

    public void setForeground(Color foreground) {
      this.foreground = foreground;
      this.tabbedPane.repaint();
    }
  }

某些更改不需要重新绘制调用

不需要
BasicTableHeaderUIColored
类,只使用
BasicTableHeaderUI

构造函数的更改

  public Table_TabbedPane_Nimbus_ChangeColor() {

    JTable table = new  JTable(new DefaultTableModel(
        new Object [][] { {null, null}, {null, null}, {null, null} },
        new String [] { "Title 1", "Title 2" }
    )) {
      @Override public void updateUI() {
        this.getTableHeader().setUI(new BasicTableHeaderUI());
      }
    };
    //Alternative to assign in instantiation time
    //table.getTableHeader().setUI(new BasicTableHeaderUI());

    JScrollPane scroll = new JScrollPane();
    scroll.setViewportView(table);
    scroll.setPreferredSize(new Dimension(80,80));

    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JTabbedPane tabbedPane = new JTabbedPane() {
      @Override public void updateUI() {
        setUI(new BasicTabbedPaneUIColored(this));
      }
    };
    //Alternative to assign in instantiation time
    //tabbedPane.setUI(new BasicTabbedPaneUIColored(tabbedPane));

    tabbedPane.setOpaque(true);
    tabbedPane.addTab("panel1", panel1);
    tabbedPane.addTab("panel2", panel2);

    JButton buttonBlue = new JButton("Blue");
    JButton buttonGreen = new JButton("Green");
    JButton buttonRed = new JButton("Red");

    buttonBlue.addActionListener(new ActionListener() {
      @Override public void actionPerformed( ActionEvent evt) {
        table.getTableHeader().setBackground(Color.blue);
        table.getTableHeader().setForeground(Color.yellow);
        tabbedPane.setBackground(Color.blue);
        tabbedPane.setForeground(Color.yellow);
      }
    });
    buttonGreen.addActionListener(new ActionListener() {
      @Override public void actionPerformed( ActionEvent evt) {
        table.getTableHeader().setBackground(Color.green);
        table.getTableHeader().setForeground(Color.magenta);
        tabbedPane.setBackground(Color.green);
        tabbedPane.setForeground(Color.magenta);
      }
    });
    buttonRed.addActionListener(new ActionListener() {
      @Override public void actionPerformed( ActionEvent evt) {
        table.getTableHeader().setBackground(Color.red);
        table.getTableHeader().setForeground(Color.cyan);
        tabbedPane.setBackground(Color.red);
        tabbedPane.setForeground(Color.cyan);
      }
    });

    JPanel buttonsPanel = new JPanel();
    buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.PAGE_AXIS));
    buttonsPanel.add(buttonBlue);
    buttonsPanel.add(buttonGreen);
    buttonsPanel.add(buttonRed);

    JPanel outer = new JPanel();
    outer.setLayout(new BoxLayout(outer, BoxLayout.LINE_AXIS));
    outer.add(buttonsPanel);
    outer.add(scroll);
    outer.add(tabbedPane);
    add(outer);

    setSize(600, 300);
    setVisible(true);
  }
basicTabbedPaneUIColor
类的一些更改

  public class BasicTabbedPaneUIColored extends BasicTabbedPaneUI {
    private final JTabbedPane tabbedPane;

    BasicTabbedPaneUIColored(JTabbedPane tabbedPane) {
      super();
      this.tabbedPane = tabbedPane;
    }

    @Override protected void paintTabBackground(Graphics g, int tabPlacement,
        int tabIndex, int x, int y, int w, int h, boolean isSelected) {
      g.setColor(tabbedPane.getBackground());
      switch (tabPlacement) {
        case SwingConstants.TOP:
          g.fillRect(x + 1, y + 1, w - 1, h - 1);
          break;
        case SwingConstants.BOTTOM:
          g.fillRect(x, y, w - 1, h - 1);
          break;
        case SwingConstants.LEFT:
          g.fillRect(x + 1, y + 1, w - 1, h - 2);
          break;
        case SwingConstants.RIGHT:
          g.fillRect(x, y + 1, w - 1, h - 2);
          break;
      }
    }

    @Override protected void paintText(Graphics g, int tabPlacement, Font font,
        FontMetrics metrics, int tabIndex, String title, Rectangle textRect,
        boolean isSelected) {
      g.setFont(font);
      View v = getTextViewForTab(tabIndex);
      if (v != null) {
          // html
          v.paint(g, textRect);
      } else {
        // plain text
        int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);

        if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
          g.setColor(tabbedPane.getForeground());
          SwingUtilities2.drawStringUnderlineCharAt(tabPane, g, title,
              mnemIndex, textRect.x, textRect.y + metrics.getAscent());
        } else { // tab disabled
          g.setColor(tabPane.getBackgroundAt(tabIndex).brighter());
          SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,title,
              mnemIndex, textRect.x, textRect.y + metrics.getAscent());
          g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
          SwingUtilities2.drawStringUnderlineCharAt(tabPane, g, title,
              mnemIndex, textRect.x - 1, textRect.y + metrics.getAscent() - 1);
        }
      }
    }
  }
如果您不想使用Swingutilities 2

JTabbedPane tabbedPane = new JTabbedPane() {
  @Override public void updateUI() {
    setUI(new BasicTabbedPaneUIColored(this, new JPanel().getBackground(), 
        new JPanel().getForeground()));
  }
};
//Alternative to assign in instantiation time
//tabbedPane.setUI(new BasicTabbedPaneUIColored(tabbedPane, tabbedPane.getBackground(), tabbedPane.getForeground()));
按钮上的更改

buttonBlue.addActionListener(new ActionListener() {
  @Override public void actionPerformed( ActionEvent evt) {
    if (tabbedPane.getUI() instanceof BasicTabbedPaneUIColored) {
      ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setBackground(Color.blue);
      ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setForeground(Color.yellow);
    } else {
      tabbedPane.setBackground(Color.blue);
      tabbedPane.setForeground(Color.yellow);
    }
    table.getTableHeader().setBackground(Color.blue);
    table.getTableHeader().setForeground(Color.yellow);
  }
});
buttonGreen.addActionListener(new ActionListener() {
  @Override public void actionPerformed( ActionEvent evt) {
    if (tabbedPane.getUI() instanceof BasicTabbedPaneUIColored) {
      ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setBackground(Color.green);
      ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setForeground(Color.magenta);
    } else {
      tabbedPane.setBackground(Color.green);
      tabbedPane.setForeground(Color.magenta);
    }
    table.getTableHeader().setBackground(Color.green);
    table.getTableHeader().setForeground(Color.magenta);
  }
});
buttonRed.addActionListener(new ActionListener() {
  @Override public void actionPerformed( ActionEvent evt) {
    if (tabbedPane.getUI() instanceof BasicTabbedPaneUIColored) {
      ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setBackground(Color.red);
      ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setForeground(Color.cyan);
    } else {
      tabbedPane.setBackground(Color.red);
      tabbedPane.setForeground(Color.cyan);
    }
    table.getTableHeader().setBackground(Color.red);
    table.getTableHeader().setForeground(Color.cyan);
  }
});
以及
basicTableHeaderUI类的更改

  public class BasicTabbedPaneUIColored extends BasicTabbedPaneUI {
    private Color background;
    private Color foreground;
    private final JTabbedPane tabbedPane;

    BasicTabbedPaneUIColored(JTabbedPane tabbedPane, Color background, Color foreground) {
      super();
      this.tabbedPane = tabbedPane;
      this.background = background;
      this.foreground = foreground;
    }

    @Override protected void paintTabBackground(Graphics g, int tabPlacement,
        int tabIndex, int x, int y, int w, int h, boolean isSelected) {
      g.setColor(this.background);
      switch (tabPlacement) {
        case SwingConstants.TOP:
          g.fillRect(x + 1, y + 1, w - 1, h - 1);
          break;
        case SwingConstants.BOTTOM:
          g.fillRect(x, y, w - 1, h - 1);
          break;
        case SwingConstants.LEFT:
          g.fillRect(x + 1, y + 1, w - 1, h - 2);
          break;
        case SwingConstants.RIGHT:
          g.fillRect(x, y + 1, w - 1, h - 2);
          break;
      }
    }

    @Override protected void paintText(Graphics g, int tabPlacement, Font font,
        FontMetrics metrics, int tabIndex, String title, Rectangle textRect,
        boolean isSelected) {
      g.setFont(font);
      View v = getTextViewForTab(tabIndex);
      if (v != null) {
          // html
          v.paint(g, textRect);
      } else {
        // plain text
        int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);


      if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
        g.setColor(this.foreground);
        BasicGraphicsUtils.drawStringUnderlineCharAt(g, title,
            mnemIndex, textRect.x, textRect.y + metrics.getAscent());
      } else { // tab disabled
        g.setColor(tabPane.getBackgroundAt(tabIndex).brighter());
        BasicGraphicsUtils.drawStringUnderlineCharAt(g,title,
            mnemIndex, textRect.x, textRect.y + metrics.getAscent());
        g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
        BasicGraphicsUtils.drawStringUnderlineCharAt(g, title,
            mnemIndex, textRect.x - 1, textRect.y + metrics.getAscent() - 1);
      }
      }
    }

    public Color getBackground() {
      return background;
    }

    public void setBackground(Color background) {
      this.background = background;
      this.tabbedPane.repaint();
    }

    public Color getForeground() {
      return foreground;
    }

    public void setForeground(Color foreground) {
      this.foreground = foreground;
      this.tabbedPane.repaint();
    }
  }