JavaFX自定义ComboBox箭头无法显示红色背景问题求助
解决方案
问题核心在于你未给ComboBox的主控件容器设置红色背景——当前list-cell的样式仅作用于下拉列表的选项,箭头按钮所在的区域属于ComboBox的主控件部分,不受该样式影响。
调整CSS代码如下:
.custom-combobox .arrow-button { -fx-background-color: transparent; -fx-background-size: contain; -fx-background-repeat: no-repeat; -fx-background-position: center; -fx-background-image: url('../img/downArrow.png'); } .custom-combobox .list-cell { -fx-pref-height: 70px; -fx-pref-width: 150px; -fx-background-color: red; -fx-background-size: 100% 100%; -fx-background-position: center; -fx-alignment: center; } .custom-combobox .combo-box { -fx-max-height: 70px; -fx-max-width: 150px; -fx-background-color: red; /* 新增:设置ComboBox主背景为红色 */ -fx-background-size: 100% 100%; -fx-background-repeat: no-repeat; -fx-background-position: center; -fx-padding: 0; } .custom-combobox .arrow { -fx-background-color: transparent; -fx-padding: 0; -fx-shape: null; /* 移除重复的背景图片设置,箭头已通过arrow-button显示 */ }
原理:ComboBox主容器设置红色背景后,箭头按钮因背景透明,会直接透出下方的红色,实现箭头后方显示红色的效果。
内容的提问来源于stack exchange,提问作者Simone Comignani
相关产品推荐
相关产品推荐

