让 JButton 只显示背景颜色的最简单方法是什么?我不需要任何其他效果,如边框、3D 外观或悬停突出显示。
提前致谢。
我不知道我是否错过了一点......但我通常会这样做:
button.setBorderPainted(false);
button.setFocusPainted(false);
button.setContentAreaFilled(false);
只需设置颜色和边框:
private static JButton createSimpleButton(String text) {
JButton button = new JButton(text);
button.setForeground(Color.BLACK);
button.setBackground(Color.WHITE);
Border line = new LineBorder(Color.BLACK);
Border margin = new EmptyBorder(5, 15, 5, 15);
Border compound = new CompoundBorder(line, margin);
button.setBorder(compound);
return button;
}
用于setOpaque(false);
使背景透明。
怎么样
yourButton.setBorder(null);
?
您可能希望将 JLabel 与 MouseListener 一起使用......除非您以某种方式使用 JButton 或 ActionListener。
首先,将您的外观和感觉设置为很酷的东西,例如“金属”。
try
{
for (UIManager.LookAndFeelInfo lnf :
UIManager.getInstalledLookAndFeels()) {
if ("Metal".equals(lnf.getName())) {
UIManager.setLookAndFeel(lnf.getClassName());
break;
}
}
} catch (Exception e) { /* Lazy handling this >.> */ }
然后做这样的事情:
my_jbutton.setBackground(new Color(240,240,241);
如果您的按钮颜色正好等于摇摆控件颜色 (240,240,240),windows 会将类似 windows 的按钮样式应用于按钮,否则,按钮将采用简单的平面样式。
我想你可以这样做
JButton button = new JButton("Click Me!!");
button.setBorderPainted(false);
button.setBackground(new Color());// inside the brackets your rgb color value like 255,255,255
button.setFocusPainted(false);
您可以使用颜色选择器获取 rgb 代码(只需搜索颜色选择器)