问题描述
我目前正在制作一个小的 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);
}
}
我不知道还能做什么,因为上面的方法都不适合我。在多个示例中,它们甚至没有工作一次。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)