java - 为 JButton 设置背景颜色

标签 java swing colors jbutton

<分区>

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

似乎这个方法只是改变了边框的颜色。这是区别(左边是 jButton):

enter image description here

有没有办法让背景一样?

我在 Windows 8 上使用 setLookAndFeel

最佳答案

这将适用于 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);
    }
}

关于java - 为 JButton 设置背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18165160/

相关文章:

java - 如何获得weka簇质心的值(value)

java - 尝试在适当的时候禁用 JButtons

java - Swing 是否持续扫描键盘输入(java)?

Android 颜色未在模拟器或设备中正确反射(reflect)

php - 基本的 CSS 问题

java - 在 ACM 图形 API 中对颜色使用十六进制代码

Java 断路器在请求线程中运行

java - 如何将 GUNIT 与 MAVEN 一起使用?

c# - 为什么 C#/VS 没有像 Java/Eclipse 这样的自动构建功能?

java - 在 jar 中加载 jpeg 图像