使用外观和感觉Nimbus将背景/前景更改为JTabbedPane和表头的缺陷

问题描述 投票:0回答:1

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

我的问题不是如何做到的? (因为这些是多个问题)。

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

当执行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

colors look-and-feel jtabbedpane nimbus jtableheader
1个回答
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();
    }
  }
© www.soinside.com 2019 - 2024. All rights reserved.