【问题标题】:JFileChooser Buttons CustomisationsJFileChooser 按钮自定义
【发布时间】:2018-11-01 15:37:42
【问题描述】:

我正在努力为 JFileChooser 提供渐变背景。在 StackOverflow 的帮助下,我能够为 JFileChooser 提供渐变背景,但 JFilechooser 中的按钮仍然具有白色背景。 有什么办法可以去掉吗?

package javaapplication2;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Point;
import java.awt.RenderingHints;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.UIManager;

/**
 *
 * @author navpreet.kaur
 */
class MyFileChooser extends JFileChooser {

    private final Color color1 = Color.DARK_GRAY;
    private final Color color2 = new Color(0, 0, 0, 0.65f); //Color.BLACK;
    final Color color3 = new Color(0, 0, 0, 0.33f);
    private final Color color4 = new Color(0, 0, 0, 0.0f);

    private final int shadowHeight = 20;
    private final GradientPaint mainGradient;
    private final GradientPaint shadowGradient;

    java.net.URL defaultViewURL = IRLookAndFeel.class.getResource("DefaultView_Icon.png");
    java.net.URL detailsViewURL = IRLookAndFeel.class.getResource("DetailsView_Icon.png");
    java.net.URL homeFolderURL = IRLookAndFeel.class.getResource("Home_Icon.png");
    java.net.URL listViewURL = IRLookAndFeel.class.getResource("listView_Icon.png");
    java.net.URL newFolderURL = IRLookAndFeel.class.getResource("Newfolder.png");
    java.net.URL upFolderURL = IRLookAndFeel.class.getResource("Upfolder.png");

    ImageIcon fcDefaultViewIcon = new ImageIcon(defaultViewURL);
    ImageIcon fcDetailsViewIcon = new ImageIcon(detailsViewURL);
    ImageIcon fcHomeFolderIcon = new ImageIcon(homeFolderURL);
    ImageIcon fcListViewIcon = new ImageIcon(listViewURL);
    ImageIcon fcNewFolderIcon = new ImageIcon(newFolderURL);
    ImageIcon fcUpFolderIcon = new ImageIcon(upFolderURL);

    public MyFileChooser() {
        Component[] comps = getComponents();
        recursiveTransparent(comps);

        mainGradient = new GradientPaint(0, 0, color1, 0, 600, color2);
        shadowGradient = new GradientPaint(0, 0, color3, 0, shadowHeight, color4);

           UIManager.put("FileChooser.detailsViewIcon", fcDetailsViewIcon);
           UIManager.put ("FileChooser.homeFolderIcon", fcHomeFolderIcon);
           UIManager.put ("FileChooser.listViewIcon", fcListViewIcon);
           UIManager.put ("FileChooser.newFolderIcon", fcNewFolderIcon);
           UIManager.put ("FileChooser.upFolderIcon", fcUpFolderIcon);
           UIManager.put ("FileChooser.viewMenuIcon", fcDefaultViewIcon);
    }

    private void recursiveTransparent(Component[] comps) {
        for (Component comp : comps) {
            if (comp instanceof JComponent && !(comp instanceof JList)) {
                ((JComponent) comp).setOpaque(false);
            }
            if (comp instanceof Container) {
                Component[] subComps = ((Container) comp).getComponents();
                recursiveTransparent(subComps);
            }
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
      super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

        int width = getWidth();
        int height = getHeight();


        g2d.setPaint(mainGradient);
        g2d.fillRect(0, 0, width, height);

        g2d.setPaint(shadowGradient);
        g2d.fillRect(0, 0, width, shadowHeight);
    }
}`

我尝试使用 UIManager 属性,但没有任何帮助。我的问题是关于如何更改图标以及 JFilechooser 上的确定和取消按钮?

![在此处输入图片描述][1]

使用你提到的代码,我可以在 JFilechooser 中执行按钮和标签

  if (comp instanceof JButton) {
             JButton b =  (JButton) comp;
             b.setContentAreaFilled(false);
             b.setForeground(Color.white);
             b.setOpaque(false);
             b.setBorderPainted(true);
        }

        if (comp instanceof JLabel) {
             JLabel l =  (JLabel) comp;
             l.setForeground(Color.white);
        }

但是有一个带有 JFilechooser 的组合框,我也在尝试更改属性,但是 JComboBox 没有遵循 L 和 F。我该怎么办?

【问题讨论】:

  • 忘记 JFileChooser。首先了解如何自定义添加到面板的 JButton。所有文件选择器代码都与自定义单个按钮的问题无关。提问时要简化问题。问题是 LAF 控制了 JButton 的绘制。在解决这个更简单的问题之前,您将无法修复文件选择器。

标签: java swing background gradient jfilechooser


【解决方案1】:

LAF 控制 JButton 的背景绘制,以在 Widows 上为您提供简单的渐变绘制。其他 LAF 可能会做一些不同的事情。

如果您想禁用此功能,可以尝试:

JButton button = new JButton( "Text Button" );
button.setContentAreaFilled( false );

现在面板的背景将显示出来。

【讨论】:

  • 非常感谢 Camickr! .我已经在 J​​FileChooser 上完成了按钮和标签定制。就 JCombo Boxes 而言,我可以在那里设置背景,但组合框中的列表是扭曲的。如何在 JFileChooser 中绘制组合框?
  • but the the list in the combo boxes is distorted. - 我不知道这意味着什么。与我的原始评论相同,通过弄清楚如何在添加到面板的组合框上执行此操作并忘记 JFileChooser 来简化问题。发布正确的minimal reproducible example,演示您如何尝试自定义组合框,以便我们了解问题所在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-22
  • 1970-01-01
  • 2017-11-14
  • 2016-08-08
  • 2013-09-20
  • 2015-08-23
相关资源
最近更新 更多