簡體   English   中英

控件在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,則可能需要跨多個列擴展該節點。 Java文檔

add(Node child,int columnIndex,int rowIndex,int colspan,int rowpan)在指定的列,行位置和跨度處向網格窗格添加一個子級。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM