【发布时间】:2013-08-12 11:25:24
【问题描述】:
我有一个关于将背景颜色设置为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);
}
}
【讨论】:
在按钮上使用 .setOpaque(true)。
【讨论】: