JavaFX 包组件

标签 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 组件未打包( 更改屏幕分辨率、自动组件时如何获取?

最佳答案

我会做这样的事情:

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

关于JavaFX 包组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38285535/

相关文章:

java - Android - 如何在另一个叠加层的 onTap 操作中显示另一个叠加层

html - 激活时在文本框周围制作发光效果

iPhone 自定义 UI 设计和构建资源

java - 仅在 Catch block 中编写记录代码而不处理行为会对程序性能产生什么样的影响?

Java路径末尾的两个点有意想不到的结果

java - 尝试获取 Radio Button.is Selected() 以设置标签的文本。不工作

html - div 容器内的 div 中的 Tex 在小视口(viewport)上显得很小

css - 我可以在 CSS 中继承类作为包装层吗

c# - 子窗口顶部的 WPF 所有者窗口

flutter - 在 ListTile 中更改 CircleAvatar 大小