我正在申请一份申请表。示例代码如下所示
public class Main extends Application
{
private BorderPane border;
@Override
public void start(Stage primaryStage)
{
//Displaying all the functions in Scene
border = new BorderPane();
Scene scene = new Scene(border,750,500);
primaryStage.setTitle("BorderPane");
primaryStage.setScene(scene);
primaryStage.show();
//Fall-Tree Root Item
TreeItem<String> tree = new TreeItem<String>("Library");
TreeItem<String> item1 = new TreeItem<String>("Module");
TreeItem<String> item1Child = new TreeItem<String>("MX");
item1.getChildren().add(item1Child);
TreeItem<String> item2 = new TreeItem<String>("Unite");
TreeItem<String> item2Child1 = new TreeItem<String>("UX");
TreeItem<String> item2Child2 = new TreeItem<String>("UY");
item2.getChildren().addAll(item2Child1,item2Child2);
item2.setExpanded(true);
TreeItem<String> item3 = new TreeItem<String>("Translate");
TreeItem<String> item3Child = new TreeItem<String>("TX");
item3.getChildren().add(item3Child);
TreeItem<String> item4 = new TreeItem<String>("Rotate");
TreeItem<String> item4Child = new TreeItem<String>("Rx");
item4.getChildren().add(item4Child);
tree.setExpanded(true);
tree.getChildren().addAll(item1,item2,item3,item4);
TreeView<String> treeView = new TreeView<String>(tree);
//Making Tree Editable
treeView.setEditable(true);
treeView.setCellFactory(TextFieldTreeCell.forTreeView());
//Assigning Leaf an Explorer View
VBox box1 = new VBox();
treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Object>() {
@Override
public void changed(ObservableValue<?> observable, Object oldValue, Object newValue) {
if (newValue == item2Child1) {
box1.getChildren().add(getrightPane1());
} else {
int i = box1.getChildren().size();
if (i > 0) {
box1.getChildren().remove(0);
}
}
}
});
// Main Branch
VBox box2 = new VBox();
treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Object>() {
@Override
public void changed(ObservableValue<?> observable, Object oldValue, Object newValue) {
if (newValue == item2) {
box2.getChildren().add(getrightPane2());
} else {
int i = box2.getChildren().size();
if (i > 0) {
box2.getChildren().remove(0);
}
}
}
});
//Displaying all the Functions into border's center
VBox vbox =new VBox(2);
vbox.setPadding(new Insets(5));
VBox.setVgrow(treeView, Priority.ALWAYS);
vbox.getChildren().addAll(new Text("Fall Tree"),treeView);
HBox hb = new HBox();
hb.getChildren().addAll(vbox,box1,box2);
border.setCenter(hb);
}
//Method for 1st Leaf Explorer View
private BorderPane getrightPane1() {
BorderPane root = new BorderPane();
VBox vbox = new VBox(20);
vbox.setPadding(new Insets(10));
HBox h1 = new HBox(7);
HBox h2 = new HBox(7);
TextField textf1 = new TextField();
TextField textf2 = new TextField();
textf1.setPrefWidth(100);
textf1.setPromptText("Enter Height");
//Generating Rectangle's Height by providing Values in Output
textf1.setOnKeyReleased(new EventHandler<KeyEvent>(){
@Override
public void handle(KeyEvent event)
{
if(textf1.getText().length() > 0 && textf2.getText().length() > 0)
{
Rectangle rect = new Rectangle();
rect.setHeight(Double.parseDouble(textf1.getText()));
rect.setWidth(Double.parseDouble(textf2.getText()));
rect.setFill(null);
rect.setStroke(Color.RED);
root.setBottom(rect);
}
}
});
textf2.setPrefWidth(100);
textf2.setPromptText("Enter Width");
//Generating Rectangle's Width by providing Values in Output
textf2.setOnKeyReleased(new EventHandler<KeyEvent>(){
@Override
public void handle(KeyEvent event)
{
if(textf1.getText().length() > 0 && textf2.getText().length() > 0)
{
Rectangle rect = new Rectangle();
rect.setHeight(Double.parseDouble(textf1.getText()));
rect.setWidth(Double.parseDouble(textf2.getText()));
rect.setFill(null);
rect.setStroke(Color.RED);
root.setBottom(rect);
}
}
});
//Labels for TextFields
h1.getChildren().addAll(new Label("Y:"), textf1);
h2.getChildren().addAll(new Label("X:"), textf2);
vbox.getChildren().addAll(h1, h2);
root.setLeft(vbox);
return root;
}
//Method for Branch Explorer View
private HBox getrightPane2() {
HBox hbox = new HBox(20);
hbox.setAlignment(Pos.CENTER);
hbox.getChildren().addAll(getrightPane1());
return hbox;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
这是示例代码的工作原理
&#34;如果用户将值放在&#34; UX&#34;中提供的字段中UX中生成矩形。 但是当用户点击任何其他分支时,这个矩形消失了。&#34;
应用程序应该像这样工作
&#34;如果用户将值放入UX中的字段,则在 UX 和 Unite 分支中创建矩形。&#34;
请查看方法getrightPane2,由于获取getrightPane1的所有子项,字段显示在Unite中,并且该用户可以创建矩形,但这不正确。只有在UX中创建的Rectangle才能显示在Unite中。请任何提示都没问题。
谢谢
答案 0 :(得分:0)
试试此代码
public class Main extends Application{
private BorderPane border;
@Override
public void start(Stage primaryStage) {
// Displaying all the functions in Scene
border = new BorderPane();
Scene scene = new Scene(border, 750, 500);
primaryStage.setTitle("BorderPane");
primaryStage.setScene(scene);
primaryStage.show();
// Fall-Tree Root Item
TreeItem<String> tree = new TreeItem<String>("Library");
TreeItem<String> item1 = new TreeItem<String>("Module");
TreeItem<String> item1Child = new TreeItem<String>("MX");
item1.getChildren().add(item1Child);
TreeItem<String> item2 = new TreeItem<String>("Unite");
TreeItem<String> item2Child1 = new TreeItem<String>("UX");
System.out.println();
TreeItem<String> item2Child2 = new TreeItem<String>("UY");
item2.getChildren().addAll(item2Child1, item2Child2);
item2.setExpanded(true);
TreeItem<String> item3 = new TreeItem<String>("Translate");
TreeItem<String> item3Child = new TreeItem<String>("TX");
item3.getChildren().add(item3Child);
TreeItem<String> item4 = new TreeItem<String>("Rotate");
TreeItem<String> item4Child = new TreeItem<String>("Rx");
item4.getChildren().add(item4Child);
tree.setExpanded(true);
tree.getChildren().addAll(item1, item2, item3, item4);
TreeView<String> treeView = new TreeView<String>(tree);
// Making Tree Editable
treeView.setEditable(true);
treeView.setCellFactory(TextFieldTreeCell.forTreeView());
// Assigning Leaf an Explorer View
VBox box1 = new VBox();
treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem>() {
@Override
public void changed(ObservableValue<? extends TreeItem> observable, TreeItem oldValue, TreeItem newValue) {
if (newValue.getValue().equals(item2Child1.getValue())) {
box1.getChildren().add(getrightPane1());
} else {
int i = box1.getChildren().size();
if (i > 0) {
box1.getChildren().remove(0);
}
}
}
});
// Main Branch
VBox box2 = new VBox();
treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem>() {
@Override
public void changed(ObservableValue<? extends TreeItem> observable, TreeItem oldValue, TreeItem newValue) {
if (newValue.getValue().equals(item2.getValue())) {
box2.getChildren().add(getrightPane2());
} else {
int i = box2.getChildren().size();
if (i > 0) {
box2.getChildren().remove(0);
}
}
}
});
// Displaying all the Functions into border's center
VBox vbox = new VBox(2);
vbox.setPadding(new Insets(5));
VBox.setVgrow(treeView, Priority.ALWAYS);
vbox.getChildren().addAll(new Text("Fall Tree"), treeView);
HBox hb = new HBox();
hb.getChildren().addAll(vbox, box1, box2);
border.setCenter(hb);
}
TextField textf1 = new TextField();
TextField textf2 = new TextField();
BorderPane root = new BorderPane();
// Method for 1st Leaf Explorer View
private BorderPane getrightPane1() {
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");
// Generating Rectangle's Height by providing Values in Output
textf1.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (textf1.getText().length() > 0 && textf2.getText().length() > 0) {
Rectangle rect = new Rectangle();
rect.setHeight(Double.parseDouble(textf1.getText()));
rect.setWidth(Double.parseDouble(textf2.getText()));
rect.setFill(null);
rect.setStroke(Color.RED);
root.setBottom(rect);
}
}
});
textf2.setPrefWidth(100);
textf2.setPromptText("Enter Width");
// Generating Rectangle's Width by providing Values in Output
textf2.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (textf1.getText().length() > 0 && textf2.getText().length() > 0) {
Rectangle rect = new Rectangle();
rect.setHeight(Double.parseDouble(textf1.getText()));
rect.setWidth(Double.parseDouble(textf2.getText()));
rect.setFill(null);
rect.setStroke(Color.RED);
root.setBottom(rect);
}
}
});
// Labels for TextFields
h1.getChildren().addAll(new Label("Y:"), textf1);
h2.getChildren().addAll(new Label("X:"), textf2);
vbox.getChildren().addAll(h1, h2);
root.setLeft(vbox);
return root;
}
// Method for Branch Explorer View
private HBox getrightPane2() {
HBox hbox = new HBox(20);
hbox.setAlignment(Pos.CENTER);
hbox.setPadding(new Insets(20));
System.out.println(textf1.getText());
if (!textf1.getText().equals("") && !textf2.getText().equals("")) {
Rectangle rectangle = new Rectangle();
rectangle.setHeight(Double.parseDouble(textf1.getText()));
rectangle.setWidth(Double.parseDouble(textf2.getText()));
rectangle.setFill(null);
rectangle.setStroke(Color.RED);
hbox.getChildren().addAll(rectangle);
} else {
Alert alert = new Alert(AlertType.INFORMATION, "First fill the UX Field");
alert.show();
}
return hbox;
}
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}