java swing 中的 adapter_Java Swing中的MouseMotionListener,将其与组件内部的组件一起使用...

此临时方法利用了JScrollPane通常在键绑定中使用的现有操作。你必须调整N你的实现Scrollable。import java.awt.Color;import java.awt.Dimension;import java.awt.EventQueue;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.Action;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JViewport;import javax.swing.Timer;/** @see http://stackoverflow.com/questions/7201509 */public class ScrollAction extends JPanel {

private static final int TILE = 64;

private static final int DELTA = 16;

public ScrollAction() {

this.setOpaque(false);

this.setFocusable(true);

this.setPreferredSize(new Dimension(50 * TILE, 50 * TILE));

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.setColor(Color.lightGray);

int w = this.getWidth() / TILE + 1;

int h = this.getHeight() / TILE + 1;

for (int row = 0; row 

for (int col = 0; col 

if ((row + col) % 2 == 0) {

g.fillRect(col * TILE, row * TILE, TILE, TILE);

}

}

}

}

private void display() {

JFrame f = new JFrame("ScrollAction");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JScrollPane scrollPane = new JScrollPane(this);

final ScrollTimer left = new ScrollTimer(scrollPane, "scrollLeft");

final ScrollTimer right = new ScrollTimer(scrollPane, "scrollRight");

final ScrollTimer up = new ScrollTimer(scrollPane, "scrollUp");

final ScrollTimer down = new ScrollTimer(scrollPane, "scrollDown");

final JViewport viewPort = scrollPane.getViewport();

viewPort.setPreferredSize(new Dimension(5 * TILE, 5 * TILE));

viewPort.addMouseMotionListener(new MouseAdapter() {

@Override

public void mouseMoved(MouseEvent e) {

left.stop();

if (e.getX() 

left.start();

}

right.stop();

if (e.getX() > viewPort.getWidth() - DELTA) {

right.start();

}

up.stop();

if (e.getY() 

up.start();

}

down.stop();

if (e.getY() > viewPort.getHeight() - DELTA) {

down.start();

}

}

});

f.add(scrollPane);

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

private static final class ScrollTimer implements ActionListener {

private static int N = 10;

private static int DELAY = 100;

private String cmd;

private Timer timer;

private Action action;

private JScrollPane scrollPane;

private int count;

public ScrollTimer(JScrollPane scrollPane, String action) {

this.cmd = action;

this.timer = new Timer(DELAY, this);

this.action = scrollPane.getActionMap().get(action);

this.scrollPane = scrollPane;

}

@Override

public void actionPerformed(ActionEvent e) {

if (count++ 

action.actionPerformed(new ActionEvent(scrollPane, 0, cmd));

} else {

timer.stop();

}

}

public void start() {

count = 0;

timer.start();

}

public void stop() {

timer.stop();

count = 0;

}

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

new ScrollAction().display();

}

});

}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值