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

How do I change the color of a button, not just the border around it?

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

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

你可以这样做:this.closeButton.setTextColor(Color.PINK); 取消注释行:this.closeButton.setForeground(Color.PINK) ;

"border" 由外观委托提供。您可以 "disable" 通过调用 button.setBorderPainted

这可能会或可能不会满足您的期望

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