<分区>
我有一个关于将背景颜色设置为 JButton
的问题。
似乎这个方法只是改变了边框的颜色。这是区别(左边是 jButton
):
有没有办法让背景一样?
我在 Windows 8 上使用 setLookAndFeel
。
<分区>
我有一个关于将背景颜色设置为 JButton
的问题。
似乎这个方法只是改变了边框的颜色。这是区别(左边是 jButton
):
有没有办法让背景一样?
我在 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/