调用 setGridLinesVisible(true) 时,GridPane 布局调试行未按预期显示
GridPane layout debugging lines aren't displayed as expected when calling setGridLinesVisible(true)
我试图在 JavaFX 中显示 GridPane 场景的网格线,但尽管调用了 setGridLinesVisible(true)
,它们仍未显示。我做错了什么?
我想在我的程序的主菜单上显示网格线,这样我就可以知道在其上放置节点的位置。不幸的是,当我 运行 程序时,显示的只是一个只有一个按钮的空白屏幕。
我的主菜单Class:
package screens;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
/**
* Creates the main menu Pane object and returns it.
* @author LuminousNutria
*/
public class MainMenu {
public MainMenu() {}
public Pane getPane() {
GridPane grid = new GridPane();
grid.setGridLinesVisible(true);
Button bttn = new Button("button");
grid.add(bttn, 2, 2);
return grid;
}
}
我的主要Class:
package mainPackage;
import screens.*;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
/**
* Displays the Pane.
* @author LuminousNutria
*/
public class Main extends Application {
// create main menu Pane
private Pane mainMenu = new MainMenu().getPane();
// create Scene
private Scene scene = new Scene(mainMenu, 1600, 900);
@Override
public void start(Stage stage) {
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
显示的是:
JavaFX 的 GridPane class 只会创建程序员在设置对象在网格上的位置时定义的点数。
例如,如果将对象添加到网格中,x 和 y 位置都等于 0,则网格将只有一个 (x, y) 位置,即 (0, 0)。
还有,即使网格有很多位置,除非程序员设置了HGap
和Vgap
网格属性,否则网格的所有"positions",都会在程序创建的 window 中的相同点。
问题是我没有调整 HGap
和 VGap
属性,所以整个网格只聚集在 window 中的一个点上。这使得无法看到任何线条。
用下面的代码替换 getPane()
方法让我看到了网格线。
public Pane getPane() {
GridPane grid = new GridPane();
grid.setGridLinesVisible(true);
grid.setVgap(8);
grid.setHgap(8);
Button btn = new Button("button");
grid.add(btn, 5, 5);
return grid;
}
这是我修复程序后显示的内容。
我试图在 JavaFX 中显示 GridPane 场景的网格线,但尽管调用了 setGridLinesVisible(true)
,它们仍未显示。我做错了什么?
我想在我的程序的主菜单上显示网格线,这样我就可以知道在其上放置节点的位置。不幸的是,当我 运行 程序时,显示的只是一个只有一个按钮的空白屏幕。
我的主菜单Class:
package screens;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
/**
* Creates the main menu Pane object and returns it.
* @author LuminousNutria
*/
public class MainMenu {
public MainMenu() {}
public Pane getPane() {
GridPane grid = new GridPane();
grid.setGridLinesVisible(true);
Button bttn = new Button("button");
grid.add(bttn, 2, 2);
return grid;
}
}
我的主要Class:
package mainPackage;
import screens.*;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
/**
* Displays the Pane.
* @author LuminousNutria
*/
public class Main extends Application {
// create main menu Pane
private Pane mainMenu = new MainMenu().getPane();
// create Scene
private Scene scene = new Scene(mainMenu, 1600, 900);
@Override
public void start(Stage stage) {
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
显示的是:
JavaFX 的 GridPane class 只会创建程序员在设置对象在网格上的位置时定义的点数。
例如,如果将对象添加到网格中,x 和 y 位置都等于 0,则网格将只有一个 (x, y) 位置,即 (0, 0)。
还有,即使网格有很多位置,除非程序员设置了HGap
和Vgap
网格属性,否则网格的所有"positions",都会在程序创建的 window 中的相同点。
问题是我没有调整 HGap
和 VGap
属性,所以整个网格只聚集在 window 中的一个点上。这使得无法看到任何线条。
用下面的代码替换 getPane()
方法让我看到了网格线。
public Pane getPane() {
GridPane grid = new GridPane();
grid.setGridLinesVisible(true);
grid.setVgap(8);
grid.setHgap(8);
Button btn = new Button("button");
grid.add(btn, 5, 5);
return grid;
}
这是我修复程序后显示的内容。