这次我准备了一个例子:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class testMain extends Application {
public static void main(String[] args) {
testMain.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
VBox root = new VBox(5);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(10, 5, 10, 5));
Scene scene = new Scene(root);
ComboBox<String> cb = new ComboBox<>();
cb.getItems().addAll("content 1" , "content 2", "content 3");
Label label = new Label("shows content");
cb.valueProperty().addListener((observable, oldValue, newValue) -> {
ComboBox<String> newCb = cb;
root.getChildren().clear();
Label newLabel = new Label(newValue);
root.getChildren().addAll(newLabel, cb);
});
root.getChildren().addAll(label, cb);
primaryStage.setScene(scene);
primaryStage.show();
}
}
在我的程序中,我向根添加了可变数量的Vbox,具体取决于来自保管箱的选择。 Thatswhy我需要清除子列表并从头开始重建(这是最简单的方法^^)。
问题是,组合框在第一次选择后会冻结,有没有办法避免这种情况或让它再次运行?
答案 0 :(得分:1)
这看起来像个错误。
那就是说,我强烈建议你不要这样实施。例如,您可以使用BorderPane
底部的组合框和中心的VBox
。然后重建VBox
:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane ;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TestMain extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
VBox vbox = new VBox(5);
vbox.setAlignment(Pos.CENTER);
vbox.setPadding(new Insets(10, 5, 10, 5));
BorderPane root = new BorderPane(vbox);
Scene scene = new Scene(root);
ComboBox<String> cb = new ComboBox<>();
root.setBottom(cb);
cb.getItems().addAll("content 1" , "content 2", "content 3");
Label label = new Label("shows content");
cb.valueProperty().addListener((observable, oldValue, newValue) -> {
vbox.getChildren().clear();
Label newLabel = new Label(newValue);
vbox.getChildren().addAll(newLabel);
});
vbox.getChildren().addAll(label);
primaryStage.setScene(scene);
primaryStage.show();
}
}
或者,根据您的设计,只是不要删除组合框:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.Arrays ;
public class testMain extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
VBox root = new VBox(5);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(10, 5, 10, 5));
Scene scene = new Scene(root);
ComboBox<String> cb = new ComboBox<>();
cb.getItems().addAll("content 1" , "content 2", "content 3");
Label label = new Label("shows content");
cb.valueProperty().addListener((observable, oldValue, newValue) -> {
root.getChildren().removeIf(node -> node != cb);
Label newLabel = new Label(newValue);
root.getChildren().addAll(0, Arrays.asList(newLabel));
});
root.getChildren().addAll(label, cb);
primaryStage.setScene(scene);
primaryStage.show();
}
}