WindowsLookAndFeel关于按钮着色的意外行为

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WindowsLookAndFeel关于按钮着色的意外行为相关的知识,希望对你有一定的参考价值。

使用选择的WindowsLookAndFeel

UIManager.getSystemLookAndFeelClassName()

因为我在Windows上工作会导致意外行为。按钮的背景颜色发生变化。有时他们不在我目前的项目中,但我无法发现差异。

行为错误的按钮:

https://i.stack.imgur.com/d5OUk.png

默认LookAndFeel的按钮:

https://i.stack.imgur.com/AMoHv.png

我将给出一个产生意外行为的工作示例:

import javax.swing.UIManager;

public class Context {
    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        new VocableEditor();
    }
}

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class VocableEditor extends JFrame implements WindowListener, ActionListener {
    private static final long serialVersionUID = -6209345602005762300L;
    private Button b_save, b_discard;
    private GridBagLayout layout;

    public VocableEditor() {

        setTitle("vocableEditorTitle");
        setSize(600, 400);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(this);

        layout = new GridBagLayout();
        setLayout(layout);
        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = 1;
        c.gridheight = 1;

        b_discard = new Button("discardAndLeave");
        b_discard.addActionListener(this);
        c.gridwidth = 1;
        c.gridx = 2;
        add(b_discard, c);
        b_save = new Button("saveAndExit");
        b_save.addActionListener(this);
        c.gridx = 3;
        add(b_save, c);

        setVisible(true);
    }

    public void end(boolean save) {
        System.out.println(save);
        dispose();
    }

    @Override
    public void windowOpened(WindowEvent e) {
    }

    @Override
    public void windowClosing(WindowEvent e) {
    }

    @Override
    public void windowClosed(WindowEvent e) {
    }

    @Override
    public void windowIconified(WindowEvent e) {
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    }

    @Override
    public void windowActivated(WindowEvent e) {
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b_save) {
            end(true);
        } else if (e.getSource() == b_discard) {
            end(false);
        }
    }
}

import java.awt.Color;

import javax.swing.JButton;

public class Button extends JButton {
    private static final Color BACKGROUND_COLOR = new Color(60, 90, 180);

    public Button(String name) {
        super(name);
        setBackground(BACKGROUND_COLOR);
        setForeground(Color.WHITE);
    }
}
答案

经过数小时的研究,我找到了解决方案。甲骨文表示,即使我无法理解为什么LAF必须是这样不透明的,所以显示出所显示的行为。解决方案是将以下行添加到按钮的构造函数中:

setContentAreaFilled(false);
setOpaque(true);

第一行禁用绘制背景,第二行重新启用它但是错过了LAF会改变外观的部分。

以上是关于WindowsLookAndFeel关于按钮着色的意外行为的主要内容,如果未能解决你的问题,请参考以下文章

为啥 Windows LookAndFeel 使字体太小?

为 MFC 静态标签的整个背景着色

如何使用最新的 AppCompat 为按钮着色

使用 AppCompat 为 Android 中的按钮着色

iOS 7 中忽略的条形按钮项目着色

着色 iPhone 导航栏时颜色和按钮会改变行为