16

让 JButton 只显示背景颜色的最简单方法是什么?我不需要任何其他效果,如边框、3D 外观或悬停突出显示。

提前致谢。

4

6 回答 6

31

我不知道我是否错过了一点......但我通常会这样做:

button.setBorderPainted(false);
button.setFocusPainted(false);
button.setContentAreaFilled(false);
于 2013-01-21T19:00:39.340 回答
13

只需设置颜色和边框:

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);使背景透明。

于 2009-12-03T10:51:34.837 回答
2

怎么样

yourButton.setBorder(null);

?

于 2009-12-03T10:49:10.300 回答
1

您可能希望将 JLabel 与 MouseListener 一起使用......除非您以某种方式使用 JButton 或 ActionListener。

于 2009-12-03T13:20:43.990 回答
1

首先,将您的外观和感觉设置为很酷的东西,例如“金属”。

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 的按钮样式应用于按钮,否则,按钮将采用简单的平面样式。

于 2013-06-05T13:34:08.733 回答
0

我想你可以这样做

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 代码(只需搜索颜色选择器)

于 2021-03-16T16:35:17.203 回答