我想同时更改按钮的背景色和前景色。我使用了setBackground和setForeground和setOpaque(true),它适用于前景,但不适用于按钮的背景。按钮周围有点像黑色边框,但是我希望按钮本身是黑色的。我该如何解决?
this.closeButton = new JButton ("Close");
this.closeButton.setBackground(Color.BLACK);
this.closeButton.setForeground(Color.PINK);
this.closeButton.setOpaque(true);
答案 0 :(得分:1)
“边界”由外观委托提供。您可以通过调用button.setBorderPainted
这可能符合您的期望
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);