控件说 ... 在 JavaFX 中

Controls say ... in JavaFX

我正在尝试增加我对 javafx 的了解,但是 运行 遇到了控件的一些问题。它们通常不够宽并且说 ... 而不是所需的字符串。我试图使用 setWidth 方法,但这不起作用。在这种特定情况下,我指的是选择框。

这是一个标准的javafx程序,我在start方法中完成了这段代码。选择框位于 GridPane 内。这是重现问题的示例代码。

//imports
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage; 
import javafx.scene.Group;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.*;

//main class
public class HelloWorld extends Application {
//main method
public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) {
    //set title for window
    primaryStage.setTitle("Hello World!");
    //create a new button & format it
    Button btn = new Button();
    btn.setText("Say 'Hello World'");
    //give button a set action (print hello world)
    btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            System.out.println("Hello World!");
        }
    });
    //create gridpane to hold button
    GridPane root = new GridPane();

    //establish gridpane
    for(int x = 0; x < 20; x++){
       root.getRowConstraints().add(new RowConstraints(30));
    }
    for(int x = 0; x < 30; x++){
       root.getColumnConstraints().add(new ColumnConstraints(20));
    }
    //set constraints, add button to gridpane
    root.setConstraints(btn,3,3);
    root.getChildren().add(btn);
    //set scene and show
    primaryStage.setScene(new Scene(root, 300, 250));
    primaryStage.show();
 }
}

如果您要将此节点添加到 GridPane,您可能需要跨多个列扩展该节点。 JavaDocs

add(Node child, int columnIndex, int rowIndex, int colspan, int rowspan) Adds a child to the gridpane at the specified column,row position and spans.

gridPane.add(accounts, 0, 0, 2, 1);