2016-02-07 17 views
1

我在使用JxBrowser引擎的Java中編寫一個簡單的應用程序,但我一開始就卡住了。在我的代碼中,有一個未修飾的stage,我想讓它可拖動。要做到這一點,搜查,發現了以下鏈接:上stackPanesetOnMouseDragged在瀏覽器視圖上不工作

How to drag undecorated window

所以我設置mousePressedMouseDragged事件,但只有mousePressed事件被解僱,mouseDragged事件沒有辦法被炒魷魚。任何想法是什麼問題?

在此先感謝。

import com.teamdev.jxbrowser.chromium.Browser; 
import com.teamdev.jxbrowser.chromium.javafx.BrowserView; 

import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.event.EventHandler; 
import javafx.scene.Scene; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 

private static double xOffset = 0; 
private static double yOffset = 0; 

public class Main extends Application { 

    public static void main(String[] args) { 

     launch(args); 

    } 

    @Override 
    public void start(Stage primaryStage) { 

     Platform.setImplicitExit(false); 

     Browser browser = new Browser(); 

     BrowserView browserView = new BrowserView(browser); 

     StackPane pane = new StackPane(); 

     pane.getChildren().add(browserView); 

     Scene scene = new Scene(pane, 380, 500); 

     primaryStage.initStyle(StageStyle.UNDECORATED); 

     primaryStage.setScene(scene); 

     pane.setOnMousePressed(new EventHandler<MouseEvent>() { 

      @Override 
      public void handle(MouseEvent event) { 

       System.out.println("mouse pressed"); 

       xOffset = primaryStage.getX() - event.getScreenX(); 

       yOffset = primaryStage.getY() - event.getScreenY(); 

      } 

     }); 

     pane.setOnMouseDragged(new EventHandler<MouseEvent>() { 

      @Override 
      public void handle(MouseEvent event) { 

       System.out.println("mouse dragged"); 

       primaryStage.setX(event.getScreenX() + xOffset); 

       primaryStage.setY(event.getScreenY() + yOffset); 

      } 

     }); 

     primaryStage.show(); 

    } 

} 
+0

只是一個猜測:也許你應該在附加事件偵聽器之前調用'primaryStage.show();'。 – Benvorth

+0

不幸的是沒有幫助:( –

+0

因爲jxbrowser需要一個許可證我不能在本地進行測試。我的下一個猜測是將事件監聽器添加到窗格或瀏覽器本身 – Benvorth

回答

2

由於jxbrowser需要許可證我無法測試它。所以我更換了與標籤和它工作得很好。所以我的猜測是,你正在試圖通過點擊瀏覽器本身,而不是拖StackPane ..嘗試單擊舞臺的角落,或者將堆疊窗格添加到VBox和setPadding中。然後嘗試單擊角落..如果您單擊瀏覽器,然後瀏覽器的鼠標事件將被激怒。

證明: enter image description here

正確的代碼

package RezRem; 

import com.teamdev.jxbrowser.chromium.Browser; 
import com.teamdev.jxbrowser.chromium.javafx.BrowserView; 

import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.event.EventHandler; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.StackPane; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 


public class Main extends Application { 

private static double xOffset = 0; 
private static double yOffset = 0; 
public static void main(String[] args) { 

    launch(args); 

} 

@Override 
public void start(Stage primaryStage) { 

    Platform.setImplicitExit(false); 


    Browser browser = new Browser(); 

    BrowserView browserView = new BrowserView(browser); 
    StackPane pane = new StackPane(); 

    pane.getChildren().add(browserView); 
    pane.setPadding(new Insets(10,10,10,10)); 
    Scene scene = new Scene(pane, 380, 500); 

    primaryStage.initStyle(StageStyle.UNDECORATED); 

    primaryStage.setScene(scene); 

    pane.setOnMousePressed(new EventHandler<MouseEvent>() { 

     @Override 
     public void handle(MouseEvent event) { 

      System.out.println("mouse pressed"); 

      xOffset = primaryStage.getX() - event.getScreenX(); 

      yOffset = primaryStage.getY() - event.getScreenY(); 

     } 

    }); 

    pane.setOnMouseDragged(new EventHandler<MouseEvent>() { 

     @Override 
     public void handle(MouseEvent event) { 

      System.out.println("mouse dragged"); 

      primaryStage.setX(event.getScreenX() + xOffset); 

      primaryStage.setY(event.getScreenY() + yOffset); 

     } 

    }); 
    browser.loadURL("http://www.google.com"); 
    primaryStage.show(); 

} 

} 

原因

的jxbrowser延伸至邊緣,以便堆棧窗格沒有在上面也不是它的側面可見,所以鼠標聽衆從來沒有得到通過設置堆棧的填充觸發窗格的四邊都有10px的間隙,如果點擊,觸發鼠標事件並從而解決問題。

+0

「嘗試點擊舞臺的角落」沒有幫助,我創建了一個VBox,'vb'的實例,並在其上設置了'padding',然後添加了'pane ''''vb'並創建'scene'這種方式:'Scene scene = new Scene(vb,300,500);'。同時增加拖動和鼠標按下'vb'的監聽器。最後,監聽器正在工作正確!但是瀏覽器甚至沒有顯示任何東西,甚至當我加載一個html時。(加載html沒有VBox工作正常)。此外,如果你認爲它可以提供幫助,我可以給你授權。我的許可證是免費的。項目。謝謝你的嘗試。 –

+0

等待十分鐘左右..我會的嘗試一種解決方法..所以你不必使用vbox .. –

+0

你是如何加載網頁? –

相關問題