BorderPane 不会显示 ListView JavaFx
BorderPane won't show ListView JavaFx
我有一个 window 由底部的 BorderPane 组成,西区有一个 VBox,我在其中放置一些标签,东区有另一个 VBox,我在其中放置文本字段和 ListView,HBox 有一些按钮放在南方。
问题是无论如何我放置这个ListView我只能看到一行。 ListView 已填充。
如果我使用 GridPane,它会显示 ListView,但所有内容都放错了。
这是 BorderPane 的样子:
代码如下:
window2.setTitle("Update Category");
List<Skill> listSlill=(new SkillCrud()).allS();
ObservableList<Skill> items = FXCollections.observableArrayList
(listSlill);
skillListView = new ListView<>(items);
//System.out.println((new SkillCrud()).allS()+" -- "+skillListView.getItems());
Label categoryLabel = new Label("Category: ");
categoryLabel.setFont(Font.font("Arial", FontWeight.BOLD, 14));
categoryLabel.setTranslateY(30);
Label skillLabel = new Label("Skill: ");
skillLabel.setFont(Font.font("Arial", FontWeight.BOLD, 14));
skillLabel.setTranslateY(40);
TextField categoryField = new TextField();
categoryField.setText(up.getName());
VBox lefta = new VBox();
lefta.getChildren().addAll(categoryLabel,skillLabel);
//lefta.setPadding(new Insets(0, 0, 0, 30));
VBox righta = new VBox();
righta.getChildren().addAll(categoryField,skillListView);
HBox downa = new HBox();
Button updateButton = new Button("Update");
Button cancelButton = new Button("Cancel");
updateButton.setMaxWidth(Double.MAX_VALUE);
cancelButton.setMaxWidth(Double.MAX_VALUE);
cancelButton.setPrefSize(75, 20);
cancelButton.setTranslateX(20);
updateButton.setPrefSize(75, 20);
updateButton.setTranslateX(45);
downa.setSpacing(40);
//downa.setPadding(new Insets(0, 10, 8, 25));
downa.getChildren().addAll(cancelButton, updateButton);
//righta.setSpacing(10);
//righta.setPadding(new Insets(20, 30, 0, 0));
righta.setAlignment(Pos.BASELINE_LEFT);
// GridPane grid = new GridPane();
//
// grid.setAlignment(Pos.CENTER);
// grid.setHgap(10);
// grid.setVgap(10);
// grid.setPadding(new Insets(25, 25, 25, 25));
//
//
// grid.add(categoryLabel, 0, 1, 2, 2);
// grid.add(categoryField, 1, 1, 2, 2);
//
// grid.add(skillLabel, 0, 2);
// grid.add(skillListView, 1, 2);
BorderPane layout = new BorderPane();
layout.setLeft(lefta);
layout.setRight(righta);
layout.setBottom(downa);
Scene scene2 = new Scene(layout, 300, 300);
// window2.setMaxWidth(300);
// window2.setMaxHeight(300);
// window2.setMinWidth(300);
// window2.setMinHeight(300);
window2.setScene(scene2);
window2.showAndWait();
这里有一个 mcve,它使调试和获得帮助变得更加容易:
public class StageTest extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Update Category");
ObservableList<String> items
= FXCollections.observableArrayList(createStrings());
ListView<String> skillListView = new ListView<>(items);
TextField categoryField = new TextField();
categoryField.setText("Category");
VBox righta = new VBox();
righta.getChildren().addAll(categoryField,skillListView);
righta.setAlignment(Pos.BASELINE_LEFT);
BorderPane layout = new BorderPane();
layout.setRight(righta);
Scene scene2 = new Scene(layout, 300, 300);
stage.setScene(scene2);
stage.show();
}
private List<String> createStrings() {
return IntStream.rangeClosed(0, 5)
.mapToObj(i -> "Skill "+i)
.map(String::new)
.collect(Collectors.toList());
}
}
运行吧,看看问题所在。
删除 righta.setAlignment(Pos.BASELINE_LEFT);
并查看它是否已解决。
我有一个 window 由底部的 BorderPane 组成,西区有一个 VBox,我在其中放置一些标签,东区有另一个 VBox,我在其中放置文本字段和 ListView,HBox 有一些按钮放在南方。
问题是无论如何我放置这个ListView我只能看到一行。 ListView 已填充。
如果我使用 GridPane,它会显示 ListView,但所有内容都放错了。
这是 BorderPane 的样子:
代码如下:
window2.setTitle("Update Category");
List<Skill> listSlill=(new SkillCrud()).allS();
ObservableList<Skill> items = FXCollections.observableArrayList
(listSlill);
skillListView = new ListView<>(items);
//System.out.println((new SkillCrud()).allS()+" -- "+skillListView.getItems());
Label categoryLabel = new Label("Category: ");
categoryLabel.setFont(Font.font("Arial", FontWeight.BOLD, 14));
categoryLabel.setTranslateY(30);
Label skillLabel = new Label("Skill: ");
skillLabel.setFont(Font.font("Arial", FontWeight.BOLD, 14));
skillLabel.setTranslateY(40);
TextField categoryField = new TextField();
categoryField.setText(up.getName());
VBox lefta = new VBox();
lefta.getChildren().addAll(categoryLabel,skillLabel);
//lefta.setPadding(new Insets(0, 0, 0, 30));
VBox righta = new VBox();
righta.getChildren().addAll(categoryField,skillListView);
HBox downa = new HBox();
Button updateButton = new Button("Update");
Button cancelButton = new Button("Cancel");
updateButton.setMaxWidth(Double.MAX_VALUE);
cancelButton.setMaxWidth(Double.MAX_VALUE);
cancelButton.setPrefSize(75, 20);
cancelButton.setTranslateX(20);
updateButton.setPrefSize(75, 20);
updateButton.setTranslateX(45);
downa.setSpacing(40);
//downa.setPadding(new Insets(0, 10, 8, 25));
downa.getChildren().addAll(cancelButton, updateButton);
//righta.setSpacing(10);
//righta.setPadding(new Insets(20, 30, 0, 0));
righta.setAlignment(Pos.BASELINE_LEFT);
// GridPane grid = new GridPane();
//
// grid.setAlignment(Pos.CENTER);
// grid.setHgap(10);
// grid.setVgap(10);
// grid.setPadding(new Insets(25, 25, 25, 25));
//
//
// grid.add(categoryLabel, 0, 1, 2, 2);
// grid.add(categoryField, 1, 1, 2, 2);
//
// grid.add(skillLabel, 0, 2);
// grid.add(skillListView, 1, 2);
BorderPane layout = new BorderPane();
layout.setLeft(lefta);
layout.setRight(righta);
layout.setBottom(downa);
Scene scene2 = new Scene(layout, 300, 300);
// window2.setMaxWidth(300);
// window2.setMaxHeight(300);
// window2.setMinWidth(300);
// window2.setMinHeight(300);
window2.setScene(scene2);
window2.showAndWait();
这里有一个 mcve,它使调试和获得帮助变得更加容易:
public class StageTest extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Update Category");
ObservableList<String> items
= FXCollections.observableArrayList(createStrings());
ListView<String> skillListView = new ListView<>(items);
TextField categoryField = new TextField();
categoryField.setText("Category");
VBox righta = new VBox();
righta.getChildren().addAll(categoryField,skillListView);
righta.setAlignment(Pos.BASELINE_LEFT);
BorderPane layout = new BorderPane();
layout.setRight(righta);
Scene scene2 = new Scene(layout, 300, 300);
stage.setScene(scene2);
stage.show();
}
private List<String> createStrings() {
return IntStream.rangeClosed(0, 5)
.mapToObj(i -> "Skill "+i)
.map(String::new)
.collect(Collectors.toList());
}
}
运行吧,看看问题所在。
删除 righta.setAlignment(Pos.BASELINE_LEFT);
并查看它是否已解决。