问题描述
我目前正在制作一个小的 JavaFX 应用程序。
在这个应用程序中,我创建了一个 Label 并希望在两个方向上拉伸自己。
Label 放置在 GridPane 中,我尝试了所有我知道的方法,包括 Double.MAX_VALUE
方法,在我看来,它似乎不是很优雅,有时也会以错误的方式工作。下面你会看到我得到的结果:
这是我的代码:
package sample;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
GridPane gp = new GridPane();
gp.setBorder(new Border(new BorderStroke(Color.RED,BorderStrokeStyle.SOLID,CornerRadii.EMPTY,new BorderWidths(3))));
Label label = new Label("Some text that could be longer");
label.setFont(new Font(20));
label.setTextFill(Color.WHITE);
label.setBackground(new Background(new BackgroundFill(Color.rgb(34,35,36),Insets.EMPTY)));
label.setBorder(new Border(new BorderStroke(Color.GREEN,new BorderWidths(3))));
// Commands that don't look like to work
GridPane.setHgrow(label,Priority.ALWAYS);
GridPane.setFillWidth(label,true);
GridPane.setVgrow(label,Priority.ALWAYS);
GridPane.setFillHeight(label,true);
ColumnConstraints cc = new ColumnConstraints();
cc.setFillWidth(true);
cc.setHgrow(Priority.ALWAYS);
cc.setPercentWidth(100);
RowConstraints rc = new RowConstraints();
rc.setFillHeight(true);
rc.setVgrow(Priority.ALWAYS);
rc.setPercentHeight(100);
// end
gp.add(label,0);
gp.getColumnConstraints().add(cc);
gp.getRowConstraints().add(rc);
primaryStage.setScene(new Scene(gp,800,300));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我不知道还能做什么,因为上面的方法都不适合我。在多个示例中,它们甚至没有工作一次。
解决方法
默认情况下,Label
的 maxWidth
设置为 Region.USE_PREF_SIZE
,因此它的宽度不能超过其首选大小。使用
maxWidth
设置为无界
label.setMaxWidth(Double.MAX_VALUE);
在您的其他设置中,您只需将 hgrow
设置为 ALWAYS
,您可以使用静态方法或通过列约束来执行此操作:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
GridPane gp = new GridPane();
gp.setBorder(new Border(new BorderStroke(Color.RED,BorderStrokeStyle.SOLID,CornerRadii.EMPTY,new BorderWidths(3))));
Label label = new Label("Some text that could be longer");
label.setFont(new Font(20));
label.setTextFill(Color.WHITE);
label.setBackground(new Background(new BackgroundFill(Color.rgb(34,35,36),Insets.EMPTY)));
label.setBorder(new Border(new BorderStroke(Color.GREEN,new BorderWidths(3))));
label.setMaxWidth(Double.MAX_VALUE);
GridPane.setHgrow(label,Priority.SOMETIMES);
// Can use the following instead of the previous line:
// ColumnConstraints cc = new ColumnConstraints();
// cc.setHgrow(Priority.ALWAYS);
// gp.getColumnConstraints().add(cc);
gp.add(label,0);
primaryStage.setScene(new Scene(gp,800,300));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}