当前位置: 首页 > 面试题库 >

覆盖Java的默认外观

栾弘新
2023-03-14
问题内容

我想覆盖Java外观。我只想显示不同的按钮。
我想要Windows Look and Feel的所有功能,但仅按钮有所不同。希望你明白我的意思。

Color color = new Color(220, 220, 220, 200);
        UIManager.put("OptionPane.background", color);
        UIManager.put("Panel.background", color);
        UIManager.put("Button.foreground", new Color(255, 255, 255, 255));      
        List<Object> gradients = new ArrayList<Object>(5);
        gradients.add(0.00f);
        gradients.add(0.00f);
        gradients.add(new Color(0xC1C1C1));
        gradients.add(new Color(0xFFFFFF));
        gradients.add(new Color(0x5C5D5C));
        UIManager.put("Button.gradient", gradients);
        UIManager.put("Button.highlight",Color.RED);

        UIManager.setLookAndFeel(com.sun.java.swing.plaf.windows.WindowsLookAndFeel);

还告诉我如何制作圆形的JtabbedPane ???


问题答案:

自定义GUI类

package com.ibm.gui;
/**
*Auhor az
**/

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


 类似资料:
  • 问题内容: 我已经能够覆盖所有名称以“ android:”为前缀的主题,但是Android themes.xml还定义了似乎无法被覆盖的属性。例如: colorTheground是在Theme.Light xml中定义的,但是在此处添加它可以使我 错误。如何为整个应用程序覆盖该样式? 问题答案: 您可以用修改属性(如)的方式覆盖标准属性,只是不要忘记添加如下前缀:

  • 问题内容: 我有一个模板与此: Django自动将此翻译为Terminarsesión西班牙语。但是,我想将其翻译为Cerrarsesión。 我试图将此文字添加到.po文件中,但是在编译消息时出现错误,指出该文字重复。 有没有一种方法可以更改/覆盖默认的Django翻译? 谢谢。 问题答案: 最简单的方法是收集在django.contrib.admin语言环境文件夹中找到的.po文件,然后重新编

  • 我试图将Spring应用程序(大部分)转换为Spring Boot应用程序。在应用程序中,我有一个HTTP基本过滤器,收集用户名和密码,然后在DataSource实现中作为变量传递。 在这个数据源中,getConnection()方法是这样的: (由于StackOverflow格式问题,\n作为新行) 在Spring中,我可以毫无问题地实现@autowiledPrivate DataSource

  • 问题内容: 我有一个静态html,imgs,flash内容文件夹,它位于webapp文件夹之外。现在,我正在使用符号链接将该文件夹映射到我的webapp目录中。我的问题是,当我取消部署应用程序时,它会遵循符号链接并删除所有这些文件。 我尝试实现的解决方案之一是特殊的servlet,它包装了默认的servlet,但是使用了不同的相对路径。我在找出如何以覆盖默认servlet路径的方式包装默认serv

  • 我有一个RESTAPI,我不想强迫客户端发送请求参数。我有将近400个api方法,我不想将所有参数设置为“required=false” 我想覆盖Spring RequestParam的默认行为。我想将RequestParam接口的“required”属性的默认值设置为“false”。 有什么方法可以覆盖它吗?如果我不能或这不是最佳实践,有什么方法可以解决上述问题。

  • 问题内容: 我已经在现有的mysql数据库中反映了表的负载。我想表示的是,任何表中具有特定名称的任何列默认为datetime.now()。但是,天真地遍历表和列,仅对那些我发现具有特定名称的表和列设置默认设置是行不通的。session.flush()我收到以下错误: 这似乎与对_set_parent的调用(以及在sqlalchemy.schema的第721行中的调用)有关。 有谁知道是否有办法做到