gpt4 book ai didi

java - 更改 JComboBox 箭头的背景颜色

转载 作者:行者123 更新时间:2023-12-04 06:38:25 25 4
gpt4 key购买 nike

我找到了以下 solution for changing a JComboBox arrow's color :

For JComboBox and Metal L&F
-- iterate recursively over the components of the JComboBox and grab a reference
to the button of class javax.swing.plaf.metal.MetalComboBoxButton
-- get the icon by getComboIcon()
-- create a BufferedImage (type ARGB) the size of the icon
-- paintIcon the icon to the Graphics context of the BufferedImage
-- iterate over the pixels of the BufferedImage and change any non-zero pixels
(by getRGB) to the color you want (by setRGB).
-- construct a new ImageIcon from the image
-- set the new icon to the button by setComboIcon

您究竟如何“将图标绘制到 BufferedImage 的图形上下文中”?

最佳答案

像这样:

    int componentCount = comboBox.getComponentCount();
for (int i = 0; i < componentCount; i++) {
Component component = comboBox.getComponent(i);
if (component instanceof MetalComboBoxButton) {
MetalComboBoxButton metalComboBoxButton =
(MetalComboBoxButton) component;
Icon comboIcon = metalComboBoxButton.getComboIcon();
BufferedImage bufferedImage =
new BufferedImage(
comboIcon.getIconWidth(),
comboIcon.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
comboIcon.paintIcon(
metalComboBoxButton, bufferedImage.getGraphics(), 0, 0);
}
}

关于java - 更改 JComboBox 箭头的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4586567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com