更改 JFileChooser 的颜色

发布于 2024-08-07 06:40:40 字数 818 浏览 7 评论 0原文

我试图按照这篇文章中的一些想法更改 JFileChooser 的颜色:

更改 JFileChooser 中 WindowsPlacesBar 的颜色

但是我无法这样做,我找不到缺少哪些代码。到目前为止我只得到这个:

    UIManager.put("Panel.background", Color.BLACK);
    UIManager.put("Button.background", Color.BLACK);
    UIManager.put("ComboBox.background", Color.BLACK);
    UIManager.put("ComboBox.foreground", Color.WHITE);

    UIManager.put("TextField.background", Color.BLACK);
    UIManager.put("TextField.foreground", Color.WHITE);
    UIManager.put("ToolBar.background", Color.BLACK);

    UIManager.put("Viewport.background", Color.BLACK);
    UIManager.put("Viewport.foreground", Color.WHITE);
    SwingUtilities.updateComponentTreeUI(this);

I was trying to change the color of my JFileChooser following some ideas from this post:

Change color of WindowsPlacesBar in JFileChooser

However I was not able to do so, I could not found which code is missing. Until now I got only this:

    UIManager.put("Panel.background", Color.BLACK);
    UIManager.put("Button.background", Color.BLACK);
    UIManager.put("ComboBox.background", Color.BLACK);
    UIManager.put("ComboBox.foreground", Color.WHITE);

    UIManager.put("TextField.background", Color.BLACK);
    UIManager.put("TextField.foreground", Color.WHITE);
    UIManager.put("ToolBar.background", Color.BLACK);

    UIManager.put("Viewport.background", Color.BLACK);
    UIManager.put("Viewport.foreground", Color.WHITE);
    SwingUtilities.updateComponentTreeUI(this);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

下壹個目標 2024-08-14 06:40:40
UIManager.put("ScrollPane.background", Color.BLACK);
UIManager.put("List.background", Color.BLACK);
UIManager.put("List.foreground", Color.WHITE);

或者,如果您愿意:

private static void setColors(Component c, Color fg, Color bg) {
    setColors0(c.getAccessibleContext(), fg, bg);
}

private static void setColors0(AccessibleContext ac, Color fg, Color bg) {
    ac.getAccessibleComponent().setForeground(fg);
    ac.getAccessibleComponent().setBackground(bg);
    int n = ac.getAccessibleChildrenCount();
    for (int i=0; i<n; i++) {
        setColors0(ac.getAccessibleChild(i).getAccessibleContext(), fg, bg);
    }
}
UIManager.put("ScrollPane.background", Color.BLACK);
UIManager.put("List.background", Color.BLACK);
UIManager.put("List.foreground", Color.WHITE);

or, if you prefer:

private static void setColors(Component c, Color fg, Color bg) {
    setColors0(c.getAccessibleContext(), fg, bg);
}

private static void setColors0(AccessibleContext ac, Color fg, Color bg) {
    ac.getAccessibleComponent().setForeground(fg);
    ac.getAccessibleComponent().setBackground(bg);
    int n = ac.getAccessibleChildrenCount();
    for (int i=0; i<n; i++) {
        setColors0(ac.getAccessibleChild(i).getAccessibleContext(), fg, bg);
    }
}
要走就滚别墨迹 2024-08-14 06:40:40

您需要创建一个 CustomFileChooser 类并重写 createDialog(Componentparent) 方法并设置对话框的 contentPane 背景颜色并设置对话框中每个组件的背景,

public class CustomFileChooser extends JFileChooser{
    public CustomFileChooser (){
        super();
        initComponent();
    }

    public CustomFileChooser (String currentDirPath){
        super(currentDirPath);
        initComponent();
    }

    public void initComponent(){
        Component c[] = getComponents();
        customizeUI(c);
    }


public void customizeUI(Component[] c){
    for(int i=0;i<c.length;i++){
        if(c[i] instanceof JPanel){
            ((JPanel)c[i]).setBackground(Color.black);
            if(((JPanel)c[i]).getComponentCount() !=0){
                customizeUI(((JPanel)c[i]).getComponents());
            }
        }
        if(c[i] instanceof JTextField){
            System.out.println("JTextField");
            ((JTextField)c[i]).setBackground(Color.RED);
        }
        if(c[i] instanceof JToggleButton){
            System.out.println("JToggleButton");
        }
        if(c[i] instanceof JScrollPane){
            System.out.println("JScrollPane");
        }

    }   
}
    @Override
    protected JDialog createDialog(Component parent) throws HeadlessException {
    FileChooserUI ui = getUI();
    String title = ui.getDialogTitle(this);
    putClientProperty(AccessibleContext.ACCESSIBLE_DESCRIPTION_PROPERTY,
                      title);

    JDialog dialog;

    if (parent instanceof Frame) {
        dialog = new JDialog((Frame)parent, title, true);
    } else {
        dialog = new JDialog((Dialog)parent, title, true);
    }
    dialog.setComponentOrientation(this.getComponentOrientation());

    Container contentPane = dialog.getContentPane();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(this, BorderLayout.CENTER);
    contentPane.setBackground(Color.black);


    dialog.pack();
    dialog.setLocationRelativeTo(parent);

    return dialog;
}}

You need to create a CustomFileChooser class and override createDialog(Component parent) method and set the dialog's contentPane background color and set background of each component in the dialog,

public class CustomFileChooser extends JFileChooser{
    public CustomFileChooser (){
        super();
        initComponent();
    }

    public CustomFileChooser (String currentDirPath){
        super(currentDirPath);
        initComponent();
    }

    public void initComponent(){
        Component c[] = getComponents();
        customizeUI(c);
    }


public void customizeUI(Component[] c){
    for(int i=0;i<c.length;i++){
        if(c[i] instanceof JPanel){
            ((JPanel)c[i]).setBackground(Color.black);
            if(((JPanel)c[i]).getComponentCount() !=0){
                customizeUI(((JPanel)c[i]).getComponents());
            }
        }
        if(c[i] instanceof JTextField){
            System.out.println("JTextField");
            ((JTextField)c[i]).setBackground(Color.RED);
        }
        if(c[i] instanceof JToggleButton){
            System.out.println("JToggleButton");
        }
        if(c[i] instanceof JScrollPane){
            System.out.println("JScrollPane");
        }

    }   
}
    @Override
    protected JDialog createDialog(Component parent) throws HeadlessException {
    FileChooserUI ui = getUI();
    String title = ui.getDialogTitle(this);
    putClientProperty(AccessibleContext.ACCESSIBLE_DESCRIPTION_PROPERTY,
                      title);

    JDialog dialog;

    if (parent instanceof Frame) {
        dialog = new JDialog((Frame)parent, title, true);
    } else {
        dialog = new JDialog((Dialog)parent, title, true);
    }
    dialog.setComponentOrientation(this.getComponentOrientation());

    Container contentPane = dialog.getContentPane();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(this, BorderLayout.CENTER);
    contentPane.setBackground(Color.black);


    dialog.pack();
    dialog.setLocationRelativeTo(parent);

    return dialog;
}}
樱花落人离去 2024-08-14 06:40:40

我搞乱了一点想出了这个:

   public void setFileChooserColors(JFileChooser jfc, Color f, Color b){
        jFileChooser1.setForeground(f);
        jFileChooser1.setBackground(b);

for (int index1 = 0; index1 < jfc.getAccessibleContext().getAccessibleChildrenCount(); index1++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleComponent().setForeground(f); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleComponent().setBackground(b); for (int index2 = 0; index2 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChildrenCount(); index2++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleComponent().setForeground(f); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleComponent().setBackground(b); for (int index3 = 0; index3 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChildrenCount(); index3++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleComponent().setForeground(f); for (int index4 = 0; index4 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChildrenCount(); index4++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleComponent().setForeground(f); for (int index5 = 0; index5 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChildrenCount(); index5++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleComponent().setForeground(f); for (int index6 = 0; index6 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChildrenCount(); index6++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleComponent().setForeground(f); for (int index7 = 0; index7 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChildrenCount(); index7++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChild(index7).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChild(index7).getAccessibleContext().getAccessibleComponent().setForeground(f); for (int index8 = 0; index8 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChild(index7).getAccessibleContext().getAccessibleChildrenCount(); index8++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChild(index7).getAccessibleContext().getAccessibleChild(index8).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChild(index7).getAccessibleContext().getAccessibleChild(index8).getAccessibleContext().getAccessibleComponent().setForeground(f); } } } } } } } } }<code>

I messed around a little a came up with this:

   public void setFileChooserColors(JFileChooser jfc, Color f, Color b){
        jFileChooser1.setForeground(f);
        jFileChooser1.setBackground(b);

for (int index1 = 0; index1 < jfc.getAccessibleContext().getAccessibleChildrenCount(); index1++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleComponent().setForeground(f); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleComponent().setBackground(b); for (int index2 = 0; index2 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChildrenCount(); index2++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleComponent().setForeground(f); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleComponent().setBackground(b); for (int index3 = 0; index3 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChildrenCount(); index3++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleComponent().setForeground(f); for (int index4 = 0; index4 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChildrenCount(); index4++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleComponent().setForeground(f); for (int index5 = 0; index5 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChildrenCount(); index5++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleComponent().setForeground(f); for (int index6 = 0; index6 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChildrenCount(); index6++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleComponent().setForeground(f); for (int index7 = 0; index7 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChildrenCount(); index7++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChild(index7).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChild(index7).getAccessibleContext().getAccessibleComponent().setForeground(f); for (int index8 = 0; index8 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChild(index7).getAccessibleContext().getAccessibleChildrenCount(); index8++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChild(index7).getAccessibleContext().getAccessibleChild(index8).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChild(index7).getAccessibleContext().getAccessibleChild(index8).getAccessibleContext().getAccessibleComponent().setForeground(f); } } } } } } } } }<code>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文