我问了这个问题,但没有得到任何回应。 Javafx: Check box Listener example我需要帮助解决我的这个问题。代码是
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import java.util.HashMap;
import java.util.Map;
public class Main extends Application {
private static int rootNr = 0;
private static int coordinateNr = 0;
public static void main(String[] args) {
launch(args);
}
static final Map<TreeItem<String>, BorderPane> map = new HashMap<>();
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
TreeItem<String> tree = new TreeItem<>("Main System");
TreeItem<String> item1 = new TreeItem<>("Roots");
TreeView<String> treeView = new TreeView<>(tree);
treeView.setOnMouseClicked((event) -> {
TreeItem<String> treeItem = treeView.getSelectionModel().getSelectedItem();
if (treeItem.getChildren().stream().anyMatch(child -> child.getValue().startsWith("C"))) {
root.setCenter(getRootsPanel(treeItem.getValue()));
}else {
root.setCenter(map.get(treeItem));
}
});
treeView.setCellFactory(p -> new AddMenuTreeCell());
tree.setExpanded(true);
root.setLeft(treeView);
tree.getChildren().add(item1);
Scene scene = new Scene(root, 700, 500);
primaryStage.setTitle("Tree View");
primaryStage.setScene(scene);
primaryStage.show();
}
private static class AddMenuTreeCell extends TextFieldTreeCell<String> {
private ContextMenu menu = new ContextMenu();
private TextField textField;
public AddMenuTreeCell() {
MenuItem newitem1 = new MenuItem("Insert Roots");
MenuItem newitem2 = new MenuItem("Insert Coordinates");
menu.getItems().addAll(newitem1, newitem2);
newitem1.setOnAction(arg0 -> {
TreeItem<String> item3 = new TreeItem<>("Root" + rootNr++);
getTreeItem().getChildren().add(item3);
});
newitem2.setOnAction(arg0 -> {
TreeItem<String> newLeaf = new TreeItem<>("Coordinates" + coordinateNr++);
TreeItem<String> uxItem1 = new TreeItem<>("X");
map.put(uxItem1, getrightPane1());
TreeItem<String> uyItem1 = new TreeItem<>("y");
map.put(uyItem1, getrightPane1());
newLeaf.getChildren().add(uxItem1);
newLeaf.getChildren().add(uyItem1);
getTreeItem().getChildren().add(newLeaf);
});
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if (!isEditing()) {
setText(item);
setGraphic(getTreeItem().getGraphic());
if (!(getTreeItem().isLeaf() && getTreeItem().getParent() == null)) {
setContextMenu(menu);
}
}
}
}
}
private static BorderPane getrightPane1() {
TextField textf1 = new TextField();
TextField textf2 = new TextField();
BorderPane root1 = new BorderPane();
VBox vbox = new VBox(20);
vbox.setPadding(new Insets(10));
HBox h1 = new HBox(7);
HBox h2 = new HBox(7);
textf1.setPrefWidth(100);
textf1.setPromptText("Enter Height");
textf1.setOnKeyReleased(event -> {
if (textf1.getText().length() > 0 && textf2.getText().length() > 0) {
Rectangle rect1 = new Rectangle();
rect1.setHeight(Double.parseDouble(textf1.getText()));
rect1.setWidth(Double.parseDouble(textf2.getText()));
rect1.setFill(null);
rect1.setStroke(Color.BLUE);
root1.setCenter(rect1);
}
});
textf2.setPrefWidth(100);
textf2.setPromptText("Enter Width");
textf2.setOnKeyReleased(event -> {
if (textf1.getText().length() > 0 && textf2.getText().length() > 0) {
Rectangle rect2 = new Rectangle();
rect2.setHeight(Double.parseDouble(textf1.getText()));
rect2.setWidth(Double.parseDouble(textf2.getText()));
rect2.setFill(null);
rect2.setStroke(Color.RED);
root1.setCenter(rect2);
}
});
if (textf1.getText().length() > 0 && textf2.getText().length() > 0 && root1.getCenter() == null) {
Rectangle rect = new Rectangle();
rect.setHeight(Double.parseDouble(textf1.getText()));
rect.setWidth(Double.parseDouble(textf2.getText()));
rect.setFill(null);
rect.setStroke(Color.RED);
root1.setCenter(rect);
}
h1.getChildren().addAll(new Label("Y1:"), textf1);
h2.getChildren().addAll(new Label("X1:"), textf2);
vbox.getChildren().addAll(h1, h2);
root1.setLeft(vbox);
return root1;
}
private static BorderPane getRootsPanel(String root) {
BorderPane root2 = new BorderPane();
HBox hbox = new HBox(10);
hbox.setPadding(new Insets(40));
hbox.setAlignment(Pos.TOP_CENTER);
CheckBox box1 = new CheckBox("Change order");
CheckBox box2 = new CheckBox("Change View");
box1.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (newValue) {
// rect1.setStroke(Color.RED);
// rect2.setStroke(Color.BLUE);
} else {
// rect1.setStroke(Color.BLUE);
// rect2.setStroke(Color.RED);
}
});
box2.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (newValue) {
} else {
}
});
VBox vbox = new VBox(30, box1,box2);
for (Map.Entry<TreeItem<String>, BorderPane> entry : map.entrySet()) {
if (entry.getKey().getParent().getParent().getValue().equals(root)) {
Rectangle rect1 = (Rectangle) entry.getValue().getCenter();
if (rect1 != null) {
Rectangle rect2 = new Rectangle();
rect2.setWidth(rect1.getWidth());
rect2.setHeight(rect1.getHeight());
rect2.setFill(rect1.getFill());
rect2.setStroke(rect1.getStroke());
Platform.runLater(() -> hbox.getChildren().addAll(rect2));
}
}
}
hbox.getChildren().add(vbox);
Platform.runLater(() -> root2.setLeft(hbox));
return root2;
}
}
TreeView,用户可以在其中添加根和坐标。坐标的矩形在根中水平显示。
用户首先需要复选框以更改矩形顺序(例如1 2 3到3 2 1)&amp;第二个改变视图(水平到垂直)。如果只有2个叶子,这可以使用注释中给出的代码完成(我没有在评论中放置第二个盒子代码,因为我只能使用2个叶子)。
这些复选框应该对所有生成的矩形产生影响,而不仅仅是2。
这将是一个很大的帮助。谢谢