JButton 的颜色
color of JButton
在查看了许多以前的 Whosebug 帖子后,我仍然无法将我的 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
时,没有任何区别。
我知道该函数确实被调用了,因为我的按钮的位置和字体信息工作得很好。
如有任何帮助,我们将不胜感激!谢谢!
一个JButton
由一系列层组成,包括content
、border
和focus
层。根据您要执行的操作,您可能需要删除所有这些,例如...
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
setStartButton();
}
public void setStartButton() {
JButton button = new JButton("START");
button.setMargin(new Insets(20, 20, 20, 20));
// color of start button
button.setOpaque(true);
button.setContentAreaFilled(true);
button.setBorderPainted(false);
button.setFocusPainted(false);
button.setBackground(BLACK);
button.setForeground(WHITE);
// font
button.setFont(new Font("Arial", Font.PLAIN, 8));
add(button);
}
}
我也强烈建议您考虑使用适当的布局管理器并使用它的属性和 JButton
来生成您需要的所需填充,这些将与字体规格一起使用,这往往在系统之间有所不同,以便为按钮生成适当的大小
在查看了许多以前的 Whosebug 帖子后,我仍然无法将我的 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
时,没有任何区别。
我知道该函数确实被调用了,因为我的按钮的位置和字体信息工作得很好。
如有任何帮助,我们将不胜感激!谢谢!
一个JButton
由一系列层组成,包括content
、border
和focus
层。根据您要执行的操作,您可能需要删除所有这些,例如...
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
setStartButton();
}
public void setStartButton() {
JButton button = new JButton("START");
button.setMargin(new Insets(20, 20, 20, 20));
// color of start button
button.setOpaque(true);
button.setContentAreaFilled(true);
button.setBorderPainted(false);
button.setFocusPainted(false);
button.setBackground(BLACK);
button.setForeground(WHITE);
// font
button.setFont(new Font("Arial", Font.PLAIN, 8));
add(button);
}
}
我也强烈建议您考虑使用适当的布局管理器并使用它的属性和 JButton
来生成您需要的所需填充,这些将与字体规格一起使用,这往往在系统之间有所不同,以便为按钮生成适当的大小