2016-07-13 8 views
-1

Ich habe einen Rasterbereich mit einer Anzahl von Zeilen erstellt. Ein Marker sollte nur in einer Reihe beweglich sein. Wenn Sie die Maus bewegen, sollte sie sich nur nach links oder rechts bewegen. Wenn Sie auf den Marker klicken, sollte er gesperrt sein, keine Bewegung mehr. Es sollte mit einer anderen Farbe hervorgehoben werden. Wie kann ich das erreichen? Wenn Sie irgendwelche Tutorials oder Beispiele kennen, fügen Sie bitte hinzu, danke.Verschieben von Markierungen in einem Rasterbereich

Nachrichtenausgabe:

Ausführen C: \ Benutzer \ s22380 \ Desktop \ temp \ JavaFXApplication9 \ dist \ run269988000 \ JavaFXApplication9.jar mit Plattform C: \ Programme \ Java \ jdk1.8.0_92 \ jre/bin/java Exception in Anwendungskonstruktor java.lang.reflect.InvocationTargetException bei sun.reflect.NativeMethodAccessorImpl.invoke0 (native Methode) bei sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) bei sun.reflect.DelegatingMethodAccessorI mpl.invoke (DelegatingMethodAccessorImpl.java:43) bei java.lang.reflect.Method.invoke (Method.java:498) bei com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs (LauncherImpl.java:389) bei com.sun.javafx.application.LauncherImpl.launchApplication (LauncherImpl.java:328) bei sun.reflect.NativeMethodAccessorImpl.invoke0 (native Methode) bei sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) bei sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) bei java.lang.reflect.Method.invoke (Method.java:498) bei sun.launcher.LauncherHelper $ FXHelper.main (LauncherHelper.java:767) verursacht durch : Java.lang.RuntimeException: Kann nicht Anwendung Instanz konstruieren: class javafxapplication9.JavaFXApplication9 bei com.sun.javafx.application.LauncherImpl.launchApplication1 (LauncherImpl.java:907) bei com.sun.javafx.application.LauncherImpl & lgr $ launch $ 155 (LauncherImpl.java:182) bei java.lang.Thread.run (Thread.java:745) verursacht durch: java.lang.reflect.InvocationTargetException bei sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Muttersprache Methode) bei sun.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:62) bei sun.reflect.DelegatingConstructorAccessorImpl.newInstance (DelegatingConstructor AccessorImpl.java:45) bei java.lang.reflect.Constructor.newInstance (Constructor.java:423) bei com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 161 (LauncherImpl.java:819) bei com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 175 (PlatformImpl.java:326) bei com.sun.javafx.application.PlatformImpl.lambda $ null $ 173 (PlatformImpl.java:295) auf Java. security.AccessController.doPrivileged (native Methode) bei com.sun.javafx.application.PlatformImpl.lambda $ runLater 174 $ (PlatformImpl.java:294) bei com.sun.glass.ui.InvokeLaterDispatcher $ Future.run (InvokeLaterDispatcher .java: 95) bei com.sun.glass.ui.wi n.WinApplication._runLoop (Native Methode) um com.sun.glass.ui.win.WinApplication.lambda $ null $ 148 (WinApplication.java:191) ... 1 mehr verursacht durch: java.lang.NullPointerException um sample.Controller. (Controller.java:33) um javafxapplication9.JavaFXApplication9. (JavaFXApplication9.java:19) ...13 weitere Ausnahme laufende Anwendung javafxapplication9.JavaFXApplication9 Java Ergebnis: 1 Löschen Verzeichnis C: \ Benutzer \ s22380 \ Desktop \ temp \ JavaFXApplication9 \ dist \ run269988000 jfxsa-run: BUILD SUCCESSFUL (Gesamtzeit: 1 Sekunde)

Antwort

0

Wie wäre es damit: Main.java

package sample; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class Main extends Application 
{ 
    Controller controller = new Controller(10,10); 

    @Override 
    public void start(Stage primaryStage) throws Exception{ 
     FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml")); 
     loader.setController(this.controller); 
     Parent root = (Parent)loader.load(); 
     this.controller.InitUi(); 
     primaryStage.setTitle("Hello World"); 
     primaryStage.setScene(new Scene(root, 300, 275)); 
     primaryStage.show(); 
    } 

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

das Codebeispiel Dies ist im Grunde, wenn Sie in IntelliJ neuen JavaFX Projekt sagen. Ich habe es geändert, um den Controller explizit zu setzen. Aber das ist nur persönliche Vorliebe.

XAML für das

<?import javafx.scene.layout.*?> 

<GridPane fx:id="mainGrid" alignment="center" gridLinesVisible="true" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> 
    <children> 
    </children> 
</GridPane> 

wieder. Im Grunde das Standard-Zeug.

Ich fügte auch ein Stylesheet hinzu, um die unterschiedliche Sperre für den nicht beweglichen Zustand zu definieren. redStyle.css

.button { 
    -fx-text-fill: #006464; 
    -fx-background-color: #DFB951; 
    -fx-border-radius: 20; 
    -fx-background-radius: 20; 
    -fx-padding: 5; 
} 

Jetzt für den Controller.

Es wird ein paar Dinge tun:

  • Hören Sie die Mausereignisse des Objekts ich um schieben wollen. Wenn das Delata groß genug ist, verschiebe ich es => wenn die Maus schnell genug bewegt wird
  • Wenn die Taste gedrückt wird, werde ich das Stylesheet ausschalten, um das andere Aussehen zu bekommen. Momentan funktioniert dieser Style auf allen Buttons in der Szene. Es könnte geändert werden, um nur an einem bestimmten zu arbeiten.

    package sample; 
    
    
    import javafx.event.ActionEvent; 
    import javafx.event.EventHandler; 
    import javafx.fxml.FXML; 
    
    import javafx.scene.control.Button; 
    import javafx.scene.input.MouseEvent; 
    import javafx.scene.layout.ColumnConstraints; 
    import javafx.scene.layout.GridPane; 
    import javafx.scene.layout.RowConstraints; 
    import javafx.scene.shape.Rectangle; 
    
    public class Controller 
    { 
        @FXML 
        private GridPane mainGrid; 
    
        @FXML 
        private Button movable; 
    
        private final int sizeX; 
        private final int sizeY; 
    
        private final double minMoveDistanc = 3; 
    
        private boolean canMove = true; 
    
        private Double lastX = null; 
        private Double lastY = null; 
    
        private String redButtonStyle = Controller.class.getResource("redStyle.css").toExternalForm(); 
    
        public Controller(int sizeX, int sizeY) 
        { 
         this.sizeX = sizeX; 
         this.sizeY = sizeY; 
        } 
    
        public void InitUi() 
        { 
         if (this.mainGrid != null) 
         { 
          final int numCols = sizeX; 
          final int numRows = sizeY; 
          for (int i = 0; i < numCols; i++) 
          { 
           ColumnConstraints colConst = new ColumnConstraints(); 
           this.mainGrid.getColumnConstraints().add(colConst); 
          } 
          for (int i = 0; i < numRows; i++) 
          { 
           RowConstraints rowConst = new RowConstraints(); 
           this.mainGrid.getRowConstraints().add(rowConst); 
          } 
    
          // add rectangle to keep grid in size 
          for (int i = 0; i < numCols; i++) 
          { 
           for (int j = 0; j < numRows; j++) 
           { 
            Rectangle rect = new Rectangle(); 
            rect.setWidth(50); 
            rect.setHeight(50); 
            this.mainGrid.add(rect,i,j); 
           } 
          } 
    
          // ad movable object (Button) 
          this.movable = new Button("Hallo"); 
          this.movable.setPrefWidth(50); 
          this.movable.setPrefHeight(50); 
          this.movable.setOnAction(new EventHandler<ActionEvent>() 
          { 
           @Override 
           public void handle(ActionEvent actionEvent) 
           { 
            canMove = ! canMove; 
            movable.setText(canMove? "move" : "stop"); 
            if (canMove) 
            { 
             movable.getScene().getStylesheets().remove(redButtonStyle); 
            } 
            else 
            { 
             movable.getScene().getStylesheets().add(redButtonStyle); 
            } 
           } 
          }); 
          this.mainGrid.add(this.movable,5,5); 
    
         } 
    
         if (this.movable != null) 
         { 
          this.movable.setOnMouseEntered(new EventHandler<MouseEvent>() 
          { 
           @Override 
           public void handle(MouseEvent mouseEvent) 
           { 
            lastX = null; 
            lastY = null; 
           } 
          }); 
    
          this.movable.setOnMouseExited(new EventHandler<MouseEvent>() 
          { 
           @Override 
           public void handle(MouseEvent mouseEvent) 
           { 
            lastX = null; 
            lastY = null; 
           } 
          }); 
    
          this.movable.setOnMouseMoved(new EventHandler<MouseEvent>() 
          { 
           @Override 
           public void handle(MouseEvent mouseEvent) 
           { 
            if (!canMove) 
            { return; } 
    
            double x = mouseEvent.getSceneX(); 
            double y = mouseEvent.getSceneY(); 
    
            if (lastX == null) 
            { 
             lastX = x; 
             lastY = y; 
             return; 
            } 
    
            // calculate delta 
            double deltaX = x - lastX; 
            double deltaY = y - lastY; 
    
            // remember new position 
            lastX = x; 
            lastY = y; 
    
            boolean moved = false; 
    
            // x movement 
            if (Math.abs(deltaX) > minMoveDistanc) 
            { 
             moved = true; 
             int currentColumn = GridPane.getColumnIndex(movable); 
    
             if (deltaX < 0) 
             { 
              GridPane.setColumnIndex(movable, Math.max(currentColumn -1 ,0)); 
             } 
             else 
             { 
              GridPane.setColumnIndex(movable, Math.min(currentColumn + 1 ,sizeX-1)); 
             } 
            } 
    
            // y movement 
            if (Math.abs(deltaY) > minMoveDistanc) 
            { 
             moved = true; 
             int currentRow = GridPane.getRowIndex(movable); 
    
             if (deltaY < 0) 
             { 
              GridPane.setRowIndex(movable, Math.max(currentRow - 1 ,0)); 
             } 
             else 
             { 
              GridPane.setRowIndex(movable, Math.min(currentRow + 1 ,sizeY-1)); 
             } 
            } 
    
            if (moved) 
            { 
             lastX = null; 
             lastY = null; 
            } 
           } 
          }); 
         } 
        } 
    } 
    
+0

Vielen Dank für Ihre Mühe und perfekt präsentiert Beispiel. Ich habe es versucht, aber beim Laufen eine Ausnahme bekommen. Nicht genau, warum. Ich habe die CSS-Datei nicht enthalten. Wo soll ich es hinstellen? – user2909180

+0

Setzen Sie I einfach neben die anderen Dateien 'String redButtonStyle = Controller.class.getResource (" redStyle.css "). ToExternalForm();' versucht, es aus dem gleichen Ordner zu laden. Außerdem: Was ist Ihre Ausnahme – FrankT

+0

'verursacht durch: java.lang.NullPointerException bei sample.Controller. (Controller.java:33) at' Controller Zeile 33 ist das Laden des Stils aus der Datei. Es kann wahrscheinlich nicht finden. Versuchen Sie, das Style-Zeug zu entfernen, und überprüfen Sie, ob der Rest funktioniert – FrankT

Verwandte Themen