Java 在Combox事件上动态添加文本字段和标签

Java 在Combox事件上动态添加文本字段和标签,java,javafx,combobox,textfield,fxml,Java,Javafx,Combobox,Textfield,Fxml,我已经制作了一个运行良好的应用程序,但我不想添加函数,要这样做,我需要在更改组合框的值时添加一些文本字段和标签。 这是我的主要代码: import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.Parent; public class maincombo extend

我已经制作了一个运行良好的应用程序,但我不想添加函数,要这样做,我需要在更改组合框的值时添加一些文本字段和标签。 这是我的主要代码:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.Parent;


public class maincombo extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent parent = FXMLLoader.load(getClass().getResource("/combobox.fxml"));
        Scene scene = new Scene(parent);
        stage.setTitle("Application");
        stage.setScene(scene);
        stage.show();

}
}
我的控制器:

import java.net.URL;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;

public class comboboxcontroller implements Initializable {

    @FXML
    private ComboBox<Integer>methode;
    ObservableList<Integer>nombre = FXCollections.observableArrayList();
    @FXML
    private void metho (ActionEvent event){
        switch (methode.getValue()) {
        case 0 :

            break;
        case 1 :
            break;
        case 2 :

            break;
        case 3 :


            break;
        case 4 :

            break;
        case 5 :

            break;
        default :

            break;
        }
    }

    public comboboxcontroller (){

    }


    public void initialize(URL url,ResourceBundle rb){

        nombre.add(new Integer(0));
        nombre.add(new Integer(1));
        nombre.add(new Integer(2));
        nombre.add(new Integer(3));
        nombre.add(new Integer(4));
        nombre.add(new Integer(5));
        methode.setItems(nombre);
    }
}

但一切仍然可见。所以我开始想,也许动态添加或者使用弹出窗口更容易。这就是我来这里寻求帮助的原因。

几天前我做了类似的事情,请查看:

“类型”-枚举

“指标”-等级:

组合框目标的操作方法:

@FXML private ComboBox<Type> target;
@FXML private VBox targetBox;   

@FXML public void onTargetSelected() {
    targetBox.getChildren().clear();
    for(Indicator indic : target.getValue().getIndics()) {
        VBox box = new VBox();

        Text t = new Text(indic.getLabel());
        box.getChildren().add(t);

        TextField txt = new TextField();
        txt.setMaxWidth(target.getWidth());
        box.getChildren().add(txt);

        targetBox.getChildren().add(box);
    }
    Controller.stage.close();
    Controller.stage.show();
}
@FXML私有组合框目标;
@FXML专用VBox targetBox;
@FXML public void onTargetSelected(){
targetBox.getChildren().clear();
对于(指示符标记:target.getValue().getIndics()){
VBox box=新的VBox();
Text t=新文本(indi.getLabel());
box.getChildren().add(t);
TextField txt=新的TextField();
setMaxWidth(target.getWidth());
box.getChildren().add(txt);
targetBox.getChildren().add(box);
}
Controller.stage.close();
Controller.stage.show();
}

是的,但当我不写任何代码时,每个人都在要求它,当我写代码时,没有人想要它!我不知道我可以从代码中删除什么,因为我需要做的就是清楚我的代码结构。它还显示了它是如何制作的,可能会帮助我添加我想要的内容,并最终改进它。但是如果你在这里看到任何无用的东西,告诉我,我会改变它!这是完整的。拿走我所有的代码,你就可以像我一样执行它了!我怎样才能向你证明我不知道怎么做?因为这实际上是我的问题!我没有任何例外或错误。我只是不知道。所有这些都与问题有关,因为当我更改组合框上的选项时,我需要添加文本字段和标签!只是感觉它是最小的(即使它是很多我不知道如何减少它或它将不可执行)、完整的(不知道您还需要什么)、可验证的(您可以看到在更改我的Combox的值时没有发生任何事情)那新的一个呢?我再次编辑主题您能解释更多关于“类型枚举”和“指示符”的信息吗类。我真的不明白它是什么。Indicator类定义了textfield的标签(将显示在textfield上方)。在类型枚举中,您定义了组合框的所有可能值。在构造函数中,您定义了将显示多少textfield以及所选值(例如boObject有三个textfield)那么boObjekt、job、table、tableColumn是组合框中可用的不同值吗?是的,您是对的。使用“target.getItems().addAll(Arrays.asList(Type.values());”可以将定义的所有值添加到组合框中;)
MyTextField.setVisible(false);
public enum Type {
boObject("BO-Objekt", new Indicator("Test1"), new Indicator("Test2"), new Indicator("Test3")),
job("Job", new Indicator("Test1"), new Indicator("Test2")),
table("Tabelle", new Indicator("Test1")),
tableColumn("Tabellenspalte", new Indicator("Test1"), new Indicator("Test2"));

private String displayName;
private Indicator[] indics;

private Type(String displayName, Indicator ... indics) {
    this.displayName = displayName;
    this.indics = indics;
}

@Override public String toString() {
    return displayName;
}

public Indicator[] getIndics() {
    return indics;
}

public void setIndics(Indicator[] indics) {
    this.indics = indics;
}
}
public class Indicator {
private String label;

public Indicator(String label) {
    this.label = label;
}

public String getLabel() {
    return label;
}

public void setLabel(String label) {
    this.label = label;
}
}
@FXML private ComboBox<Type> target;
@FXML private VBox targetBox;   

@FXML public void onTargetSelected() {
    targetBox.getChildren().clear();
    for(Indicator indic : target.getValue().getIndics()) {
        VBox box = new VBox();

        Text t = new Text(indic.getLabel());
        box.getChildren().add(t);

        TextField txt = new TextField();
        txt.setMaxWidth(target.getWidth());
        box.getChildren().add(txt);

        targetBox.getChildren().add(box);
    }
    Controller.stage.close();
    Controller.stage.show();
}