自定义滚动条箭头 [英] Custom scrollbar arrows

查看:157
本文介绍了自定义滚动条箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码来自定义jScrollPane.它可以正常工作,它以我想要的方式更改颜色,但是隐藏了箭头按钮.

Im am trying to customize a jScrollPane using the code below. It works, It changes the color the way I want it, but hides the arrowbuttons.

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

What I want is to make them visible again and change them with a custom image. I tried searching on this forum, but I couldnt find any info about it.

我希望有人能帮助我.提前致谢!

I hope someone can help me. Thanks in advance!

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

推荐答案

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

It changes the color the way I want it, but hides the arrowbuttons.

问题出在这里

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

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

In your code b button is the responsible for painting the arrows through createDecreaseButton and createIncreaseButton methods. If its preferred size is (0,0) then logically it won't be visible.

我想要的是让它们再次可见,并用 自定义图片.

What I want is to make them visible again and change them with a custom image.

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

You need to modify createDecreaseButton and createIncreaseButton to make them return a new JButton with the desired icon.

更新

我已经尝试过使用偏好的尺寸(使它们相同 大小作为自定义图片),但自定义arrowimage仍然没有 出现.我无能为力

I already tried playing with the prefferedsize (making them the same size as the custom image), but the custom arrowimages are still not showing up. Im clueless

看看这个工作示例. MyScrollbarUI源自 BasicScrollBarUI 就像您的班级一样.您会看到按键覆盖了按钮的getPreferredSize()方法,并根据需要设置了适当的图标.

Look at this working example. MyScrollbarUI extends from BasicScrollBarUI just as your class does. You'll see the key is overrding the button's getPreferredSize() method and setting the appropriate icon as needed.

在这方面,我应该说 BasicScrollBarUI.createDecreaseButton(int方向)

In this regard I should say BasicScrollBarUI.createDecreaseButton(int orientation) and BasicScrollBarUI.createIncreaseButton(int orientation) methods are poorly documented (there's no javadoc). But if you dive into this class using an IDE then you'll see orientation paramenter can take one of these values: SwingConstants.NORTH, SwingConstants.SOUTH, SwingConstants.EAST, SwingConstants.WEST. Keep this in mind when look at getAppropriateIcon(int orientation) method.

以下使用的图标:

These are used icons:

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

}

屏幕截图

这篇关于自定义滚动条箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆