2016-04-24 76 views
12

我覺得我已經搜索了一半的網絡,發現沒有解決方案...
我有一個Java應用程序顯示一個地圖(不同的國家等)。
目前,您可以使用鼠標滾輪向下滾動...
我希望如此,您可以橫向滾動(水平)。
我只需要一個監聽器(在Swing或Javafx中無所謂),只要鼠標滾輪傾斜,而不需要地圖焦點(用鼠標懸停應該足夠,窗戶仍應該聚焦),而不需要任何可見的滾動條。水平滾動監聽器

+1

怎麼樣''node.setOnScroll(new EventHandler()...''?它給出Y和X座標 –

+0

它不會觸發橫向滾動... – RoiEX

+0

有一次類似的問題。 http://stackoverflow.com/questions/12911506/why-jscrollpane-does-not-react-to-mouse-wheel-events JScrollPane沒有反應鼠標滾輪滾動 –

回答

2

使用下面的代碼每次側身滾動郵件時被打印出來......

import javafx.application.Application; 
import javafx.event.Event; 
import javafx.event.EventHandler; 
import javafx.event.EventType; 
import javafx.scene.Scene; 
import javafx.scene.input.ScrollEvent; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 


public class Main extends Application { 
    @Override 
    public void start(Stage primaryStage) { 
     try { 
      BorderPane root = new BorderPane(); 
      Scene scene = new Scene(root,400,400); 
      scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 
      primaryStage.setScene(scene); 
      scene.setOnScroll(new EventHandler<ScrollEvent>(){ 

       @Override 
       public void handle(ScrollEvent event) { 
        System.out.println("Scroll:" + event.getDeltaX()); 
       } 
      }); 
      primaryStage.show(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

有一點要考慮: 顯然嵌入到JFXPanel一個JFrame是沒有得到通過側向滾動事件時。