简体   繁体   English

扩展Swing应用程序中的系统外观

[英]Extend the system look and feel in a Swing application

I want to use the system look and feel for a Swing application. 我想为Swing应用程序使用系统外观。 So I used the getSystemLookAndFeelClassName() method which works pretty fine. 因此,我使用了getSystemLookAndFeelClassName()方法,该方法效果很好。

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

But, now I want to change all the JButtons of the app (in all my JFrames , JDialogs , JOptionPanes , JFileChoosers , etc.) and only the JButtons . 但是,现在我想改变这一切的JButtons应用程序的(在我所有的JFramesJDialogsJOptionPanesJFileChoosers等),只有JButtons So I would like to know how can I extend the system look and feel to keep it for all components except the JButtons (and the JPanels , which I want with a gray background). 因此,我想知道如何扩展系统的外观,以保持除JButtons (以及我想要的灰色背景的JPanels之外的所有组件的外观。

Thank you. 谢谢。

Finally I extended the system look and feel like this: 最后,我扩展了系统外观,如下所示:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
UIManager.put("ButtonUI", "com.my.package.MyButtonUI");
UIManager.put("Panel.background", Color.green);

With MyButtonUI : 使用MyButtonUI

public class MyButtonUI extends BasicButtonUI {

    public static final int BUTTON_HEIGHT = 24;

    private static final MyButtonUI INSTANCE = new MyButtonUI ();

    public static ComponentUI createUI(JComponent b) {
        return INSTANCE;
    }

    @Override
    public void paint(Graphics g, JComponent c) {
        AbstractButton button = (AbstractButton) c;
        Graphics2D g2d = (Graphics2D) g;
        final int buttonWidth = button.getWidth();
        if (button.getModel().isRollover()) {
            // Rollover
            GradientPaint gp = new GradientPaint(0, 0, Color.green, 0, BUTTON_HEIGHT * 0.6f, Color.red, true);
            g2d.setPaint(gp);
        } else if (button.isEnabled()) {
            // Enabled
            GradientPaint gp = new GradientPaint(0, 0, Color.red, 0, BUTTON_HEIGHT * 0.6f, Color.gray, true);
            g2d.setPaint(gp);
        } else {
            // Disabled
            GradientPaint gp = new GradientPaint(0, 0, Color.black, 0, BUTTON_HEIGHT * 0.6f, Color.blue, true);
            g2d.setPaint(gp);
        }
        g2d.fillRect(0, 0, buttonWidth, BUTTON_HEIGHT);
        super.paint(g, button);
    }

    @Override
    public void update(Graphics g, JComponent c) {
        AbstractButton button = (AbstractButton) c;
        if (isInToolBar(button)) {
            // Toolbar button
            button.setOpaque(false);
            super.paint(g, button);
        } else if (button.isOpaque()) {
            // Other opaque button
            button.setRolloverEnabled(true);
            button.setForeground(Color.white);
            paint(g, button);
        } else {
            // Other non-opaque button
            super.paint(g, button);
        }
    }

    private boolean isInToolBar(AbstractButton button) {
        return SwingUtilities.getAncestorOfClass(JToolBar.class, button) != null;
    }
}

Note: I higly recommend this link: http://tips4java.wordpress.com/2008/10/09/uimanager-defaults/ (from Robin) 注意:我强烈推荐此链接: http : //tips4java.wordpress.com/2008/10/09/uimanager-defaults/ (来自Robin)

Thank you. 谢谢。

You could also make your own button class, eg CustomButton, which extends JButton, make all your changes and use this instead of the standard JButton. 您还可以创建自己的按钮类,例如CustomButton,它扩展了JButton,进行所有更改并使用它代替标准JButton。 This way you can use both custom and standard buttons - if you change your mind and want to add a standard JButton somewhere ;) 这样,您可以同时使用自定义按钮和标准按钮-如果您改变主意并想在某个地方添加标准JButton;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM