【问题标题】:How do I change the color of a button, not just the border around it?如何更改按钮的颜色,而不仅仅是它周围的边框?
【发布时间】:2020-03-30 20:56:56
【问题描述】:

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

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

【问题讨论】:

标签: java swing jbutton


【解决方案1】:

“边框”由外观委托提供。你可以通过调用button.setBorderPainted“禁用”它

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • java.swing.JButton 没有setTextColor 方法,你可能会想到JavaFX
    猜你喜欢
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    相关资源
    最近更新 更多