将背景颜色设置为JButton

ro-*_*o-E 1 java swing colors jbutton

我有一个关于设置背景颜色的问题JButton.

看来这种方法只会改变边框的颜色.这是区别(左边jButton):

在此输入图像描述

有没有办法使背景相同?

setLookAndFeel在Windows 8上使用.

And*_*son 17

这适用于Metal(默认)或Windows PLAF.

import java.awt.Color;
import javax.swing.*;

class ColoredButton {

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                UIManager.setLookAndFeel(
                        UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                e.printStackTrace();
            }

            JButton b1 = new JButton("Button 1");
            b1.setBackground(Color.RED);
            // these next two lines do the magic..
            b1.setContentAreaFilled(false);
            b1.setOpaque(true);

            JOptionPane.showMessageDialog(null, b1);
        };
        SwingUtilities.invokeLater(r);
    }
}
Run Code Online (Sandbox Code Playgroud)