下拉列表中的Javafx选择提供了不同的文本框

下拉列表中的Javafx选择提供了不同的文本框,javafx,combobox,Javafx,Combobox,我对javafx非常陌生,但是我想让它成为一个下拉列表(combobox),这样当你做出选择时,文本框或列表中的文本就会改变——例如,在下拉列表中选择水果或饮料时,会出现一个包含不同水果或饮料的列表 我不能使用fxml。 谁能告诉我怎么做 这是我的主课 package grocerylist; import javafx.application.Application; import static javafx.application.Application.launch; import ja

我对javafx非常陌生,但是我想让它成为一个下拉列表(combobox),这样当你做出选择时,文本框或列表中的文本就会改变——例如,在下拉列表中选择水果或饮料时,会出现一个包含不同水果或饮料的列表

我不能使用fxml。 谁能告诉我怎么做

这是我的主课

package grocerylist;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;


public class GroceryList extends Application { 
FruitVeg fruitVegList;
Beverages beverageList;
Bread breadList;
ComboBox comboBox;
TextField tf1;
ListView lv2;

@Override
public void start(Stage stage){

    fruitVegList = new FruitVeg();
    beverageList = new Beverages();
    breadList = new Bread();



    BorderPane root = new BorderPane();
    Scene scene = new Scene(root, 750, 750);

    GridPane grid = new GridPane();
    grid.setPadding(new Insets(20,20,20,20));
    grid.setHgap(10);
    grid.setVgap(10);
    ColumnConstraints column1 = new ColumnConstraints(50);
    ColumnConstraints column2 = new ColumnConstraints(200, 200,
    Double.MAX_VALUE);
    ColumnConstraints column3 = new ColumnConstraints(200, 200,
    Double.MAX_VALUE);
    column1.setHgrow(Priority.ALWAYS);
    column3.setHgrow(Priority.ALWAYS);
    grid.getColumnConstraints().addAll(column1, column2, column3);
    grid.setGridLinesVisible(true);


    Text scenetitle = new Text("My Shopping List");
    scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
    grid.add(scenetitle, 0, 0, 4, 1);


    Label op1 = new Label("1.");
    op1.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
    grid.add(op1, 0, 1);

    comboBox = new ComboBox();
    comboBox.getItems().addAll(
                "Fruits and Vegetables",
                "Beverages",
                "Bread"

    );
    comboBox.setPromptText("Choose a department");
    grid.add(comboBox, 1, 1);

   /* comboBox.setOnAction ((ActionEvent event) -> {
        comboBox.getSelectionModel().getSelectedItem();
        if (comboBox.getValue().equals("Fruits")) {
            lv1.setItem(fruitVegList.getFruit());
        }
    }); */


    Label op2 = new Label("2.");
    op2.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
    grid.add(op2, 0, 2);

    tf1 = new TextField();
    grid.add(tf1, 1, 2);

    Label op3 = new Label("3.");
    op3.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
    grid.add(op3, 2, 2);

    lv2 = new ListView();
    grid.add(lv2, 3, 2);

    comboBox.setOnAction(event -> {
        comboBox.getSelectionModel().selectedItemProperty().addListener((ObservableValue observable, Object oldvalue, Object newValue) -> {
            if (newValue == "Fruit and Vegetables") {
                tf1.setText(fruitVegList);
            }
        });

    });

    root.getChildren().addAll(grid);

    stage.setTitle("MY SHOPPING LIST");
    stage.setScene(scene);
    stage.show();
}

public static void main(String[] args) { launch(args); }
}
我有一个抽象类作为我作业的一部分,但这就是为什么我不能让它工作的原因吗

package grocerylist;

import java.util.ArrayList;
import java.util.List;


public abstract class Groceries {

List<String> fruitVegList = new ArrayList<>();   
}
包装杂货店列表;
导入java.util.ArrayList;
导入java.util.List;
公共抽象类杂货{
List-fruitVegList=new-ArrayList();
}
水果和蔬菜类

package grocerylist;

import java.util.List;

public class FruitVeg extends Groceries {

public FruitVeg(){
    fruitVegList.add("Apple");
    fruitVegList.add("Banana");
    fruitVegList.add("Cucumber");
    fruitVegList.add("Carrot");
    fruitVegList.add("Kale");
    fruitVegList.add("Salad");
    fruitVegList.add("Pear");

}

public List<String> getFruit() {
    return fruitVegList;
}

}    
包装杂货店列表;
导入java.util.List;
公共级水果蔬菜扩展食品杂货{
公共果蔬{
水果蔬菜列表。添加(“苹果”);
水果蔬菜列表。添加(“香蕉”);
添加(“黄瓜”);
水果蔬菜列表。添加(“胡萝卜”);
水果蔬菜列表。添加(“羽衣甘蓝”);
水果蔬菜列表。添加(“沙拉”);
水果蔬菜列表。添加(“梨”);
}
公共列表getFruit(){
返回果蔬列表;
}
}    

当从comboBox中选择时,它将显示水果列表的内容,我该如何设置呢?

下面是一个可运行的类,让您开始学习

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage){

        final String[] comboBoxItems = {"Fruit", "Beverages"};

        ComboBox comboBox = new ComboBox();

        TextField textField = new TextField();

        VBox root = new VBox();

        comboBox.getItems().addAll(comboBoxItems);

        comboBox.setOnAction(event -> {
            textField.setText(comboBox.getValue().toString());
        });

        root.getChildren().addAll(comboBox, textField);

        Scene scene = new Scene(root);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) { launch(args); }
}

欢迎来到StackOverflow。如前所述,你的问题不太适合这个网站。我建议你看一下,尤其是在。你的问题表述方式的问题在于,我们不知道你到底停留在哪一部分:可能是你编写监听器的部分,或者是你更改标签文本的部分,或者是你知道当选择改变时,
组合框
的哪个属性会改变的部分,等等,对此的任何回答基本上都是一个完整的JavaFX教程,看看是否可以发布一个特定的问题,并用代码精确地指出您尝试的部分不起作用。