Javafx 在网格窗格中移动标记
我已经创建了一个包含多行的网格窗格。标记只能在一行上移动。当移动鼠标时,它应该只横向移动,向左或向右。当你点击标记时,它应该被锁定,不再移动。它应该用不同的颜色突出显示。 我怎样才能做到这一点? 如果您知道任何教程或示例,请添加,谢谢 消息输出: 执行 C:\Users\s22380\Desktop\temp\JavaFXApplication9\dist\Run26998800\JavaFXApplication9.jar 使用平台C:\Program Files\Java\jdk1.8.0\u 92\jre/bin/Java 应用程序构造函数中的异常 java.lang.reflect.InvocationTargetException位于 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于 invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 位于java.lang.reflect.Method.invoke(Method.java:498) com.sun.javafx.application.LaunchImpl.launchApplicationWithArgs(LaunchImpl.java:389) 在 com.sun.javafx.application.LaunchImpl.launchApplication(LaunchImpl.java:328) 位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 位于java.lang.reflect.Method.invoke(Method.java:498) sun.launcher.launchelper$FXHelper.main(launchelper.java:767) 原因:java.lang.RuntimeException:无法构造应用程序 实例:位于的类javafxapplication9.javafxapplication9 com.sun.javafx.application.LaunchImpl.launchApplication1(LaunchImpl.java:907) 在 com.sun.javafx.application.launchempl.lambda$launchApplication$155(launchempl.java:182) 在java.lang.Thread.run(Thread.java:745)处,由以下原因引起: java.lang.reflect.InvocationTargetException位于 sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 位于java.lang.reflect.Constructor.newInstance(Constructor.java:423) 在 com.sun.javafx.application.launchempl.lambda$launchApplication1$161(launchempl.java:819) 在 com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 在 com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 位于java.security.AccessController.doPrivileged(本机方法) com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 在 com.sun.glass.ui.invokelateDispatcher$Future.run(invokelateDispatcher.java:95) 在com.sun.glass.ui.win.WinApplication.\u runLoop(本机方法) com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) ... 1由以下原因引起的更多:java.lang.NullPointerException位于 Controller.(Controller.java:33)位于 javafxapplication9.javafxapplication9.(javafxapplication9.java:19) ... 13更多运行应用程序的异常 javafxapplication9.javafxapplication9 Java结果:1正在删除 目录 C:\Users\s22380\Desktop\temp\JavaFXApplication9\dist\Run26998800 jfxsa运行:生成成功(总时间:1秒)Javafx 在网格窗格中移动标记,javafx,grid,Javafx,Grid,我已经创建了一个包含多行的网格窗格。标记只能在一行上移动。当移动鼠标时,它应该只横向移动,向左或向右。当你点击标记时,它应该被锁定,不再移动。它应该用不同的颜色突出显示。 我怎样才能做到这一点? 如果您知道任何教程或示例,请添加,谢谢 消息输出: 执行 C:\Users\s22380\Desktop\temp\JavaFXApplication9\dist\Run26998800\JavaFXApplication9.jar 使用平台C:\Program Files\Java\jdk1.8.0\
这个怎么样: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);
}
}
这基本上就是在IntelliJ中使用新JavaFX项目时的示例代码。我将其更改为显式设置控制器。但这只是个人偏好
这方面的xaml
现在是控制器
它将做一些事情:
- 听我想推的对象的鼠标事件。如果delata足够大,我将移动它=>如果鼠标移动足够快
- 当按下按钮时,我将切换出样式表以获得不同的外观。现在,此样式适用于场景中的所有按钮。它可以更改为只在特定的一个上工作
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; } } }); } } }
包装样品; 导入javafx.event.ActionEvent; 导入javafx.event.EventHandler; 导入javafx.fxml.fxml; 导入javafx.scene.control.Button; 导入javafx.scene.input.MouseEvent; 导入javafx.scene.layout.ColumnConstraints; 导入javafx.scene.layout.GridPane; 导入javafx.scene.layout.RowConstraints; 导入javafx.scene.shape.Rectangle; 公共类控制器 { @FXML 私有网格窗格主网格; @FXML 专用按钮可移动; 私人最终int sizeX; 私人决赛; 私人最终双移动距离=3; 私有布尔值canMove=true; private Double lastX=null; private Double lastY=null; 私有字符串redButtonStyle=Controller.class.getResource(“redStyle.css”).toExternalForm(); 公共控制器(int sizeX、int sizeY) { this.sizeX=sizeX; this.sizeY=sizeY; } public void InitUi() { 如果(this.mainGrid!=null) { 最终int numCols=sizeX; 最终整数numRows=sizeY; 对于(int i=0;i
.button { -fx-text-fill: #006464; -fx-background-color: #DFB951; -fx-border-radius: 20; -fx-background-radius: 20; -fx-padding: 5; }
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; } } }); } } }