如何更改按钮的颜色,而不仅仅是边框的颜色?

时间:2020-03-30 20:56:56

标签: java swing jbutton

我想同时更改按钮的背景色和前景色。我使用了setBackground和setForeground和setOpaque(true),它适用于前景,但不适用于按钮的背景。按钮周围有点像黑色边框,但是我希望按钮本身是黑色的。我该如何解决?

this.closeButton = new JButton ("Close");
    this.closeButton.setBackground(Color.BLACK);
    this.closeButton.setForeground(Color.PINK);
    this.closeButton.setOpaque(true);

This is what I have

2 个答案:

答案 0 :(得分:1)

“边界”由外观委托提供。您可以通过调用button.setBorderPainted

来“禁用”它

这可能符合您的期望

Close button

JButton button = new JButton("Close");
button.setBackground(Color.BLACK);
button.setForeground(Color.PINK);
button.setBorderPainted(false);
button.setOpaque(true);

答案 1 :(得分:0)

您可以执行以下操作:this.closeButton.setTextColor(Color.PINK); 取消注释行:this.closeButton.setForeground(Color.PINK);