覆盖Java的默认外观

覆盖Java的默认外观,第1张

覆盖Java的默认外观

自定义GUI类

package com.ibm.gui;import javax.swing.*;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.basic.BasicTabbedPaneUI;import java.awt.*;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;public class AizCustomGUI extends BasicTabbedPaneUI {    private static final Insets NO_INSETS = new Insets(0, 0, 0, 0);    private ColorSet selectedColorSet;    private ColorSet defaultColorSet;    private ColorSet hoverColorSet;    private boolean contentTopBorderDrawn = true;    private Color lineColor = new Color(158, 158, 158);    private Color dividerColor = new Color(200, 200, 200);    private Insets contentInsets = new Insets(10, 10, 10, 10);    private int lastRollOverTab = -1;    public static ComponentUI createUI(JComponent c) {        return new AizCustomGUI();    }    public AizCustomGUI() {        selectedColorSet = new ColorSet();        selectedColorSet.topGradColor1 = new Color(198, 198, 197);        selectedColorSet.topGradColor2 = Color.WHITE;        selectedColorSet.bottomGradColor1 = Color.WHITE;        selectedColorSet.bottomGradColor2 = Color.WHITE;        defaultColorSet = new ColorSet();        defaultColorSet.topGradColor1 = Color.WHITE;        defaultColorSet.topGradColor2 = Color.WHITE;        defaultColorSet.bottomGradColor1 = Color.WHITE;        defaultColorSet.bottomGradColor2 = new Color(198, 198, 197);        hoverColorSet = new ColorSet();        hoverColorSet.topGradColor1 = new Color(244, 244, 244);        hoverColorSet.topGradColor2 = new Color(223, 223, 223);        hoverColorSet.bottomGradColor1 = new Color(211, 211, 211);        hoverColorSet.bottomGradColor2 = new Color(235, 235, 235);        maxTabHeight = 20;        setContentInsets(0);    }    public void setContentTopBorderDrawn(boolean b) {        contentTopBorderDrawn = b;    }    public void setContentInsets(Insets i) {        contentInsets = i;    }    public void setContentInsets(int i) {        contentInsets = new Insets(i, i, i, i);    }    public int getTabRunCount(JTabbedPane pane) {        return 1;    }    @Override    protected void installDefaults() {        super.installDefaults();        RollOverListener l = new RollOverListener();        tabPane.addMouseListener(l);        tabPane.addMouseMotionListener(l);        tabAreaInsets = NO_INSETS;        tabInsets = new Insets(0, 0, 0, 0);    }    protected boolean scrollableTabLayoutEnabled() {        return false;    }    @Override    protected Insets getContentBorderInsets(int tabPlacement) {        return contentInsets;    }    @Override    protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight) {        return super.calculateTabHeight(tabPlacement, tabIndex, fontHeight);        //  return 21;    }    @Override    protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) {        int w = super.calculateTabWidth(tabPlacement, tabIndex, metrics);        int wid = metrics.charWidth('M');        w += wid * 2;        return w;    }    @Override    protected int calculateMaxTabHeight(int tabPlacement) {        return super.calculateMaxTabHeight(tabPlacement);        //return 21;    }    @Override    protected void paintTabArea(Graphics g, int tabPlacement, int selectedIndex) {        g.setColor(Color.WHITE);        g.fillRoundRect(0, 0, tabPane.getWidth(), tabPane.getHeight(), 10, 10);        super.paintTabArea(g, tabPlacement, selectedIndex);    }    @Override    protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {        Graphics2D g2d = (Graphics2D) g;        ColorSet colorSet;        Rectangle rect = rects[tabIndex];        if (isSelected) { colorSet = selectedColorSet;        } else if (getRolloverTab() == tabIndex) { //colorSet = hoverColorSet; colorSet = selectedColorSet;        } else { colorSet = defaultColorSet;        }        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,     RenderingHints.VALUE_ANTIALIAS_ON);        int width = rect.width;        int xpos = rect.x;        if (tabIndex > 0) { width--; xpos++;        }        g2d.setPaint(new GradientPaint(xpos, 0, colorSet.topGradColor1, xpos,     10, colorSet.topGradColor2));        g2d.fillRoundRect(xpos, 0, width, 10, 10, 10);        g2d.setPaint(new GradientPaint(0, 10, colorSet.bottomGradColor1, 0, 21,     colorSet.bottomGradColor2));        g2d.fillRoundRect(xpos, 10, width, 11, 10, 10);        if (contentTopBorderDrawn) { g2d.setColor(lineColor); g2d.drawLine(rect.x, 20, rect.x + rect.width - 1, 20);        }    }    @Override    protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {        g.setColor(dividerColor);        g.drawRoundRect(x, y, w, tabPane.getHeight(), 10, 10);    }    @Override    protected void paintContentBorderTopEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w, int h) {    }    @Override    protected void paintContentBorderRightEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w, int h) {        // Do nothing    }    @Override    protected void paintContentBorderLeftEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w, int h) {        // Do nothing    }    @Override    protected void paintContentBorderBottomEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w, int h) {        // Do nothing    }    @Override    protected void paintFocusIndicator(Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex, Rectangle iconRect, Rectangle textRect, boolean isSelected) {        // Do nothing    }    @Override    protected int getTabLabelShiftY(int tabPlacement, int tabIndex, boolean isSelected) {        return 0;    }    private class ColorSet {        Color topGradColor1;        Color topGradColor2;        Color bottomGradColor1;        Color bottomGradColor2;    }    private class RollOverListener implements MouseMotionListener, MouseListener {        public void mouseDragged(MouseEvent e) {        }        public void mouseMoved(MouseEvent e) { checkRollOver();        }        public void mouseClicked(MouseEvent e) {        }        public void mousePressed(MouseEvent e) {        }        public void mouseReleased(MouseEvent e) {        }        public void mouseEntered(MouseEvent e) { checkRollOver();        }        public void mouseExited(MouseEvent e) { tabPane.repaint();        }        private void checkRollOver() { int currentRollOver = getRolloverTab(); if (currentRollOver != lastRollOverTab) {     lastRollOverTab = currentRollOver;     Rectangle tabsRect = new Rectangle(0, 0, tabPane.getWidth(), tabPane.getHeight());     tabPane.repaint(tabsRect); }        }    }}

调用您的自定义GUI类

import java.awt.Color;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.Jframe;import javax.swing.JPanel;import javax.swing.JTabbedPane;import javax.swing.UIManager;public class TestPSTabbedPaneUI{    public static void main(String[] args)    {        try        { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");        }        catch (Exception exc)        { exc.printStackTrace();        }        Jframe vframe = new Jframe();        vframe.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);        vframe.setSize(200, 200);        JTabbedPane vTab = new JTabbedPane();     vTab.setUI(new AizCustomGUI() );        vTab.add("One", new JPanel());        JPanel vPanel2 = new JPanel();        vPanel2.setBorder(BorderFactory.createLineBorder(Color.BLACK,2));        vTab.add("Two", vPanel2);        vTab.add("Three", new JButton("three"));        vframe.getContentPane().add(vTab);        vframe.setTitle("Tabs Example");        vframe.show();    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5506512.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存