简体   繁体   English

如何更改JColorChooser的色板组件上的颜色?

[英]How Do You Change The Color on The Swatches Component of JColorChooser?

I'm trying to make a drawing application involving JColorChooser Swatches Component, and I'm trying to make my UI a certain color. 我正在尝试制作一个涉及JColorChooser Swatches Component的绘图应用程序,并且正在尝试将UI设置为某种颜色。 I was able to change the color through setting the background pretty much everywhere except for one small area around the "Recent" box. 我可以通过在几乎所有地方设置背景来更改颜色,除了“最近”框周围的一个小区域。 Screenshot 屏幕截图

Any help would be appreciated, I'll paste part of my code below for context: 任何帮助将不胜感激,我将在下面粘贴部分代码作为上下文:

 //Sets up color chooser
  chooser = new JColorChooser(Color.BLACK);
  AbstractColorChooserPanel[] panels = chooser.getChooserPanels();
  for (int i = 0; i < panels.length; i++) {
     if (!panels[i].getDisplayName().equalsIgnoreCase("Swatches"))
         chooser.removeChooserPanel(panels[i]);
     else {
        panels[i].setBackground(new Color(0, 155, 228));
     }
  }
  chooser.setPreviewPanel(new JPanel());
  //Sets up size slider
  sizeSlide = new JSlider(1, 45);

  //Adds Color/Size to one control panel, adds new panel to bottom of 
  //main
  optionP = new JPanel();
  optionP.setBackground(new Color(0, 155, 228));
  optionP.setLayout(new BorderLayout());
  optionP.add(sizeSlide, BorderLayout.EAST);
  optionP.add(chooser, BorderLayout.WEST);
  this.add(optionP, BorderLayout.SOUTH);    

I don't think this can be done without some sophisticated search through the JComponent tree. 我认为,如果不通过JComponent树进行一些复杂的搜索,就无法做到这一点。 But even than I was not able to change the background color. 但是甚至比我还不能更改背景色。

EDIT: I finally managed to change the background color for the recent panel with below code: 编辑:我终于设法用以下代码更改了最近面板的背景颜色:

JColorChooser chooser = new JColorChooser(Color.BLACK);
AbstractColorChooserPanel[] panels = chooser.getChooserPanels();
for (int i = 0; i < panels.length; i++) {
    if (!panels[i].getDisplayName().equalsIgnoreCase("Swatches")) {
        chooser.removeChooserPanel(panels[i]);
    } else {
        panels[i].setBackground(new Color(0, 155, 228));
        // placing code to change recent panel background color here
        // will not work for some odd reason
        // JComponent component = (JComponent) panels[i].getComponent(0);
        // component.setBackground(new Color(0, 155, 228));
    }
}
AbstractColorChooserPanel panel = chooser.getChooserPanels()[0];
JComponent component = (JComponent) panel.getComponent(0);
component.setBackground(new Color(0, 155, 228));

Another approach worked but it will change the background of all your Panels across the application. 另一种方法可行,但是它将更改整个应用程序中所有面板的背景。 Use the UIManager to change the Background: 使用UIManager更改背景:

UIManager.put("Panel.background", new ColorUIResource(0, 155, 228));

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

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