在javafx中清除场景
我需要JavaFX的帮助。我有一个在场景中用鼠标画线的程序。当我按下清除按钮时,整个场景需要清除。但是这个程序只清除最后一条画的线 按下清除按钮时,应清除所有绘制的线。现在,只清除最后绘制的线在javafx中清除场景,javafx,javafx-8,Javafx,Javafx 8,我需要JavaFX的帮助。我有一个在场景中用鼠标画线的程序。当我按下清除按钮时,整个场景需要清除。但是这个程序只清除最后一条画的线 按下清除按钮时,应清除所有绘制的线。现在,只清除最后绘制的线 public class Test extends Application { private Line currentLine; private Group root; private ColorPicker colorPicker; private Button cl
public class Test extends Application {
private Line currentLine;
private Group root;
private ColorPicker colorPicker;
private Button clearButton;
private HBox buttons;
private Scene scene;
public void start(Stage primaryStage) {
root = new Group();
colorPicker = new ColorPicker(Color.WHITE);
clearButton = new Button("Clear");
clearButton.setOnAction(this::processActionButton);
buttons = new HBox(colorPicker, clearButton);
buttons.setSpacing(15);
root.getChildren().addAll(buttons);
scene = new Scene(root, 500, 300, Color.BLACK);
scene.setOnMousePressed(this::processMousePress);
scene.setOnMouseDragged(this::processMouseDrag);
primaryStage.setTitle("Color Lines");
primaryStage.setScene(scene);
primaryStage.show();
}
public void processMousePress(MouseEvent event) {
currentLine = new Line(event.getX(), event.getY(), event.getX(),
event.getY());
currentLine.setStroke(colorPicker.getValue());
currentLine.setStrokeWidth(3);
root.getChildren().add(currentLine);
}
public void processMouseDrag(MouseEvent event) {
currentLine.setEndX(event.getX());
currentLine.setEndY(event.getY());
}
public void processActionButton(ActionEvent event) {
root.getChildren().removeAll(currentLine);
}
public static void main(String[] args) {
launch(args);
}
}
您只能为行设置一个特殊组:
Group groupLines = new Group();
...
root.getChildren().add(groupLines);
将新行添加到此组中:
public void processMousePress(MouseEvent event) {
...
groupLines.getChildren().add(currentLine);
}
groupLines.getChildren().clear();
并仅清洁此组:
public void processMousePress(MouseEvent event) {
...
groupLines.getChildren().add(currentLine);
}
groupLines.getChildren().clear();
root.getChildren().clear()
将清除包括按钮在内的场景。。。如果这不是期望的结果,请使用集合来存储行…我不希望按钮被清除。我只希望台词消失。你能帮我使用收藏吗?非常感谢!