自定义滚动条箭头

时间:2013-10-24 22:28:26

标签: java image swing custom-component jscrollbar

我正在尝试使用以下代码自定义jScrollPane。它有效,它按照我想要的方式改变颜色,但隐藏了箭头按钮。

我想要的是让它们再次可见并使用自定义图像进行更改。我尝试在这个论坛上搜索,但我找不到任何关于它的信息。

我希望有人可以帮助我。提前致谢!

    private Image imageThumb, imageTrack;
    private JButton b = new JButton() {
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(0, 0);
        }
    };

  public YourScrollbarUI () {
        imageThumb = WrapImage .create(45, 45, new Color(46,218,163));
        imageTrack = WrapImage .create(32, 32, new Color(90,90,90));
    }

    @Override
    protected void paintThumb(Graphics g, JComponent c, Rectangle r) {
        g.setColor(Color.blue);
        ((Graphics2D) g).drawImage(imageThumb,
                r.x, r.y, r.width, r.height, null);
    }

    @Override
    protected void paintTrack(Graphics g, JComponent c, Rectangle r) {
        ((Graphics2D) g).drawImage(imageTrack,
                r.x, r.y, r.width, r.height, null);
    }

    @Override
    protected JButton createDecreaseButton(int orientation) {
        return b;
    }

    @Override
    protected JButton createIncreaseButton(int orientation) {
        return b;
    }

private static class WrapImage {

    static public Image create(int w, int h, Color c) {
        BufferedImage bi = new BufferedImage(
                w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = bi.createGraphics();
        g2d.setPaint(c);
        g2d.fillRect(0, 0, w, h);
        g2d.dispose();
        return bi;}}

1 个答案:

答案 0 :(得分:2)

  

它以我想要的方式改变颜色,但隐藏了箭头按钮。

问题在于:

private JButton b = new JButton() {
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(0, 0); // why (0,0) ???
    }
};

在您的代码b按钮中,负责通过createDecreaseButtoncreateIncreaseButton方法绘制箭头。如果其首选大小为(0,0),那么逻辑上它将不可见。

  

我想要的是让它们再次可见并用一个改变它们   自定义图片。

您需要修改createDecreaseButtoncreateIncreaseButton,以使其返回带有所需图标的新JButton

<强>更新

  

我已经尝试过使用prefferedsize(使它们相同   大小作为自定义图像),但自定义箭头图像仍然没有   出现。我一无所知

看看这个工作示例。 MyScrollbarUI就像你班级一样从BasicScrollBarUI延伸。您将看到该键覆盖了按钮的getPreferredSize()方法,并根据需要设置了相应的图标。

在这方面,我应该说BasicScrollBarUI.createDecreaseButton(int orientation)BasicScrollBarUI.createIncreaseButton(int orientation)方法记录很少(没有javadoc)。但是如果你使用IDE深入这个类,那么你会看到orientation参数可以采用以下值之一:SwingConstants.NORTH, SwingConstants.SOUTH, SwingConstants.EAST, SwingConstants.WEST。在查看getAppropriateIcon(int orientation)方法时请记住这一点。

这些是使用过的图标:Up arrow Down arrow Left arrow Right arrow

import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicScrollBarUI;

public class Demo {

    private void initGUI(){
        JScrollPane scrollPane = new JScrollPane(new JTextArea(10, 20));
        scrollPane.getHorizontalScrollBar().setUI(new MyScrollbarUI());
        scrollPane.getVerticalScrollBar().setUI(new MyScrollbarUI());

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(scrollPane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {        
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().initGUI();
            }
        });
    }

    class MyScrollbarUI extends BasicScrollBarUI {

        private ImageIcon downArrow, upArrow, leftArrow, rightArrow;

        public MyScrollbarUI(){
            try {
                upArrow = new ImageIcon(new java.net.URL("http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/16/Actions-arrow-up-icon.png"));
                downArrow = new ImageIcon(new java.net.URL("http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/16/Actions-arrow-down-icon.png"));
                rightArrow = new ImageIcon(new java.net.URL("http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/16/Actions-arrow-right-icon.png"));
                leftArrow = new ImageIcon(new java.net.URL("http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/16/Actions-arrow-left-icon.png"));
            } catch (java.net.MalformedURLException ex) {
                ex.printStackTrace();
            }        
        }

        @Override
        protected JButton createDecreaseButton(int orientation) {
            JButton decreaseButton = new JButton(getAppropriateIcon(orientation)){
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(22, 22);
                }
            };
            return decreaseButton;
        }

        @Override
        protected JButton createIncreaseButton(int orientation) {
            JButton increaseButton = new JButton(getAppropriateIcon(orientation)){
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(22, 22);
                }
            };
            return increaseButton;
        }

        private ImageIcon getAppropriateIcon(int orientation){
            switch(orientation){
                case SwingConstants.SOUTH: return downArrow;
                case SwingConstants.NORTH: return upArrow;
                case SwingConstants.EAST: return rightArrow;
                    default: return leftArrow;
            }
        }
    }    

}

<强>截图

enter image description here