Javafx 自动调整滚动窗格的大小
我想调整滚动窗格的大小以适应父组件。我测试了这段代码:Javafx 自动调整滚动窗格的大小,javafx,javafx-2,javafx-8,Javafx,Javafx 2,Javafx 8,我想调整滚动窗格的大小以适应父组件。我测试了这段代码: import javafx.application.Application; import static javafx.application.Application.launch; import javafx.scene.Scene; import javafx.scene.control.ScrollPane; import javafx.scene.control.ScrollPane.ScrollBarPolicy; import
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class MainApp extends Application {
@Override
public void start(Stage stage) throws Exception {
BorderPane bp = new BorderPane();
bp.setPrefSize(600, 600);
bp.setMaxSize(600, 600);
bp.setStyle("-fx-background-color: #2f4f4f;");
VBox vb = new VBox(bp);
ScrollPane scrollPane = new ScrollPane(vb);
scrollPane.setFitToHeight(true);
scrollPane.setFitToWidth(true);
scrollPane.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);
scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
Scene scene = new Scene(scrollPane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
但正如你所看到的,我没有看到树滚动条。我的代码有什么错误吗
滚动条不会显示,因为
ScrollBarPolicy.AS\u所需
setFitToHeight
和setFitToWidth
并将其保留为false
请注意,ScrollBarPolicy
也可以设置为始终
,而不是根据需要
,这样即使在窗口展开时,滚动条也会保持不变
滚动条不会显示,因为
ScrollBarPolicy.AS\u所需
setFitToHeight
和setFitToWidth
并将其保留为false
请注意,ScrollBarPolicy
也可以设置为始终
,而不是根据需要
,这样即使在窗口展开时,滚动条也会保持不变
public class MainApp extends Application {
@Override
public void start(Stage stage) throws Exception {
BorderPane bp = new BorderPane();
bp.setPrefSize(600, 600);
bp.setMaxSize(600, 600);
bp.setStyle("-fx-background-color: #2f4f4f;");
VBox vb = new VBox(bp);
ScrollPane scrollPane = new ScrollPane(vb);
//scrollPane.setFitToHeight(true);
//scrollPane.setFitToWidth(true);
scrollPane.setHbarPolicy(ScrollBarPolicy.ALWAYS);
scrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS);
Scene scene = new Scene(scrollPane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}