java fx gridpane setpadding

时间:2015-10-22 19:24:01

标签: javafx gridpane insets

我正在尝试为我的GridPane设置填充,但每次设置填充时它都会给出错误消息:

incompatible types: java.awt.Insets cannot be converted to javafx.geometry.insets

我去的每个网站和每个搜索都声明如下所示:

grid.setPadding(new Insets(10, 10, 10, 10));

在我的代码中,我将它设置为完全相同,仅用于测试目的:

grid.setPadding(new Insets(10, 10, 10, 10));

所以我不明白为什么它不允许我写这个,我也试图研究错误信息,但没有任何结果。

这是我的代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package myrectangle2d;

import java.awt.Insets;
import javafx.application.Application;
import javafx.event.ActionEvent;

import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author macuser
 */
public class Exercise18_29 extends Application {

    Stage window;
    Label label1, label2, centerXL, centerXR;
    TextField textCenterXL, textCenterXR;

    public static void main(String[] args) {
        launch(args);
    }


    @Override
    public void start(Stage primaryStage) {

        window = primaryStage;
        window.setTitle("Triangles");



        GridPane grid = new GridPane();
        grid.setPadding(new Insets(10, 10, 10, 10));

       label1 = new Label("Do the Triangles match");
       GridPane.setConstraints(label1, 0, 0);


       label2 = new Label("Yes or no");
       GridPane.setConstraints(label2, 1, 0);


       centerXL = new Label("Center X LEFT");
       GridPane.setConstraints(centerXL, 0, 5);

       textCenterXL = new TextField();
       GridPane.setConstraints(textCenterXL, 1, 5);


       centerXR = new Label("Center X RIGHT");
       GridPane.setConstraints(centerXL, 0, 5);

       textCenterXR = new TextField();
       GridPane.setConstraints(textCenterXR, 3, 5);

       grid.getChildren().addAll(label1, label2, centerXL, textCenterXL, centerXR, textCenterXR);

       Scene scene = new Scene(grid, 400, 600);


       window.setScene(scene);



        window.show();

    }




}

2 个答案:

答案 0 :(得分:4)

错误消息告诉您需要知道的一切:

  

不兼容的类型:java.awt.Insets无法转换为javafx.geometry.insets

您已导入java.awt.Insets :(代码中的第一个导入行),您需要javafx.geometry.Insets

答案 1 :(得分:-1)

上面的答案很好。你需要

elevation

代替

import javafx.geometry.Insets;

当我允许 IDE 自动导入必要的库时,我第一次生成了错误。有时捷径会咬人。