【问题标题】:JButton background color not changing with getSystemLookANdFeel in windowsJButton 背景颜色不随 Windows 中的 getSystemLookANDFeel 改变
【发布时间】:2017-10-19 05:56:50
【问题描述】:

我正在尝试使用 UIManager 在 Windows 上更改具有系统外观的 JButton 的背景颜色。 代码sn-p

UIManager.put("Button.contentAreaFilled",false);
UIManager.put("Button.background",Color.white);
UIManager.put("Button.foreground",Color.black);
UIManager.put("Button.opaque",true);

但它显示了底层操作系统颜色。有人可以提出解决方法吗?

【问题讨论】:

  • 当你想改变颜色时。点击之后还是之前?。
  • 在加载框架时单击自身之前
  • 你最好使用JavaFx
  • @RishabhKhandelwal 请展示您如何设置外观和感觉。最好提供一个 SSCCE 来证明您的问题。

标签: java swing user-interface


【解决方案1】:

您必须使用自己的按钮来实现自己的绘图逻辑。 这是一个示例实现:

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

public class ColorButton extends JButton {
    JLabel label = new JLabel();
    private Color bg;

    public ColorButton(String text) {
        super(text);

        label.setHorizontalTextPosition(JLabel.CENTER);
        label.setText(text);
        label.setOpaque(true);

        setLayout(new BorderLayout());
        add(label, BorderLayout.CENTER);
    }

    @Override
    public void setBackground(Color bg) {
        if(!UIManager.getSystemLookAndFeelClassName().endsWith("WindowsLookAndFeel")) {
            super.setBackground(bg);
        }

        this.bg = bg;
        if(label != null) {
            label.setBackground(bg);
        }
    }

    @Override
    public void setForeground(Color fg) {
        super.setForeground(fg);
        if(label != null){
            label.setForeground(fg);
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Rectangle rectangle = getBounds();
        rectangle.x = 3;
        rectangle.y = 3;
        rectangle.width -= rectangle.x * 2;
        rectangle.height -= rectangle.y * 2;

        Graphics2D g2d = (Graphics2D)g;
        g2d.setColor(bg);
        g2d.fill(rectangle);
    }
}

这是一个测试:

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

public class ColorButtonFrame {
    public static void main(String[] args) throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        ColorButton button = new ColorButton("Click Me...");
        button.setOpaque(true);
        button.setBackground(Color.GREEN);

        JButton button1 = new JButton("Click Me...");
        button1.setOpaque(true);
        button1.setBackground(Color.GREEN);

        JPanel panel = new JPanel();
        panel.add(button);
        panel.add(Box.createHorizontalStrut(10));
        panel.add(button1);

        frame.add(panel);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

【讨论】:

  • 为什么我不能简单地将属性设置为 UIManager.put("Button.background",Color.white);
  • 好吧,在 WindowsLookAndFeel 中没有任何作用。但即使它有效,你也会改变所有按钮的颜色,而且几乎不是这样。
  • 但是当我将它与 JTextArea 或滚动窗格一起使用时,一些更改工作正常
  • 我不知道为什么它不适用于按钮。我用 windows 7 和 java 8 测试过。
  • @RishabhKhandelwal:按钮的 UI 委托决定它是否不透明;您已更改或替换它。
猜你喜欢
  • 2011-03-12
  • 2014-05-11
  • 1970-01-01
  • 2011-01-28
  • 2017-03-25
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
相关资源
最近更新 更多