我的java项目中的JavaFX ScrollPane在JFXPanel中有一个。这个项目将在触摸显示器没有键盘,所以我需要模拟触摸屏滚动。
问题是:如果我在ScrollPane中拖动鼠标,它可以正常工作,但是如果我将鼠标拖动到有按钮的地方,它就无法滚动(其想法是,ScrollPane中将填充没有空格的按钮)。
下面是在我的代码中创建ScrollPane的部分。
private static Scene createScene(int wS, int hS) {
ScrollPane scrollPane = new ScrollPane();
FlowPane flowpane = new FlowPane();
scrollPane.setCache(true);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setPannable(true);
for(int i=0; i<100; i++)
flowpane.getChildren().add((new ButtonProduct(wS, "Apple " + (i+1)));
scrollPane.setContent(flowpane);
Scene scene = new Scene(scrollPane);
return (scene);
}编辑:
当按钮被拖动时,我添加了一个MouseEvent,但是我不知道如何“使滚动窗格被选中”来移动它。
public ButtonProduct(int wS, String t) {
this.setText(t);
this.setMinSize((int) (wS*sizeW), (int) (wS*sizeW));
this.setPrefSize((int) (wS*sizeW), (int) (wS*sizeW));
this.setMaxSize((int) (wS*sizeW), (int) (wS*sizeW));
this.setOnDragDetected(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
System.out.println("Dragged Button");
e.consume();
}
});
}发布于 2021-01-31 05:02:35
我找到了答案,这是ScrollPane中按钮(Node)的代码。,但是,您需要还原Button事件,以获得更好的性能,而下一段代码没有开发这种性能。
import javax.swing.*;
import java.awt.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.control.ScrollPane;
public class ButtonProduct extends Button {
private static double sizeW = .075;
//Constructor
public ButtonProduct(ScrollPane sC, int wS, String t) {
this.setText(t);
this.setMinSize((int) (wS*sizeW), (int) (wS*sizeW));
this.setPrefSize((int) (wS*sizeW), (int) (wS*sizeW));
this.setMaxSize((int) (wS*sizeW), (int) (wS*sizeW));
this.setOnDragDetected(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
((Button) e.getSource()).setEventDispatcher(sC.getEventDispatcher());
sC.requestFocus();
e.consume();
}
});
}
}https://stackoverflow.com/questions/65965311
复制相似问题