JFileChooser 自定义 - 将渐变绘制设置为 JFilechooser

JFileChooser Customization -Setting Gradient Paint to a JFilechooser

我正在对 JFileChooser UI 进行一些定制。我正在寻找 JFilechooser 的渐变背景。我所能做的只是一个自定义的 JFilechooser,但我不确定如何将 gardientbackground 添​​加到这个 Filechooser

 public class myCustomFileChooser extends JFileChooser{

        static Point compCoords;
        static Boolean isMaximized = false;
        static Dimension defaultSize = new Dimension(1280,720);
        static IRTitleBar titleBar;
        //
        static java.net.URL logoURL = IRLookAndFeel.class.getResource("photos/topbar/12_white.png");
        //
        static BufferedImage logoImg;


        @Override
        protected JDialog createDialog(Component parent) throws HeadlessException {

            FileChooserUI ui = getUI();
            String title = ui.getDialogTitle(this);
            putClientProperty(AccessibleContext.ACCESSIBLE_DESCRIPTION_PROPERTY,
                              title);

            JDialog dialog;
            Window window;
            window = JOptionPane.getFrameForComponent(parent);
            if (window instanceof Frame) {
                dialog = new JDialog((Frame)window, title, true);
                dialog.setUndecorated(true);
            } else {
                dialog = new JDialog((Dialog)window, title, true);
                dialog.setUndecorated(true);
            }
            dialog.setComponentOrientation(this.getComponentOrientation());
            //
            try {                
              IRJFrame.logoImg = ImageIO.read(IRJFrame.logoURL);
           } catch (IOException ex) {
                // handle exception...
           }
            //
            IRJFrame.titleBar = new IRTitleBar(IRJFrame.logoImg, BackgroundPanel.ACTUAL, 0.01f, 0.5f);
            //
            //Add Actions for Buttons
            //
            IRTitleBar.closeBtn.addActionListener((ActionEvent e) -> {
                System.exit(0);
            });
            //
            IRTitleBar.minimizeBtn.addActionListener((ActionEvent e) -> {
                //dialog.setState(JFrame.ICONIFIED);
            });
            //
            IRTitleBar.maximizeBtn.addActionListener((ActionEvent e) -> {
                if(isMaximized){
                    //setExtendedState(getExtendedState() | JFrame.NORMAL);
                    setSize(defaultSize);
                    setOpaque(false);
                    setLocation(150,150);
                }else {
                    //setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
                    setOpaque(false);
                }
                isMaximized = !isMaximized;
            });
            //
            compCoords = null;//new Point(0,0);
            //
            IRJFrame.titleBar.addMouseListener(new MouseListener(){
            //
            @Override
            public void mouseReleased(MouseEvent e) {

            compCoords = e.getPoint();

            }
            @Override
            public void mouseClicked(MouseEvent e) {
            compCoords = e.getPoint();
            }

            @Override
            public void mousePressed(MouseEvent e) {
            compCoords = e.getPoint();
            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
            });
            IRJFrame.titleBar.addMouseMotionListener(new MouseMotionListener(){

            @Override
            public void mouseDragged(MouseEvent e) {
            Point currCoords = e.getLocationOnScreen();
            setLocation(currCoords.x - compCoords.x, currCoords.y - compCoords.y);
            }

            @Override
            public void mouseMoved(MouseEvent e) {
            }
            });

            //


            Container contentPane = dialog.getContentPane();
            contentPane.setLayout(new BorderLayout());
            //contentPane.add(titleBar,BorderLayout.NORTH);
            contentPane.add(this, BorderLayout.CENTER);

            /*if (JDialog.isDefaultLookAndFeelDecorated()) {
                boolean supportsWindowDecorations =
                UIManager.getLookAndFeel().getSupportsWindowDecorations();
                if (supportsWindowDecorations) {
                    dialog.getRootPane().setWindowDecorationStyle(JRootPane.FILE_CHOOSER_DIALOG);
                }
            } */
            dialog.pack();
            dialog.setLocationRelativeTo(parent);
            //dialog.setUndecorated(true);
            //dialog.getRootPane().setWindowDecorationStyle(JRootPane.NONE);

            return dialog;
        }
    }

我应该怎么做才能自定义 JFileChooser 以便我拥有 gardient 背景。我无法找到绘画方法,也无法访问 JFilechooser 内容窗格。我该如何进行?

你可以:

  • 扩展 JFileChooser
  • 覆盖其 paintComponent 方法
  • 在此覆盖中,创建您的 GradientPaint 并用它填充组件
  • 然后(这是关键)递归地遍历它的子组件并使它们不透明,这样渐变就会显示出来
  • 选择哪些组件不透明,例如保存文件的JList。

例如:

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.geom.Point2D;

import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.SwingUtilities;

public class TestFileChooser {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFileChooser fileChooser = new MyFileChooser();
            fileChooser.showOpenDialog(null);
        });
    }
}

class MyFileChooser extends JFileChooser {

    private static final Color COLOR_0 = new Color(200, 200, 255);
    private static final Color COLOR_1 = Color.BLUE;

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

    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 g2 = (Graphics2D) g;
        Point p0 = new Point(0, 0);
        Point p1 = new Point(getWidth(), getHeight());
        Paint paint = new GradientPaint(p0 , COLOR_0, p1, COLOR_1);
        g2.setPaint(paint);
        g2.fillRect(0, 0, p1.x, p1.y);
    }
}