我对javafx很新,但是想要制作这个下拉列表(组合框),这样当你做出选择时,文本框或列表中的文本会发生变化 - 例如。当在下拉菜单中选择水果或饮料时,会出现含有不同水果或饮料的清单。
我无法使用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<>();
}
水果和蔬菜类
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;
}
}
如何从comboBox中选择它会显示水果列表的内容?
答案 0 :(得分:0)
这是一个可以让你入门的可运行的课程
XPdfForm