50,530
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
}
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;
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();
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;
int my = (e.getY() - hy)/2;
System.out.println(my);
if(my > 0){
y = value + my;
if(y > scrMain.getVerticalScrollBar().getMaximum()){
y = scrMain.getVerticalScrollBar().getMaximum();
}
}else{
y = value + my;
if (y < 0) {
y = 0;
}
}
/*System.out.println(y);
if (y > scrMain.getVerticalScrollBar().getMaximum()) {
y = scrMain.getVerticalScrollBar().getMaximum();
} else if (y < 0) {
y = 0;
}*/
hy = e.getY();
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);
}
}
}
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.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.plaf.basic.BasicScrollBarUI;
public class MyScrPane extends JFrame{
private static final long serialVersionUID = 1L;
private Container content = null;
private JScrollPane scrMain = null;
private Point offsetV;
private JLabel[] labs = null;
private Box box = null;
public MyScrPane() {
initialize();
}
private void initialize() {
setSize(774, 740);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContent();
setVisible(true);
}
public static void main(String[] args) {
MyScrPane test = new MyScrPane();
test.setVisible(true);
}
public Container getContent() {
if (content == null) {
content = getContentPane();
content.setLayout(null);
box = new Box(BoxLayout.Y_AXIS);
labs = new JLabel[200];
for (int i=0;i<labs.length;i++) {
labs[i] = new JLabel(""+i);
box.add(labs[i]);
}
scrMain = new JScrollPane(box);
scrMain.setBounds(0, 0, 770, 706);
scrMain.getVerticalScrollBar().setUI(new MyScrollBarUI());
scrMain.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
box.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);
}
});
box.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);
}
}
}