现在遇到一个问题,在JScrollPane中,如何
去掉垂直方向滚动条的向上和向下的两个按钮,使得只能显示滚动条,
在页面没有滑动的时候,滚动条是不可见的,页面是通过鼠标拖动进行滑动的,
当页面滚动的时候,滑动条可见,并且随着页面一起向上或者向下滑动,
在滑动过程中滚动条根据页面滑动的距离变换长短。

解决方案 »

  1.   

    方法一:将你原来的JScrollBar隐藏
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
    然后用一个JPanel加上一个JLabel来自己实现一个ScrollBar,这样想怎么实现都好控制,我以前也这样做过一个ScrollBar方法二:重载BasicScrollBarUI中的相关方法,这个方法比较繁琐,需要你对Swing的UI理解比较深
      

  2.   


    有这么考虑,我刚刚接触Swing不久,“用一个JPanel加上一个JLabel来自己实现一个ScrollBar”不知道怎样去做,能否把代码贴出来
      

  3.   

    写了个简单的例子给你看下import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.ScrollPaneConstants;
    import javax.swing.plaf.basic.BasicScrollBarUI;public class Test extends JFrame{ private static final long serialVersionUID = 1L;
    private Container content = null;
    private JTextArea area = null;
    private JScrollPane scrMain = null;
    private Point offsetV;

    public Test() {
    initialize();
    }

    private void initialize() {
    setSize(774, 740);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContent();
    setVisible(true);
    }

    public static void main(String[] args) {
    Test test = new Test();
    test.setVisible(true);
    } public void setContent(Container content) {
    this.content = content;
    } public Container getContent() {
    if (content == null) {
    content = getContentPane();
    content.setLayout(null);
    area = new JTextArea();
    area.setPreferredSize(new Dimension(700, 2000));
    scrMain = new JScrollPane(area);
    scrMain.setBounds(0, 0, 770, 706);
    scrMain.getVerticalScrollBar().setUI(new MyScrollBarUI());
    scrMain.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
    area.addMouseListener(new MouseAdapter(){
    public void mousePressed(MouseEvent e) {
    offsetV = e.getPoint();
    scrMain.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    }
    public void mouseReleased(MouseEvent e) {
    scrMain.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
    }
    });
    area.addMouseMotionListener(new MouseMotionListener(){
    //拖动的实现,速度没控制好,不过可以将就着看出效果
    public void mouseDragged(MouseEvent e) {
    int y = (e.getY() - offsetV.y)/20 + scrMain.getVerticalScrollBar().getValue();
    if (y > scrMain.getVerticalScrollBar().getMaximum()) {
    y = scrMain.getVerticalScrollBar().getMaximum();
    } else if (y < 0) {
    y = 0;
    }
    scrMain.getVerticalScrollBar().setValue(y);
    }
    public void mouseMoved(MouseEvent e) {
    }
    });
    content.add(scrMain);
    }
    return content;
    } class MyScrollBarUI extends BasicScrollBarUI{

    public void installUI(JComponent c) {
    super.installUI(c);
    //隐藏两个箭头
    incrButton.setVisible(false);
    decrButton.setVisible(false);
    }

    //这是绘制拖动条的部分
    protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
    super.paintThumb(g, c, thumbBounds);
    //觉得原来的拖动条难看可以重写这个方法,下面是我重画的一个例子
    g.setColor(Color.LIGHT_GRAY);
    g.fillRect(thumbBounds.x, thumbBounds.y, thumbBounds.width, thumbBounds.height);
    g.setColor(Color.GRAY);
    g.drawRect(thumbBounds.x, thumbBounds.y, thumbBounds.width, thumbBounds.height-1);
    g.setColor(Color.WHITE);
    g.drawRect(thumbBounds.x+1, thumbBounds.y+1, thumbBounds.width-2, thumbBounds.height-3);
    }
    }}
      

  4.   

      //隐藏两个箭头
                incrButton.setVisible(false);
                decrButton.setVisible(false);
    这里隐藏了两个箭头,但是我想直接把这两个按钮去掉,不知道有没有好的方法
      

  5.   

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JScrollBar;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.ScrollPaneConstants;
    import javax.swing.UIManager;
    import javax.swing.plaf.basic.BasicArrowButton;
    import javax.swing.plaf.basic.BasicScrollBarUI;public class Test extends JFrame{ private static final long serialVersionUID = 1L;
    private Container content = null;
    private JTextArea area = null;
    private JScrollPane scrMain = null;
    private Point offsetV;

    public Test() {
    initialize();
    }

    private void initialize() {
    setSize(774, 740);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContent();
    setVisible(true);
    }

    public static void main(String[] args) {
    Test test = new Test();
    test.setVisible(true);
    } public void setContent(Container content) {
    this.content = content;
    } public Container getContent() {
    if (content == null) {
    content = getContentPane();
    content.setLayout(null);
    area = new JTextArea();
    area.setPreferredSize(new Dimension(700, 2000));
    scrMain = new JScrollPane(area);
    scrMain.setBounds(0, 0, 770, 706);
    scrMain.getVerticalScrollBar().setUI(new MyScrollBarUI());
    scrMain.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
    area.addMouseListener(new MouseAdapter(){
    public void mousePressed(MouseEvent e) {
    offsetV = e.getPoint();
    scrMain.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    }
    public void mouseReleased(MouseEvent e) {
    scrMain.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
    }
    });
    area.addMouseMotionListener(new MouseMotionListener(){
    //拖动的实现,速度没控制好,不过可以将就着看出效果
    public void mouseDragged(MouseEvent e) {
    int y = (e.getY() - offsetV.y)/20 + scrMain.getVerticalScrollBar().getValue();
    if (y > scrMain.getVerticalScrollBar().getMaximum()) {
    y = scrMain.getVerticalScrollBar().getMaximum();
    } else if (y < 0) {
    y = 0;
    }
    scrMain.getVerticalScrollBar().setValue(y);
    }
    public void mouseMoved(MouseEvent e) {
    }
    });
    content.add(scrMain);
    }
    return content;
    } class MyScrollBarUI extends BasicScrollBarUI {

    public void installUI(JComponent c) {
    super.installUI(c);
    //隐藏两个箭头
    incrButton.setVisible(false);
    incrButton.setSize(0,0);
    decrButton.setVisible(false);
    decrButton.setSize(0,0);
    } protected void layoutVScrollbar(JScrollBar sb) {
    super.layoutVScrollbar(sb);
    incrButton.setBounds(0,0,0,0);
    decrButton.setBounds(0,0,0,0);
    trackRect.setBounds(0, 0, trackRect.width, trackRect.height+20);
    } protected JButton createDecreaseButton(int orientation)  {
    return new MyArrowButton(orientation, 
    UIManager.getColor("ScrollBar.thumb"),
    UIManager.getColor("ScrollBar.thumbShadow"),
    UIManager.getColor("ScrollBar.thumbDarkShadow"),
    UIManager.getColor("ScrollBar.thumbHighlight"));
    } protected JButton createIncreaseButton(int orientation)  {
    return new MyArrowButton(orientation, 
    UIManager.getColor("ScrollBar.thumb"), 
    UIManager.getColor("ScrollBar.thumbShadow"), 
    UIManager.getColor("ScrollBar.thumbDarkShadow"), 
    UIManager.getColor("ScrollBar.thumbHighlight"));
    }

    //这是绘制拖动条的部分
    protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
    super.paintThumb(g, c, thumbBounds);
    //觉得原来的拖动条难看可以重写这个方法,下面是我重画的一个例子
    g.setColor(Color.LIGHT_GRAY);
    g.fillRect(thumbBounds.x, thumbBounds.y, thumbBounds.width, thumbBounds.height);
    g.setColor(Color.GRAY);
    g.drawRect(thumbBounds.x, thumbBounds.y, thumbBounds.width, thumbBounds.height-1);
    g.setColor(Color.WHITE);
    g.drawRect(thumbBounds.x+1, thumbBounds.y+1, thumbBounds.width-2, thumbBounds.height-3);
    }
    }

    //重写箭头按钮,把默认大小设为0
    class MyArrowButton extends BasicArrowButton {
    private static final long serialVersionUID = 1L; public MyArrowButton(int direction, Color background, Color shadow,
    Color darkShadow, Color highlight) {
    super(direction, background, shadow, darkShadow, highlight);
    }

    public Dimension getPreferredSize() {
                return new Dimension(0, 0);
            }

    public Dimension getMinimumSize() {
                return new Dimension(0, 0);
            }

    }}
      

  6.   

    刚才的代码里很多无用的部分,加了点注释
    简化了一下,看这个吧
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.ScrollPaneConstants;
    import javax.swing.UIManager;
    import javax.swing.plaf.basic.BasicArrowButton;
    import javax.swing.plaf.basic.BasicScrollBarUI;public class Test extends JFrame{ private static final long serialVersionUID = 1L;
    private Container content = null;
    private JTextArea area = null;
    private JScrollPane scrMain = null;
    private Point offsetV;

    public Test() {
    initialize();
    }

    private void initialize() {
    setSize(774, 740);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContent();
    setVisible(true);
    }

    public static void main(String[] args) {
    Test test = new Test();
    test.setVisible(true);
    } public Container getContent() {
    if (content == null) {
    content = getContentPane();
    content.setLayout(null);
    area = new JTextArea();
    area.setText("0");
    for (int i=1;i<1000;i++) {
    area.append("\n" + i);
    }
    scrMain = new JScrollPane(area);
    scrMain.setBounds(0, 0, 770, 706);
    scrMain.getVerticalScrollBar().setUI(new MyScrollBarUI());
    scrMain.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
    area.addMouseListener(new MouseAdapter(){
    public void mousePressed(MouseEvent e) {
    offsetV = e.getPoint();
    scrMain.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    }
    public void mouseReleased(MouseEvent e) {
    scrMain.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
    }
    });
    area.addMouseMotionListener(new MouseMotionListener(){
    //拖动的实现,速度没控制好,不过可以将就着看出效果
    public void mouseDragged(MouseEvent e) {
    int y = (e.getY() - offsetV.y)/20 + scrMain.getVerticalScrollBar().getValue();
    if (y > scrMain.getVerticalScrollBar().getMaximum()) {
    y = scrMain.getVerticalScrollBar().getMaximum();
    } else if (y < 0) {
    y = 0;
    }
    scrMain.getVerticalScrollBar().setValue(y);
    }
    public void mouseMoved(MouseEvent e) {
    }
    });
    content.add(scrMain);
    }
    return content;
    } class MyScrollBarUI extends BasicScrollBarUI {
    @Override
    protected JButton createDecreaseButton(int orientation) {
    return new MyArrowButton(orientation, //用自己的箭头替代默认的
    UIManager.getColor("ScrollBar.thumb"),
    UIManager.getColor("ScrollBar.thumbShadow"),
    UIManager.getColor("ScrollBar.thumbDarkShadow"),
    UIManager.getColor("ScrollBar.thumbHighlight"));
    }

    @Override
    protected JButton createIncreaseButton(int orientation) {
    return new MyArrowButton(orientation, //用自己的箭头替代默认的
    UIManager.getColor("ScrollBar.thumb"), 
    UIManager.getColor("ScrollBar.thumbShadow"), 
    UIManager.getColor("ScrollBar.thumbDarkShadow"), 
    UIManager.getColor("ScrollBar.thumbHighlight"));
    }

    //这是绘制拖动条的部分
    @Override
    protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
    super.paintThumb(g, c, thumbBounds);
    //觉得原来的拖动条难看可以重写这个方法,下面是我重画的一个例子
    g.setColor(Color.LIGHT_GRAY);
    g.fillRect(thumbBounds.x, thumbBounds.y, thumbBounds.width, thumbBounds.height);
    g.setColor(Color.GRAY);
    g.drawRect(thumbBounds.x, thumbBounds.y, thumbBounds.width, thumbBounds.height-1);
    g.setColor(Color.WHITE);
    g.drawRect(thumbBounds.x+1, thumbBounds.y+1, thumbBounds.width-2, thumbBounds.height-3);
    }
    }

    //重写箭头按钮,把默认大小设为0
    class MyArrowButton extends BasicArrowButton {
    private static final long serialVersionUID = 1L;
    public MyArrowButton(int direction, Color background, Color shadow,
    Color darkShadow, Color highlight) {
    super(direction, background, shadow, darkShadow, highlight);
    }

    @Override
    public Dimension getPreferredSize() {//将箭头默认大小设成0
                return new Dimension(0, 0);
            }

    @Override
    public Dimension getMinimumSize() {//将箭头最小大小设成0
                return new Dimension(0, 0);
            }
    }
    }
      

  7.   

    先前在办公室,不太方便,前面写的代码先前没有多想,没有很好的解决,刚回到住的地方,细想了下
    觉得先前的思路不对。重新写了一下这个代码,现在应该是比较好的解决了这个问题了。能够做到跟我们平时拖动滚动条时的滚动效果一样。
    主要在于要转换坐标,把鼠标事件的坐标转换到JScrollpane上面,然后拖动鼠标产生的高度差就是滑动条在JScrollpane上面所移动的像素,完全跟拖动滑动条的效果是一样的。不知道描述清楚没有,现在贴代码import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.ScrollPaneConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.plaf.basic.BasicArrowButton;
    import javax.swing.plaf.basic.BasicScrollBarUI;public class Test extends JFrame{    private static final long serialVersionUID = 1L;
        private Container content = null;
        private JTextArea area = null;
        private JScrollPane scrMain = null;
        private Point offsetV;
        private int hy;
        private int value;
        public Test() {
            initialize();
        }
        
        private void initialize() {
            setSize(774, 740);
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            getContent();
            setVisible(true);
        }
        
        public static void main(String[] args) {
            Test test = new Test();
            test.setVisible(true);
        }    public Container getContent() {
            if (content == null) {
                content = getContentPane();
                content.setLayout(null);
                area = new JTextArea();
                area.setText("0");
                for (int i=1;i<1000;i++) {
                    area.append("\n" + i);
                }
                scrMain = new JScrollPane(area);
                
                scrMain.setBounds(0, 0, 770, 706);
                scrMain.getVerticalScrollBar().setUI(new MyScrollBarUI());
                scrMain.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
                area.addMouseListener(new MouseAdapter(){
                    public void mousePressed(MouseEvent e) {
                     //   offsetV = e.getPoint();
                        offsetV = SwingUtilities.convertPoint((JTextArea)e.getSource(), e.getPoint(), ((JTextArea)e.getSource()).getParent());
                        hy = offsetV.y;
                        scrMain.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                    }
                    public void mouseReleased(MouseEvent e) {
                        scrMain.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
                    }
                });
                area.addMouseMotionListener(new MouseMotionListener(){
                    
                    public void mouseDragged(MouseEvent e) {
                        value = scrMain.getVerticalScrollBar().getValue();
                        //int y = (e.getY() - offsetV.y)/20 + (scrMain.getVerticalScrollBar().getValue());
                        int y = 0;
                        Point newPoint = SwingUtilities.convertPoint((JTextArea)e.getSource(), e.getPoint(), ((JTextArea)e.getSource()).getParent());
                        
                        int my = (newPoint.y - hy);
                        y = value + my;
                        if(y > scrMain.getVerticalScrollBar().getMaximum()){
                                y = scrMain.getVerticalScrollBar().getMaximum();
                           }
                        if (y < 0) {
                                y = 0;
                         }
                        hy = newPoint.y;
                        scrMain.getVerticalScrollBar().setValue(y);
                    }
                    public void mouseMoved(MouseEvent e) {
                    }
                });
                content.add(scrMain);
            }
            return content;
        }    class MyScrollBarUI extends BasicScrollBarUI {
            @Override
            protected JButton createDecreaseButton(int orientation) {
                return new MyArrowButton(orientation, //用自己的箭头替代默认的
                        UIManager.getColor("ScrollBar.thumb"),
                        UIManager.getColor("ScrollBar.thumbShadow"),
                        UIManager.getColor("ScrollBar.thumbDarkShadow"),
                        UIManager.getColor("ScrollBar.thumbHighlight"));
            }
            
            @Override
            protected JButton createIncreaseButton(int orientation) {
                return new MyArrowButton(orientation, //用自己的箭头替代默认的
                        UIManager.getColor("ScrollBar.thumb"), 
                        UIManager.getColor("ScrollBar.thumbShadow"), 
                        UIManager.getColor("ScrollBar.thumbDarkShadow"), 
                        UIManager.getColor("ScrollBar.thumbHighlight"));
            }
            
            //这是绘制拖动条的部分
            @Override
            protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
                super.paintThumb(g, c, thumbBounds);
                //觉得原来的拖动条难看可以重写这个方法,下面是我重画的一个例子
                g.setColor(Color.LIGHT_GRAY);
               // g.setColor(Color.black);
                g.fillRect(thumbBounds.x, thumbBounds.y, thumbBounds.width, thumbBounds.height);
                g.setColor(Color.GRAY);
                g.drawRect(thumbBounds.x, thumbBounds.y, thumbBounds.width, thumbBounds.height-1);
                g.setColor(Color.WHITE);
                g.drawRect(thumbBounds.x+1, thumbBounds.y+1, thumbBounds.width-2, thumbBounds.height-3);
            }
        }
        
        //重写箭头按钮,把默认大小设为0
        class MyArrowButton extends BasicArrowButton {
            private static final long serialVersionUID = 1L;
            public MyArrowButton(int direction, Color background, Color shadow,
                    Color darkShadow, Color highlight) {
                super(direction, background, shadow, darkShadow, highlight);
            }
            
            @Override
            public Dimension getPreferredSize() {//将箭头默认大小设成0
                return new Dimension(0, 0);
            }
            
            @Override
            public Dimension getMinimumSize() {//将箭头最小大小设成0
                return new Dimension(0, 0);
            }
        }
    }
      

  8.   

    这样拖动的效果是比较平滑
    呵呵,希望楼主的目标能早日实现
    如果UI方面有需要修改可以再一起研究哦
    我最近正好在研究重写LookAndFeel