简体   繁体   English

在Windows上设置JButton背景颜色

[英]Set JButton background color on windows

I have a JButton which I want to set the background to a color. 我有一个JButton,我想将背景设置为一种颜色。

JButton button = new JButton();
button.setVisible(true);
button.setPreferredSize(new Dimension(student_scroll.getWidth(), 50));
button.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1));
button.setBackground(Color.BLACK);
button.setForeground(Color.WHITE);
button.setOpaque(true);

I used this for mac and it showed up as I wanted it to be. 我用这个用于mac,它出现了,因为我想要它。 However, upon trying it on windows, the foreground is white(as it should) but the background is empty. 但是,在Windows上尝试它时,前景是白色的(应该如此),但背景是空的。

Setting background color to JButton 将背景颜色设置为JButton

says to add button.setContentAreaFilled(false); 说添加button.setContentAreaFilled(false); which I did but had no effect. 我做了但没有效果。 Most others say to add button.setOpaque(true); 大多数人都说添加button.setOpaque(true); which I also did already. 我也已经做过了。

What else do I have to do so that it will show up with a black background? 还有什么我必须做的,它会出现黑色背景?

EDIT 编辑

As requested, the SSCCE: 根据要求,SSCCE:

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MainSwing extends JFrame {
    private static final long serialVersionUID = -8231889836024827530L;

    public static void main(String[] args) {
        try {
            System.setProperty("apple.laf.useScreenMenuBar", "true");
            System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Test");
            UIManager.put("ScrollBarUI", "main.CustomScrollBarUI");
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(ClassNotFoundException e) {
            System.out.println("ClassNotFoundException: " + e.getMessage());
    }
    catch(InstantiationException e) {
            System.out.println("InstantiationException: " + e.getMessage());
    }
    catch(IllegalAccessException e) {
            System.out.println("IllegalAccessException: " + e.getMessage());
    }
    catch(UnsupportedLookAndFeelException e) {
            System.out.println("UnsupportedLookAndFeelException: " + e.getMessage());
    }
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JFrame frame = new JFrame() {

                    Container c = getContentPane();
                    JButton button = new JButton("Hello");
                    {
                        button.setText("Hello");
                        button.setVisible(true);
                        button.setPreferredSize(new Dimension(100, 50));
                        button.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1));
                        button.setBackground(Color.BLACK);
                        button.setForeground(Color.WHITE);
                        button.setOpaque(true);
                        c.add(button);
                    }

                };
                frame.setSize(500, 500);
                frame.setBackground(Color.BLACK);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

It seems that the problem has something to do with the line: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 似乎该问题与该行有关: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); as when I remove it, the buttons are black. 当我删除它时,按钮是黑色的。

I'm guessing from those UIManager key/value pairs that the PLAF is the OS X based Aqua PLAF. 我猜测那些UIManager键/值对,PLAF是基于OS X的Aqua PLAF。 And that seems to be part of the problem here. 这似乎是问题的一部分。 Here it is without the content area filled on Windows. 这里没有在Windows上填充的内容区域。

在此输入图像描述

import java.awt.*;
import javax.swing.*;

public class MainSwing extends JFrame {

    public static void main(String[] args) {
        try {
            System.setProperty("apple.laf.useScreenMenuBar", "true");
            System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Test");
            UIManager.put("ScrollBarUI", "main.CustomScrollBarUI");
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace(); // more info for less LOC!
        }
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame() {

                    Container c = getContentPane();
                    JButton button = new JButton("Hello");

                    {
                        c.setLayout(new GridLayout(0,1));
                        c.add(new JButton("Hi"));
                        button.setText(UIManager.getSystemLookAndFeelClassName());
                        button.setVisible(true);
                        button.setPreferredSize(new Dimension(400, 100));
                        button.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1));
                        button.setContentAreaFilled(false);
                        button.setBackground(Color.BLACK);
                        button.setForeground(Color.WHITE);
                        button.setOpaque(true);
                        c.add(button);
                    }
                };
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

It seems that the problem has something to do with the line: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 似乎该问题与该行有关:UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); as when I remove it, the buttons are black. 当我删除它时,按钮是黑色的。

So that is extra information we didn't have before you created the SSCCE (which is why you should always post a SSCCE with your question). 这是我们在您创建SSCCE之前没有的额外信息(这就是为什么您应该总是发布带有您问题的SSCCE)。

It also tells you the problem is not with your code but with the LAF. 它还告诉您问题不在于您的代码,而在于LAF。 The Windows LAF ignores the setBackground(...) method and paints its own background. Windows LAF忽略setBackground(...)方法并绘制自己的背景。

One option would be to add an Icon to the button of your desired color. 一种选择是在所需颜色的按钮上添加一个图标。 Then you can configure the text to be painted in the center of the button: 然后,您可以配置要在按钮中心绘制的文本:

import java.awt.*;
import javax.swing.*;

public class ColorIcon implements Icon
{
    private Color color;
    private int width;
    private int height;

    public ColorIcon(Color color, int width, int height)
    {
        this.color = color;
        this.width = width;
        this.height = height;
    }

    public int getIconWidth()
    {
        return width;
    }

    public int getIconHeight()
    {
        return height;
    }

    public void paintIcon(Component c, Graphics g, int x, int y)
    {
        g.setColor(color);
        g.fillRect(x, y, width, height);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    public static void createAndShowGUI()
    {
        JPanel panel = new JPanel( new GridLayout(2, 2) );

        for (int i = 0; i < 4; i++)
        {
            Icon icon = new ColorIcon(Color.RED, 50, 50);
            JButton label = new JButton( icon );
            label.setText("" + i);
            label.setHorizontalTextPosition(JButton.CENTER);
            label.setVerticalTextPosition(JButton.CENTER);
            panel.add(label);
        }

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(panel);
        f.setSize(200, 200);
        f.setLocationRelativeTo( null );
        f.setVisible(true);
    }
}

Then is should work on all LAF's. 那应该是所有LAF的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM