首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JButton颜色

JButton颜色
EN

Stack Overflow用户
提问于 2017-04-09 19:53:42
回答 1查看 64关注 0票数 0

在回顾了许多以前的StackOverflow帖子之后,我仍然无法让JButton变成黑色,而不是默认的颜色。下面是我的按钮的样子:

这是我的密码:

代码语言:javascript
复制
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时,没有什么不同。

我知道这个函数确实被调用了,因为我的按钮的位置和字体信息运行得很好。

任何帮助都将不胜感激!谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-09 22:11:38

JButton由一系列层组成,包括contentborderfocus层。根据你想要做的事情,你可能需要把它们全部删除,例如.

代码语言:javascript
复制
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生成所需的填充,它们将与不同系统之间的字体度量一起工作,以便为按钮生成合适的大小。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43311214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档