JavaFX包组件

时间:2016-07-09 19:25:38

标签: java css user-interface javafx

布局layout.fxml:

    <BorderPane fx:id="mainLayout" xmlns="http://javafx.com/javafx/8"xmlns:fx="http://javafx.com/fxml/1">
</BorderPane>

此外,该方法在循环200按钮中:

public class Main extends Application {
    @FXML
    private static BorderPane mainLayout;
    private Stage stage;

    @Override
    public void start(Stage primaryStage) throws IOException {
        this.stage = primaryStage;
        initMainLayout();
        initLoginForm();
    }

    private void initMainLayout() throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("/fxml/layout.fxml"));
        mainLayout = loader.load();
        mainLayout.getStyleClass().add("v-box");
        Scene scene = new Scene(mainLayout);
        scene.getStylesheets().add("/css/style.css");
        stage.setTitle("ТЕМА ДИПЛОМА");
        stage.setMaximized(true);
        stage.setResizable(false);
        stage.setScene(scene);
        stage.show();
    }
    public void initMainForm() {
            final int HEIGHT = 39;
            final int WIDTH = 120;
            int count = 1;
            GridPane gridPane = new GridPane();
            gridPane.getStyleClass().add("grid-pane");
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 20; j++) {
                    Button button = new Button();
                    button.setText("Помещение №" + count++);
                    button.getStyleClass().add("button-all");
                    button.setOnAction(event -> new AdditionalController().buttonPressed(event));
                    gridPane.getRowConstraints().add(new RowConstraints(HEIGHT));
                    gridPane.getColumnConstraints().add(new ColumnConstraints(WIDTH));
                    gridPane.add(button, i, j);
                }
            }
            mainLayout.setPadding(new Insets(0, 0, 0, 0));
            mainLayout.getStyleClass().remove("v-box");
            mainLayout.getStyleClass().add("border-pane");
            mainLayout.setCenter(gridPane);
        }

CSS样式按钮:

.button-all {
    -fx-background-color: #faffb0;
    -fx-font-family: "Segoe UI", Helvetica, Arial, sans-serif;
    -fx-font-size: 9pt;
    -fx-text-fill: #054500;
    -fx-max-width: 89pt;
    -fx-max-height: 120pt;
}

结果分辨率1280:1024 enter image description here 一切都很完美!

但是,1024x768,它很糟糕: enter image description here 组件不包装( 更改屏幕分辨率,自动组件时如何获得?

2 个答案:

答案 0 :(得分:1)

我会做这样的事情:

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 20; j++) {
            Button button = new Button();
            button.setText("Помещение №" + count++);
            button.getStyleClass().add("button-all");
            button.prefWidthProperty().bind(Bindings.divide(gridPane.widthProperty(), 10));
            button.prefHeightProperty().bind(Bindings.divide(gridPane.heightProperty(), 20));
            //gridPane.getRowConstraints().add(new RowConstraints(HEIGHT));
            //gridPane.getColumnConstraints().add(new ColumnConstraints(WIDTH));
            gridPane.add(button, i, j);
        }
    }

在css中摆脱-fx-max-width-fx-max-height

答案 1 :(得分:0)

添加

fetch --prune

到按钮样式表的末尾。