在Java中模拟触摸滚动
我正在寻找一种方法,用Java创建一个支持触摸滚动的触摸应用程序(不适用于移动设备)。 到目前为止,我一直在搜索,我正在调查如何做到这一点-我发现的是MT4J(),但它似乎不支持这一点(如果我错了,请纠正我)。 所以我的问题是,如何在水平触摸/滑动上模拟滚动事件 谢谢你的帮助! 亲切问候,, Alex您可以使用 以下是有关处理滚动事件的链接教程中的一些示例代码:在Java中模拟触摸滚动,java,touch,javafx,desktop-application,Java,Touch,Javafx,Desktop Application,我正在寻找一种方法,用Java创建一个支持触摸滚动的触摸应用程序(不适用于移动设备)。 到目前为止,我一直在搜索,我正在调查如何做到这一点-我发现的是MT4J(),但它似乎不支持这一点(如果我错了,请纠正我)。 所以我的问题是,如何在水平触摸/滑动上模拟滚动事件 谢谢你的帮助! 亲切问候,, Alex您可以使用 以下是有关处理滚动事件的链接教程中的一些示例代码: rect.setOnScroll(new EventHandler<ScrollEvent>() { @O
rect.setOnScroll(new EventHandler<ScrollEvent>() {
@Override public void handle(ScrollEvent event) {
if (!event.isInertia()) {
rect.setTranslateX(rect.getTranslateX() + event.getDeltaX());
rect.setTranslateY(rect.getTranslateY() + event.getDeltaY());
}
log("Rectangle: Scroll event" +
", inertia: " + event.isInertia() +
", direct: " + event.isDirect());
event.consume();
}
});
rect.setOnScrollStarted(new EventHandler<ScrollEvent>() {
@Override public void handle(ScrollEvent event) {
inc(rect);
log("Rectangle: Scroll started event");
event.consume();
}
});
rect.setOnScrollFinished(new EventHandler<ScrollEvent>() {
@Override public void handle(ScrollEvent event) {
dec(rect);
log("Rectangle: Scroll finished event");
event.consume();
}
});
rect.setOnScroll(新的EventHandler(){
@重写公共无效句柄(ScrollEvent事件){
如果(!event.isInertia()){
setTranslateX(rect.getTranslateX()+event.getDeltaX());
setTranslateY(rect.getTranslateY()+event.getDeltaY());
}
日志(“矩形:滚动事件”+
,惯性:“+event.isInertia()+
,direct:“+event.isDirect());
event.consume();
}
});
setOnScrollStarted(新的EventHandler(){
@重写公共无效句柄(ScrollEvent事件){
公司(rect);
日志(“矩形:滚动启动事件”);
event.consume();
}
});
setOnScrollFinished(新的EventHandler(){
@重写公共无效句柄(ScrollEvent事件){
dec(rect);
日志(“矩形:滚动完成事件”);
event.consume();
}
});
这通过对JScrollPane进行子分类来实现触摸和拖动滚动条
因为仅仅靠触摸和拖动是不够的,所以我增加了动力
这样,当鼠标按钮被释放时,它就会“抛出”滚动条
卷轴末端没有“反弹”,因为我负担不起
对“bounce”的所有者提起诉讼
它不是完全封装的,因为,虽然如果
该视图是jlist,视图上可能有需要的组件
如果此时正在拖动面板,则修改其响应。
还有一些组件,例如JRadioButton、JCheckBox等,会消耗
鼠标单击而不将其传递到容器,因此
需要添加TouchScroll的MouseListener和MouseMotionListener
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
public class TouchScroll extends JScrollPane implements MouseListener, MouseMotionListener {
private JScrollBar vbar = this.getVerticalScrollBar();
public TouchScroll(Component view){ // 1-arity CONSTRUCTOR
super(view);
view.addMouseListener(this);
view.addMouseMotionListener(this);
}
public TouchScroll() { super(); } // 0-arity CONSTRUCTOR
public void setViewportView(Component view) {
super.setViewportView(view);
view.addMouseListener(this);
view.addMouseMotionListener(this);
}
private static boolean wasdragged = false; // other MouseListeners may need to know this ...
public boolean wasDragged() { return wasdragged; } // ... this gives them safe access
static int lastY = 0, distY = 0;
double momentum = 0; // not really physical momentum but it will be used to 'throw' the scroll when the mouse button is released
static boolean lbdown = false;
@Override
public void mouseDragged(MouseEvent e) {
wasdragged = true;
distY = 0;
int currentY = e.getYOnScreen();
if(lbdown) {
distY = lastY - currentY;
vbar.setValue(distY + vbar.getValue());
if(Math.abs(distY) > 1)
momentum = distY + distY; // magnify and log the momentum for later use
}
lastY = currentY;
}
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
lastY = e.getYOnScreen();
lbdown = true;
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1)
lbdown = false;
if(wasdragged)
wasdragged = false;
if(Math.abs(momentum) <= 4.0) // then assume that the mouse wasn't moving (much) when the button was released
return;
// otherwise 'throw' the scroll
int max = vbar.getMaximum();
int count;
double brakingforce = 1.04; // set an initial braking force
for(count = 1000; count > 0; count--){ // don't allow it to accidentally go on forever
momentum = momentum / brakingforce; // apply the brake
if(Math.abs(momentum) < 1.5)
brakingforce = 1.02; // ease off the brake towards the end (gives a slight overrun ala iOS)
if(Math.abs(momentum) < 1.0) // bring it to a halt
break;
int val = vbar.getValue();
if(val < 0 || val > max) // prevent overrun
break;
vbar.setValue((int) momentum + val); // increment the scroll bar
try {
Thread.sleep(10); // slow the loop down so the user can see the scroll
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
public void mouseMoved(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
响应鼠标释放的任何组件可能需要执行以下操作:
public void mouseReleased(MouseEvent e) {
if(scroller.wasDragged())
return;
actionENTER();
}
JCheckBox checkbox = new JCheckBox(text);
checkbox.addMouseMotionListener(scroller);
checkbox.addMouseListener(scroller);
最后,如前所述,使用鼠标事件的组件将需要
要通知滚动条如下所示:
public void mouseReleased(MouseEvent e) {
if(scroller.wasDragged())
return;
actionENTER();
}
JCheckBox checkbox = new JCheckBox(text);
checkbox.addMouseMotionListener(scroller);
checkbox.addMouseListener(scroller);