java - 在 Swing 应用程序中扩展系统外观

标签 java swing jpanel jbutton look-and-feel

我想使用 Swing 应用程序的系统外观。所以我使用了 getSystemLookAndFeelClassName() 方法,效果非常好。

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

但是,现在我想更改应用程序的所有 JButtons(在我的所有 JFramesJDialogsJOptionPanesJFileChoosers 等中),并且仅更改 JButtons。因此,我想知道如何扩展系统的外观和感觉,以保留除 JButtons(以及 JPanels,我希望其具有灰色背景)之外的所有组件。

谢谢。

最佳答案

最后我扩展了系统的外观,如下所示:

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

使用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;
    }
}

注意:我强烈推荐此链接:http://tips4java.wordpress.com/2008/10/09/uimanager-defaults/ (来自罗宾)

谢谢。

关于java - 在 Swing 应用程序中扩展系统外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12624586/

相关文章:

java - Swing - 使用 TextComponents 的 GUI 中的关键问题(箭头键、Tab 等)

java - FindBugs - 与 null 的冗余比较

java - 我可以创建没有 svg 文件的 JSVGCanvas 吗?

java - 循环图像,java gui?

java - JLabel - 将较长的文本显示为多行?

Java 应用程序 - 添加、删除、重新排序 JButton 的元素

java - 在 JPA 实体的基类中定义序列生成器?

java - 使用 JFrame 的 JAVA 贷款计算器出现异常?

java - 如何在父组件设置为 `setVisible(false)` 后清除 JTextField 文本?

java - 用java编写C#委托(delegate)