Java 如何用简单的扁平风格制作JButton?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1839074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Howto make JButton with simple flat style?
提问by c0d3x
whats the most simple way to make a JButton only show the background color? I don't need any other effects like borders, 3D-look or hover-highlighting.
使 JButton 仅显示背景颜色的最简单方法是什么?我不需要任何其他效果,如边框、3D 外观或悬停突出显示。
Thanks in advance.
提前致谢。
回答by Joonas Pulakka
How about
怎么样
yourButton.setBorder(null);
?
?
回答by McDowell
Just set the colours and the border:
只需设置颜色和边框:
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;
}
Use setOpaque(false);
to make the background transparent.
使用setOpaque(false);
使背景透明。
回答by Nate
You may want to use a JLabel with a MouseListener instead... unless you're tied to using a JButton or ActionListener in some way.
您可能希望将 JLabel 与 MouseListener 一起使用……除非您以某种方式与使用 JButton 或 ActionListener 相关联。
回答by André Luiz Clinio
I don't know if I have missed a point... But I usually do somtehing like this:
我不知道我是否错过了一点......但我通常这样做:
button.setBorderPainted(false);
button.setFocusPainted(false);
button.setContentAreaFilled(false);
回答by Felype
First, set your look and feel to something cool, like 'metal'.
首先,将您的外观和感觉设置为很酷的东西,例如“金属”。
try
{
for (UIManager.LookAndFeelInfo lnf :
UIManager.getInstalledLookAndFeels()) {
if ("Metal".equals(lnf.getName())) {
UIManager.setLookAndFeel(lnf.getClassName());
break;
}
}
} catch (Exception e) { /* Lazy handling this >.> */ }
Then do something like this:
然后做这样的事情:
my_jbutton.setBackground(new Color(240,240,241);
If the color of your button is exactly equals to swing control color (240,240,240), windows will apply windows-like button style to the button, otherwise, the button assumes a simple flat style.
如果你的按钮的颜色正好等于摇摆控制颜色(240,240,240),windows会为按钮应用类似windows的按钮样式,否则按钮采用简单的平面样式。