Java:JComboBox -> ScrollBar - 更改颜色



我正在做一个小应用程序。我做了一个JComboBox,我想改变里面的颜色。我已经改变了背景,前景,箭头按钮的颜色,…但我不知道如何改变scrollBar的颜色。

comoBox = new JComboBox();    
comoBox.setName("currencyCombo");
DefaultComboBoxModel comboModel = new DefaultComboBoxModel();
comboModel.addElement("gagD");
comboModel.addElement("agg");
comboModel.addElement("EhgsY");
comboModel.addElement("CgafgY");
comboModel.addElement("CfadgY");
comboModel.addElement("Ggafg");
comboModel.addElement("Sgfsdg");
comboModel.addElement("Ugfasdg");
comboModel.addElement("fasfasf");
comoBox.setModel(comboModel);
comoBox.setOpaque(true);
UIManager.put( "ComboBox.disabledBackground", new java.awt.Color(32, 34, 41));
UIManager.put( "ComboBox.disabledForeground", new java.awt.Color(181, 181, 181));
UIManager.put( "ComboBox.background", new java.awt.Color(32, 34, 41));
UIManager.put( "ComboBox.foreground", new java.awt.Color(181, 181, 181));
UIManager.put( "ComboBox.selectionBackground", new java.awt.Color(181, 181, 181));
UIManager.put( "ComboBox.selectionForeground", new java.awt.Color(32, 34, 41));
   comoBox.setUI(new BasicComboBoxUI(){
   @Override
   protected JButton createArrowButton() {
       JButton arrowButton = new BasicArrowButton( 
        BasicArrowButton.SOUTH,
        new java.awt.Color(32, 34, 41), 
        new java.awt.Color(181, 181, 181),
        new java.awt.Color(181, 181, 181),
        new java.awt.Color(32, 34, 41));
       arrowButton.setBorder(BorderFactory.createLineBorder(new java.awt.Color(32, 34, 41)));
       return arrowButton;
    }             
});

我试过了:

Object obejct = currencyCombo.getUI().getAccessibleChild(currencyCombo, 0);
              Component co = ((Container) obejct).getComponent(0) ;
                      if( co instanceof JScrollPane){
                            JScrollPane pane = (JScrollPane) co;        
                            Component[] components = pane.getComponents();
                            for (Component component : components) {
                                if(component instanceof JScrollBar)
                                {
                                    JScrollBar jsb = (JScrollBar) component;
                                    jsb.setBackground(Color.red);
                                    jsb.setForeground(Color.blue);
                                    jsb.setUI(new BasicScrollBarUI());
                                    jsb.updateUI();
                                }
                            }
                      }

您好,下面的代码将改变滚动条在组合框内的背景:

EDIT:您可以通过以下方式访问滚动条的颜色:

UIManager.put("ScrollBar.thumbHighlight", Color...;         
UIManager.put("ScrollBar.thumbDarkShadow", Color...; 
UIManager.put("ScrollBar.highlight", Color...; 
UIManager.put("ScrollBar.trackHighlight", Color...;

相关内容

  • 没有找到相关文章