【发布时间】:2017-04-09 19:53:42
【问题描述】:
在查看了许多以前的 StackOverflow 帖子后,我仍然无法将我的 JButton 设置为黑色而不是默认颜色。这是我的按钮的样子:
这是我的代码:
public void setStartButton() {
JPanel panel = this.jPanel1;
panel.setLayout(null);
JButton button = new JButton("START");
// size and location of start button
int res = java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
int length = (int) Math.floor(10.0*res/25.4); // side lengths of square button
int offset = length/2; // because button should be centered...but its x and y location are given by its upper left hand corner
button.setBounds(centerX-offset, centerY-offset, length, length);
// color of start button
button.setBackground(BLACK);
button.setOpaque(true);
button.setContentAreaFilled(false);
// font
button.setFont(new Font("Arial", Font.PLAIN, 8));
button.setVisible(true);
panel.add(button);
}
顺便说一句,当我将setContentAreaFilled 更改为true 时,没有任何区别。
我知道该函数确实被调用了,因为我的按钮的位置和字体信息工作正常。
任何帮助将不胜感激!谢谢!
【问题讨论】:
-
将黑色更改为 Color.BLACK。还要确保你
import java.awt.Color -
嗯,我已经导入了
import static java.awt.Color.BLACK;,所以应该可以了,对吧? -
并认真考虑使用合适的布局管理器
-
我不相信我可以在这里做到这一点,因为我必须在画布上的不同坐标处绘制按钮。