使用LookAndFeel Nimbus将{Background / Foreground更改为TabbedPane和TableHeader的缺陷

时间:2018-03-02 05:13:29

标签: colors look-and-feel jtabbedpane nimbus jtableheader

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

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

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

执行repaintBasicTabbedPaneUIColored方法时,是否可以从 setBackground 调用setForeground

由于奇怪的原因,我需要使用 BasicTableHeaderUIColored 类。 是否需要?

enter image description here

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import javax.swing.plaf.basic.*;
import javax.swing.text.View;
import sun.swing.SwingUtilities2;

public class Table_TabbedPane_Nimbus_ChangeColor extends JFrame {

  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"
      }
    ));
    table.getTableHeader().setUI(new BasicTableHeaderUIColored());

    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.getBackground(), this.getForeground()));
      }
    };

    tabbedPane.setUI(new BasicTabbedPaneUIColored(Color.black, Color.white));
    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);
        if (tabbedPane.getUI() instanceof BasicTabbedPaneUIColored) {
          ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setBackground(Color.blue);
          ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setForeground(Color.yellow);
          tabbedPane.repaint();
          //tabbedPane.updateUI();
        } else {
          tabbedPane.setBackground(Color.blue);
        }
      }
    });
    buttonGreen.addActionListener(new ActionListener() {
      @Override public void actionPerformed( ActionEvent evt) {
        table.getTableHeader().setBackground(Color.green);
        table.getTableHeader().setForeground(Color.magenta);
        if (tabbedPane.getUI() instanceof BasicTabbedPaneUIColored) {
          ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setBackground(Color.green);
          ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setForeground(Color.magenta);
          tabbedPane.repaint();
          //tabbedPane.updateUI();
        } else {
          tabbedPane.setBackground(Color.green);
        }
      }
    });
    buttonRed.addActionListener(new ActionListener() {
      @Override public void actionPerformed( ActionEvent evt) {
        table.getTableHeader().setBackground(Color.red);
        table.getTableHeader().setForeground(Color.cyan);
        if (tabbedPane.getUI() instanceof BasicTabbedPaneUIColored) {
          ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setBackground(Color.red);
          ((BasicTabbedPaneUIColored)tabbedPane.getUI()).setForeground(Color.cyan);
          tabbedPane.repaint();
          //tabbedPane.updateUI();
        } else {
          tabbedPane.setBackground(Color.red);
        }
      }
    });

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

  public class BasicTableHeaderUIColored extends BasicTableHeaderUI {
    @Override public void paint(Graphics g, JComponent c) {
      super.paint(g, c);
    }
  }

  public class BasicTabbedPaneUIColored extends BasicTabbedPaneUI {
    private Color background;
    private Color foreground;

    BasicTabbedPaneUIColored(Color background, Color foreground) {
      super();
      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);
              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);
          }
      }
    }

    public Color getBackground() {
      return background;
    }

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

    public Color getForeground() {
      return foreground;
    }

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

  }

  public static void main( String args[] ) {
  try {
    UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
  } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
    System.out.println("ex:" + ex.toString());
  }
    Table_TabbedPane_Nimbus_ChangeColor app = new Table_TabbedPane_Nimbus_ChangeColor();
    app.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  }
}

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

某些更改没有重新调用。

不需要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);
  }

BasicTabbedPaneUIColored

上的一些更改
  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);
        }
      }
    }
  }

如果您不想使用 SwingUtilities2

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

BasicTableHeaderUIColored上的更改。

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