能否请您告诉我单击添加按钮时如何添加ChoiceBox,TextField和Label,以及单击删除按钮时如何将其删除。在ScrollPane下,在GridPane下,我添加了Add按钮。当我单击该按钮时,ChoiceBox,TextField和Label应该添加到现有字段的下方。单击“删除”按钮后,字段应删除。Refer this image for clarification
请参考图片进行澄清。
sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<GridPane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Label layoutX="234.0" layoutY="14.0" prefHeight="27.0" prefWidth="133.0" text="Bismillah Fruits" textFill="#f20404" GridPane.columnIndex="1">
<font>
<Font name="System Bold" size="18.0" />
</font>
<GridPane.margin>
<Insets bottom="100.0" left="50.0" />
</GridPane.margin>
</Label>
<Label layoutX="56.0" layoutY="72.0" text="Name:">
<font>
<Font name="System Italic" size="14.0" />
</font>
<GridPane.margin>
<Insets left="15.0" />
</GridPane.margin>
</Label>
<TextField fx:id="name" layoutX="109.0" layoutY="69.0" promptText="Please Enter Name">
<GridPane.margin>
<Insets left="60.0" />
</GridPane.margin></TextField>
<DatePicker fx:id="date" layoutX="411.0" layoutY="69.0" promptText="Select Date" GridPane.columnIndex="2" />
<ScrollPane prefHeight="200.0" prefWidth="200.0" GridPane.columnSpan="2" GridPane.rowIndex="1">
<content>
<GridPane fx:id="gridPane" prefHeight="142.0" prefWidth="406.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ChoiceBox fx:id="choiceBox1" prefWidth="150.0">
<GridPane.margin>
<Insets bottom="10.0" left="50.0" right="55.0" />
</GridPane.margin>
</ChoiceBox>
<TextField fx:id="kgs1" promptText="value" GridPane.columnSpan="2">
<GridPane.margin>
<Insets bottom="10.0" left="175.0" right="130.0" />
</GridPane.margin>
</TextField>
<Label text="x">
<GridPane.margin>
<Insets bottom="5.0" left="292.0" />
</GridPane.margin>
</Label>
<TextField fx:id="mul1" promptText="mul" GridPane.columnIndex="1">
<GridPane.margin>
<Insets bottom="5.0" left="95.0" right="40.0" />
</GridPane.margin>
</TextField>
<Button fx:id="add" mnemonicParsing="false" text="Add">
<GridPane.margin>
<Insets bottom="10.0" left="5.0" />
</GridPane.margin>
</Button>
</children>
</GridPane>
</content>
</ScrollPane>
<Label layoutX="485.0" layoutY="122.0" text="Total" GridPane.columnIndex="2">
<GridPane.margin>
<Insets left="60.0" top="150.0" />
</GridPane.margin></Label>
<Label layoutX="155.0" layoutY="122.0" text="Details" GridPane.columnIndex="1">
<font>
<Font name="System Italic" size="12.0" />
</font>
<GridPane.margin>
<Insets left="75.0" top="150.0" />
</GridPane.margin>
</Label>
<Label fx:id="value1" alignment="CENTER" contentDisplay="CENTER" layoutX="495.0" layoutY="157.0" text="0" GridPane.columnIndex="2" GridPane.rowIndex="1">
<GridPane.margin>
<Insets bottom="100.0" left="60.0" />
</GridPane.margin></Label>
<Label layoutX="43.0" layoutY="335.0" text="Advance:" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="80.0" left="90.0" />
</GridPane.margin></Label>
<Label layoutX="43.0" layoutY="368.0" text="Coolie:" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="20.0" left="90.0" />
</GridPane.margin></Label>
<Label layoutX="43.0" layoutY="403.0" text="Market:" GridPane.rowIndex="2">
<GridPane.margin>
<Insets left="90.0" top="40.0" />
</GridPane.margin></Label>
<Label layoutX="42.0" layoutY="303.0" text="Commission:" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="130.0" left="90.0" />
</GridPane.margin></Label>
<RadioButton fx:id="eight" layoutX="167.0" layoutY="303.0" mnemonicParsing="false" text="8%" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="130.0" />
</GridPane.margin></RadioButton>
<Label layoutX="211.0" layoutY="303.0" text="or" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="130.0" left="43.0" />
</GridPane.margin></Label>
<RadioButton fx:id="ten" layoutX="228.0" layoutY="303.0" mnemonicParsing="false" text="10%" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="130.0" left="60.0" />
</GridPane.margin></RadioButton>
<Label fx:id="value4" alignment="CENTER" contentDisplay="CENTER" layoutX="331.0" layoutY="303.0" text="Commission" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="130.0" left="150.0" />
</GridPane.margin></Label>
<TextField fx:id="advance" layoutX="160.0" layoutY="331.0" promptText="Advance" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="80.0" right="150.0" />
</GridPane.margin></TextField>
<TextField fx:id="coolie" layoutX="160.0" layoutY="364.0" prefHeight="25.0" prefWidth="64.0" promptText="Coolie" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="20.0" right="170.0" />
</GridPane.margin></TextField>
<Label layoutX="230.0" layoutY="368.0" text="x" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="20.0" left="80.0" />
</GridPane.margin></Label>
<RadioButton fx:id="three" layoutX="240.0" layoutY="368.0" mnemonicParsing="false" text="3" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="20.0" left="90.0" />
</GridPane.margin></RadioButton>
<Label layoutX="270.0" layoutY="368.0" text="or" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="20.0" left="120.0" />
</GridPane.margin></Label>
<RadioButton fx:id="five" layoutX="285.0" layoutY="368.0" mnemonicParsing="false" text="5" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="20.0" left="140.0" />
</GridPane.margin></RadioButton>
<Label fx:id="value5" alignment="CENTER" contentDisplay="CENTER" layoutX="360.0" layoutY="368.0" text="Coolie" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="20.0" left="185.0" />
</GridPane.margin></Label>
<TextField fx:id="market" layoutX="160.0" layoutY="399.0" promptText="Market charge" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets right="150.0" top="40.0" />
</GridPane.margin></TextField>
<Label fx:id="total" alignment="CENTER" contentDisplay="CENTER" layoutX="485.0" layoutY="423.0" text="Total" GridPane.columnIndex="2" GridPane.rowIndex="2">
<GridPane.margin>
<Insets left="60.0" top="130.0" />
</GridPane.margin></Label>
<Button fx:id="cal" layoutX="208.0" layoutY="440.0" mnemonicParsing="false" text="Calculate" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets left="90.0" top="120.0" />
</GridPane.margin></Button>
<Label layoutX="281.0" layoutY="303.0" text="=" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="130.0" left="120.0" />
</GridPane.margin></Label>
<Separator prefWidth="200.0" GridPane.columnSpan="3">
<GridPane.margin>
<Insets top="170.0" />
</GridPane.margin>
</Separator>
<Separator orientation="VERTICAL" prefHeight="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1" GridPane.rowSpan="2">
<GridPane.margin>
<Insets left="240.0" />
</GridPane.margin>
</Separator>
<Separator prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="2">
<GridPane.margin>
<Insets top="110.0" />
</GridPane.margin>
</Separator>
<Label fx:id="tot" text="0" GridPane.columnIndex="2" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="25.0" left="60.0" />
</GridPane.margin>
</Label>
<Label layoutX="320.0" layoutY="368.0" text="=" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="20.0" left="175.0" />
</GridPane.margin></Label>
<MenuBar GridPane.columnSpan="3">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="New" />
</items>
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Add" />
</items>
<items>
<MenuItem mnemonicParsing="false" text="Remove" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
<GridPane.margin>
<Insets bottom="150.0" />
</GridPane.margin>
</MenuBar>
</children>
<columnConstraints>
<ColumnConstraints maxWidth="476.0" minWidth="160.0" prefWidth="173.0" />
<ColumnConstraints maxWidth="258.0" minWidth="10.0" prefWidth="233.0" />
<ColumnConstraints maxWidth="300.0" minWidth="10.0" prefWidth="174.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="295.0" minHeight="172.0" prefHeight="172.0" />
<RowConstraints maxHeight="158.0" minHeight="10.0" prefHeight="142.0" />
<RowConstraints maxHeight="163.0" minHeight="10.0" prefHeight="163.0" />
</rowConstraints>
</GridPane>
这是Controller.java文件
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
public class Controller implements Initializable {
private static final String ORANGE = "Orange";
private static final String APPLES = "Apples";
private static final String MANGO = "Mango";
private static final String CORN = "Corn";
private ToggleGroup toggleGroup, toggleGroup2;
private List<TextField> textInputs;
private int c_num, a_num, cl_num, m_num, total43;
Alert alert = new Alert(Alert.AlertType.INFORMATION);
@FXML
public Pane pane;
@FXML
public DatePicker date;
@FXML
public TextField name, kgs1, mul1, advance, coolie, market;
@FXML
public Label value1, value4, value5, total, tot;
@FXML
public Button add, cal;
@FXML
public ChoiceBox choiceBox1;
@FXML
public RadioButton three, five, eight, ten;
@FXML
public GridPane gridPane;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
System.out.println("Inside initialize.");
textInputs = Arrays.asList(kgs1, mul1, advance, coolie, market, name);
choiceBox1.getItems().add(ORANGE);
choiceBox1.getItems().add(APPLES);
choiceBox1.getItems().add(MANGO);
choiceBox1.getItems().add(CORN);
choiceBox1.setValue(ORANGE);
choiceBox1.getSelectionModel().selectedItemProperty().addListener((observableValue, o, t1) -> {
System.out.println("choice1 clicked");
});
//
System.out.println("ToggleGroup 8 or 10 object creation begin.");
toggleGroup = new ToggleGroup();
System.out.println("ToggleGroup 8 or 10 object created.");
eight.setToggleGroup(toggleGroup);
ten.setToggleGroup(toggleGroup);
eight.setSelected(true);
//
System.out.println("ToggleGroup2 3 or 5 object creation begin.");
toggleGroup2 = new ToggleGroup();
System.out.println("ToggleGroup2 3 or 5 object created.");
three.setToggleGroup(toggleGroup2);
five.setToggleGroup(toggleGroup2);
three.setSelected(true);
cal.setOnAction(actionEvent -> {
System.out.println("cal function called. ");
cal();
System.out.println("cal function ended. ");
});
add.setOnAction(actionEvent -> {
System.out.println("add function called. ");
add();
System.out.println("add function ended. ");
});
}
private void add() {
TextField textField = new TextField();
gridPane.getChildren().add(textField);
}
public boolean isTrue() { // choose a better name for this
boolean valid = (date.getValue() != null);
if (valid) {
for (TextField tf : textInputs) {
if (tf.getText().trim().isEmpty()) {
System.out.println("TextField for loop");
valid = false;
break;
}
}
}
if (!valid) {
// show alert here...
System.out.println("inside if ");
alert.setTitle("Information");
System.out.println("alert.setTitle(Error)");
alert.setHeaderText("Error Occurred");
System.out.println("alert.setHeaderText(Error Occurred);");
alert.setContentText("Please Enter All Values");
System.out.println("alert.setContentText(Please Enter All Values)");
alert.setAlertType(Alert.AlertType.INFORMATION);
System.out.println("alert.setAlertType(Alert.AlertType.ERROR)");
alert.show();
System.out.println("alert.show()");
}
return valid;
}
private void cal() {
if (isTrue()) {
System.out.println("Else begins. ");
value1.setText(String.valueOf(Integer.parseInt(kgs1.getText()) * Integer.parseInt(mul1.getText())));
total43 = Integer.parseInt(value1.getText());
System.out.println("Total value " + total43);
// -------------------------------------
System.out.println("8 or 10 if begin.");
if (eight.isSelected()) {
c_num = (total43 * 8) / 100;
value4.setText(String.valueOf(c_num));
} else {
c_num = (total43 * 10) / 100;
value4.setText(String.valueOf(c_num));
}
System.out.println("8 or 10 if closed.");
a_num = Integer.parseInt(advance.getText());
System.out.println("advance " + a_num);
//
System.out.println("3 or 5 if begin.");
if (three.isSelected()) {
cl_num = Integer.parseInt(coolie.getText()) * 3;
value5.setText(String.valueOf(cl_num));
} else {
cl_num = Integer.parseInt(coolie.getText()) * 5;
value5.setText(String.valueOf(cl_num));
}
System.out.println("3 or 5 if closed.");
//
System.out.println("market value.");
m_num = Integer.parseInt(market.getText());
System.out.println("market value: " + market.getText());
tot.setText(String.valueOf(c_num + a_num + cl_num + m_num));
total43 = total43 - (c_num + a_num + cl_num + m_num);
total.setText(String.valueOf(total43));
}
}
}
能否请您告诉我单击添加按钮时如何添加ChoiceBox,TextField和Label,以及单击删除按钮时如何将其删除。
谢谢。