JFileChooser定制-将渐变涂料设置为JFilechooser

时间:2018-11-01 14:07:31

标签: java swing background 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,以使我具有灵活的背景知识。我找不到绘画方法,也无法访问JFilechooser内容窗格。我应该如何进行?

1 个答案:

答案 0 :(得分:3)

您可以:

  • 扩展JFileChooser
  • 覆盖其paintComponent方法
  • 在此替代项内,创建GradientPaint并用它填充组件
  • 然后(这是关键)递归地遍历其子组件并使它们不透明,以便渐变将通过显示
  • 选择要使哪些组件变得不透明,例如保存文件的JList。

例如:

enter image description here

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);
    }
}