将背景颜色设置为JButton
我有一个关于为JButton
设置背景颜色的问题。
似乎这种方法只改变了边框的颜色。 这是区别(左边是jButton
):
有没有办法使背景相同?
我在Windows 8上使用setLookAndFeel
。
这适用于Metal(默认)或Windows PLAF。
import java.awt.*; import javax.swing.*; class ColoredButton { public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { 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)。