【问题标题】:How to make JavaFX ScrollPane keep scrolling if a button inside ScrollPane is dragged?如果拖动 ScrollPane 内的按钮,如何使 JavaFX ScrollPane 继续滚动?
【发布时间】:2021-05-04 00:18:14
【问题描述】:

我的 java 项目中的 JFXPanel 中有一个 JavaFX ScrollPane。该项目将在没有键盘的触摸显示器中进行,因此我需要模拟触摸屏滚动。

问题是;如果我在 ScrollPane 中拖动鼠标,它可以正常工作,但如果我将鼠标拖动到有按钮的地方,它就无法滚动(想法是 ScrollPane 将充满按钮,它们之间没有空格)。

The white part and the buttons are inside 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();
            }
        });
    }

【问题讨论】:

  • Swing 和这个有什么关系?
  • 在黑暗中拍摄,但我的假设是您需要捕获鼠标单击事件,确定它是否是一个手势(例如滑动手指而不是轻敲它),并做出相应的反应。但是这里的问题本身有点太宽泛了。

标签: java javafx touch scrollpane


【解决方案1】:

我找到了答案,这是 ScrollPane 中的按钮(节点)的代码。 但是您需要恢复按钮事件以获得更好的性能,这不是在下一个代码上开发的。

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();
            }
        });
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多