我有一个类似于下面用fxml代码显示的场景(用SceneBuilder制作):
sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane fx:id="bpMain" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="600.0" minWidth="800.0" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<center>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<HBox AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<VBox prefHeight="200.0" prefWidth="100.0">
<children>
<BorderPane>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</BorderPane>
<BorderPane layoutX="10.0" layoutY="10.0" prefHeight="200.0" prefWidth="200.0">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<top>
<Label text="List 1:" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</BorderPane.margin>
</Label>
</top>
<center>
<ListView minHeight="500.0" minWidth="-Infinity" prefHeight="500.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</ListView>
</center>
</BorderPane>
</children>
</VBox>
<BorderPane prefHeight="200.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
<right>
<VBox minWidth="100.0" BorderPane.alignment="CENTER">
<children>
<BorderPane>
<center>
<ListView minWidth="150.0" prefHeight="500.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets top="10.0" />
</BorderPane.margin>
</ListView>
</center>
<VBox.margin>
<Insets bottom="10.0" top="10.0" />
</VBox.margin>
<top>
<VBox alignment="CENTER" BorderPane.alignment="CENTER">
<children>
<Label text="List2:" />
</children>
</VBox>
</top>
</BorderPane>
</children>
</VBox>
</right>
<center>
<BorderPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<top>
<Label text="Label" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="15.0" left="5.0" right="5.0" top="5.0" />
</BorderPane.margin>
</Label>
</top>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<center>
<BorderPane fx:id="matrixBorderPane" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</center>
<bottom>
<VBox alignment="CENTER" spacing="10.0" BorderPane.alignment="CENTER">
<children>
<HBox alignment="CENTER" spacing="10.0">
<children>
<Button fx:id="button5" mnemonicParsing="false" onAction="#show5matrix" text="5" />
<Button fx:id="button15" layoutX="171.0" layoutY="10.0" mnemonicParsing="false" onAction="#show15matrix" text="15" />
</children>
</HBox>
</children>
</VBox>
</bottom>
</BorderPane>
</center>
</BorderPane>
</children>
</HBox>
</children>
</AnchorPane>
</center>
</BorderPane>
我想在matrixBorderPane
中添加一个矩阵,TextFields
代表GridPane
。当矩阵很小时,下面添加的代码对我来说是完美的(在点击Button
的{{1}}时看到它),但是尺寸更大的值并非全部可见(5
{{1 }}):
Button
15
Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("Matrix");
primaryStage.setScene(scene);
primaryStage.setMinHeight(750);
primaryStage.setMinWidth(550);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
因此,我向Controller.java
添加了package sample;
import javafx.beans.binding.Bindings;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML public BorderPane matrixBorderPane;
@FXML public Button button5;
@FXML public Button button15;
private ScrollPane scrollPane = new ScrollPane();
////////////////////////////////////////////////
@FXML
public void show5matrix(ActionEvent e){
showMatrix(5);
}
@FXML
public void show15matrix(ActionEvent e) {
showMatrix(15);
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
private void showMatrix(int size) {
GridPane matrixGrid = new GridPane();
StackPane gridHolder = new StackPane(matrixGrid);
scrollPane.setContent(gridHolder);
matrixGrid.setMinHeight(30*size);
matrixGrid.setMinWidth(30*size);
matrixGrid.setMaxHeight(30*size);
matrixGrid.setMaxWidth(50*size);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
TextField tf = new TextField();
Double value = 1.22;
tf.setText(value.toString());
tf.setAlignment(Pos.CENTER);
tf.setEditable(true);
tf.setPrefHeight(Region.USE_COMPUTED_SIZE);
tf.setPrefWidth(Region.USE_COMPUTED_SIZE);
GridPane.setRowIndex(tf, i);
GridPane.setColumnIndex(tf, j);
matrixGrid.getChildren().add(tf);
}
}
//gridHolder.minWidthProperty().bind(Bindings.createDoubleBinding(() ->
//scrollPane.getViewportBounds().getWidth(), scrollPane.viewportBoundsProperty()));
//gridHolder.minHeightProperty().bind(Bindings.createDoubleBinding(() ->
//scrollPane.getViewportBounds().getHeight(), scrollPane.viewportBoundsProperty()));
//matrixBorderPane.setCenter(scrollPane);
matrixBorderPane.setCenter(matrixGrid); //to comment
}
}
,然后我的scrollPane
matrixBorderPane
被放置在GridPane
中。我在这里阅读:How to center the content of a javafx 8 scrollpane matrixGrid
在将它放在中心位置时可能会有所帮助,但它并不像我需要的那样对我有效。
您可以通过取消注释评论行并评论scrollPane
类中标记为StackPane
的行来查看更改结果。
我想要的是,只有当矩阵太大而不适合//to comment
的尺寸时才能激活Controller.java
,如果确实如此,我希望将它放在{scrollPane
的正中心{1}}。
所以我只希望我的matrixBorderPane
位于matrixBorderPane
并且始终位于matrixGrid
的中心,无论矩阵有多大,但是如果它&# 39;太大了,让scrollPane
做它的工作。因此,当矩阵较小时,我希望它看起来与运行具有三条注释行的代码完全相同,并且当矩阵很大但在matrixBorderPane
内时单击scrollPane
(相同代码)后看起来像。