JColorChooser: hide all default panels and show HSB panel only

烈酒焚心 提交于 2019-12-23 12:04:19

问题


How can I hide all default panels at JColorChooser except HSB?

And is it possible to show just HSB without JTabbedPane, just plain panel

Thank you!


回答1:


import javax.swing.*;
import javax.swing.colorchooser.*;

class ColorChooserTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JColorChooser cc = new JColorChooser();
                AbstractColorChooserPanel[] panels = cc.getChooserPanels();
                for (AbstractColorChooserPanel accp : panels) {
                    if (accp.getDisplayName().equals("HSB")) {
                        JOptionPane.showMessageDialog(null, accp);
                    }
                }
            }
        });
    }
}



回答2:


You can try: setChooserPanels method of JColorChooser to do this. More help here.




回答3:


It can be also done with the simple loop:

AbstractColorChooserPanel[] panels = jColorChooser1.getChooserPanels();
for (AbstractColorChooserPanel accp : panels) {
   if(!accp.getDisplayName().equals("HSB")) {
      jColorChooser1.removeChooserPanel(accp);
   } 
}



回答4:


If you want to delete panels you can follow this approach Here I'm removing all the other panels except Swatches and RGB,

AbstractColorChooserPanel[] panels=colorChooser.getChooserPanels();
        for(AbstractColorChooserPanel p:panels){
            String displayName=p.getDisplayName();
            switch (displayName) {
                case "HSV":
                    colorChooser.removeChooserPanel(p);
                    break;
                case "HSL":
                    colorChooser.removeChooserPanel(p);
                    break;
                case "CMYK":
                    colorChooser.removeChooserPanel(p);
                    break;
            }


来源:https://stackoverflow.com/questions/9079807/jcolorchooser-hide-all-default-panels-and-show-hsb-panel-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!