JavaFX GridPane 固定行高

huangapple go评论86阅读模式
英文:

JavaFX GridPane fixed row size

问题

I am working on a grammar analysis program. I am using a GridPane and have three TextAreas so far. The TextArea paliVerse should span over two rows; the TextArea analysis over four. However, when I change the TextArea analysis with

gridPane.add(analysis, 2, 1, 4, 1);
analysis.setPrefRowCount(4);

the TextArea paliVerse also spans over rows, even though the PrefRowCount is 2. How can I change this so that it looks like:

JavaFX GridPane 固定行高

英文:

I am working on a grammar analysis program. I am using a GridPane and have three TextAreas so far. The TextArea paliVerse should span over two rows; the TextArea analysis over four. However, when I change the TextArea analysis with

gridPane.add(analysis,2,1,4,1);
analysis.setPrefRowCount(4);

the TextArea paliVerse also spans over rows, even though the PrefRowCount is 2:

JavaFX GridPane 固定行高

How can I change this so that it looks like:

JavaFX GridPane 固定行高

This is the relevant code:

package com.example.projektver180523;

import java.sql.*;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class HelloApplication extends Application {
    private ComboBox<Integer> comboBox;
    private TextArea paliVerse;
    private TextArea paliVerseGER;
    private TextArea analysis;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws SQLException {

        // Erstelle die ComboBox
        comboBox = new ComboBox<>();

        // Fülle die ComboBox automatisch
        fillComboBox();

        // Erstelle die TextAreas
        paliVerse = new TextArea();
        paliVerseGER = new TextArea();
        analysis = new TextArea();

        // Textfeld ist unveränderlich
        paliVerse.setEditable(false);
        paliVerseGER.setEditable(false);
        analysis.setEditable(false);

        // 2 columns preferred
        paliVerse.setPrefColumnCount(2);
        paliVerseGER.setPrefColumnCount(2);

//        // 2 rows preferred for paliVerse and German translation textArea
        paliVerse.setPrefRowCount(2);
        paliVerseGER.setPrefRowCount(2);
        analysis.setPrefRowCount(4);

        GridPane.setConstraints(comboBox, 0, 1);
        GridPane gridPane = new GridPane();

        // Abstand nach innen setzen
        gridPane.setPadding(new Insets(10));

        // horizontaler Abstand zwischen Kindern
        gridPane.setHgap(10);

        // vertikaler Abstand zwischen Kindern
        gridPane.setVgap(10);

        // Spalte 0 ist 150 weit
        gridPane.getColumnConstraints().add(new ColumnConstraints(230));

        // Spalte 0 ist 480 weit
        gridPane.getColumnConstraints().add(new ColumnConstraints(600));

        gridPane.add(new Label("Vers Nummer:"), 0, 0);
        gridPane.add(comboBox, 1, 0);
        gridPane.add(new Label("Palitext:"), 0, 1);
        gridPane.add(paliVerse, 1, 1);
        gridPane.add(new Label("Übersetzung Deutsch:"), 0, 2);
        gridPane.add(paliVerseGER, 1, 2);
        gridPane.add(new Label("Analyse:"), 2, 0);
        gridPane.add(analysis, 2, 1, 4, 1);
        Scene scene = new Scene(gridPane, 1200, 400);

        // Verknüpfe CSS-Datei
        scene.getStylesheets().add("styles.css");

        // Zeige das Hauptfenster an
        stage.setTitle("Dhammapada Reader");
        stage.setScene(scene);
        stage.show();
    }
}

答案1

得分: 3

你似乎让这变得比必要的复杂。如文档中所述,GridPane 中的每个单元格都会根据包含的 Node 大小进行调整大小:

> 默认情况下,行和列将根据其内容大小调整大小;列将足够宽以容纳最宽的子项,行将足够高以适应最高的子项。

因此,你只需要显式设置 [TextArea] analysis 的行跨度为2(两行)。考虑以下代码:

(请注意,ComboBox 的实际内容与您的问题无关,因此下面的代码不会填充 ComboBox。此外,您的问题并不包含方法 fillComboBox 的代码。)

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class HelloApplication extends Application {
    private ComboBox<Integer> comboBox;
    private TextArea paliVerse;
    private TextArea paliVerseGER;
    private TextArea analysis;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        comboBox = new ComboBox<>();
        paliVerse = new TextArea();
        paliVerseGER = new TextArea();
        analysis = new TextArea();
        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(10));
        gridPane.setHgap(10);
        gridPane.setVgap(10);
        gridPane.add(new Label("Vers Nummer:"), 0, 0);
        gridPane.add(comboBox, 1, 0);
        gridPane.add(new Label("Palitext:"), 0, 1);
        gridPane.add(paliVerse, 1, 1);
        gridPane.add(new Label("Übersetzung Deutsch:"), 0, 2);
        gridPane.add(paliVerseGER, 1, 2);
        gridPane.add(new Label("Analyse:"), 2, 0);
        gridPane.add(analysis, 2, 1, 1, 2);
        Scene scene = new Scene(gridPane, 1200, 400);

        // Zeige das Hauptfenster an
        stage.setTitle("Dhammapada Reader");
        stage.setScene(scene);
        stage.show();
    }
}

这是它的外观:

JavaFX GridPane 固定行高

英文:

You seem to be making this more complicated than it needs to be. Each cell in GridPane will be sized to accomodate the Node it contains, as stated in the class documentation:
> By default, rows and columns will be sized to fit their content; a column will be wide enough to accommodate the widest child, a row tall enough to fit the tallest child.

So all you need do is explicitly set the row span for [TextArea] analysis to 2 (two).
Consider the following code:
(Note that the actual contents of the ComboBox is irrelevant to your question so the below code does not populate the ComboBox. Besides, your question doesn't contain the code for method fillComboBox anyway.)

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class HelloApplication extends Application {
    private ComboBox<Integer> comboBox;
    private TextArea paliVerse;
    private TextArea paliVerseGER;
    private TextArea analysis;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        comboBox = new ComboBox<>();
        paliVerse = new TextArea();
        paliVerseGER = new TextArea();
        analysis = new TextArea();
        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(10));
        gridPane.setHgap(10);
        gridPane.setVgap(10);
        gridPane.add(new Label("Vers Nummer:"), 0, 0);
        gridPane.add(comboBox, 1, 0);
        gridPane.add(new Label("Palitext:"), 0, 1);
        gridPane.add(paliVerse, 1, 1);
        gridPane.add(new Label("Übersetzung Deutsch:"), 0, 2);
        gridPane.add(paliVerseGER, 1, 2);
        gridPane.add(new Label("Analyse:"), 2, 0);
        gridPane.add(analysis, 2, 1, 1, 2);
        Scene scene = new Scene(gridPane, 1200, 400);

        // Zeige das Hauptfenster an
        stage.setTitle("Dhammapada Reader");
        stage.setScene(scene);
        stage.show();
    }
}

This is how it looks:

JavaFX GridPane 固定行高

huangapple
  • 本文由 发表于 2023年5月26日 15:06:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定