如何更改 JColorChooser 的色板组件的颜色?
How Do You Change The Color on The Swatches Component of JColorChooser?
我正在尝试制作一个涉及 JColorChooser Swatches 组件的绘图应用程序,并且我正在尝试使我的 UI 成为某种颜色。除了 "Recent" 框周围的一小块区域外,我几乎可以通过在几乎所有地方设置背景来更改颜色。
Screenshot
如有任何帮助,我将不胜感激,我将在下面粘贴部分代码以供参考:
//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);
我认为如果不通过 JComponent 树进行一些复杂的搜索,就无法做到这一点。但是即使我无法更改背景颜色。
编辑:
我终于设法用下面的代码改变了最近面板的背景颜色:
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));
另一种方法可行,但它会更改整个应用程序中所有面板的背景。使用 UIManager 更改背景:
UIManager.put("Panel.background", new ColorUIResource(0, 155, 228));
我正在尝试制作一个涉及 JColorChooser Swatches 组件的绘图应用程序,并且我正在尝试使我的 UI 成为某种颜色。除了 "Recent" 框周围的一小块区域外,我几乎可以通过在几乎所有地方设置背景来更改颜色。 Screenshot
如有任何帮助,我将不胜感激,我将在下面粘贴部分代码以供参考:
//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);
我认为如果不通过 JComponent 树进行一些复杂的搜索,就无法做到这一点。但是即使我无法更改背景颜色。
编辑: 我终于设法用下面的代码改变了最近面板的背景颜色:
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));
另一种方法可行,但它会更改整个应用程序中所有面板的背景。使用 UIManager 更改背景:
UIManager.put("Panel.background", new ColorUIResource(0, 155, 228));