【问题标题】:Setting background color to JButton [duplicate]将背景颜色设置为 JButton [重复]
【发布时间】:2013-08-12 11:25:24
【问题描述】:

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

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

有没有办法让背景一样?

我在 Windows 8 上使用 setLookAndFeel

【问题讨论】:

标签: java swing colors jbutton


【解决方案1】:

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

【讨论】:

  • 谢谢,我需要添加 setContentAreaFilled(false) 现在效果很好
  • setContentAreaFilled 为我解决了问题。 +1
【解决方案2】:

在按钮上使用 .setOpaque(true)。

【讨论】:

  • 好像不行,结果还是只有边框变红了
猜你喜欢
  • 1970-01-01
  • 2022-01-22
  • 2017-04-05
  • 2011-08-31
  • 1970-01-01
  • 1970-01-01
  • 2019-07-07
  • 2011-01-06
  • 1970-01-01
相关资源
最近更新 更多