繁体   English   中英

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,以便我有一个适合的背景。 我无法找到paint方法,也无法访问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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM