按钮未出现在 GridPane JavaFX8 中
Buttons not appearing in GridPane JavaFX8
我在 GridPane 中使用它们之前先声明按钮,这样我就可以使用 button.setOnAction() 函数来切换场景,但按钮不会出现。
当我在 gridpane.add() 函数中创建按钮时,它工作得很好。我试过使用 BorderPanes 来代替 EventHandler 的非 lambda 版本。
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Main extends Application {
public static Scene scene1;
public static Scene scene2;
public static Scene scene3;
public static Scene[] scenes = new Scene[]{null, scene1, scene2, scene3};
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Scene Switcher");
GridPane gp1 = new GridPane();
GridPane gp2 = new GridPane();
GridPane gp3 = new GridPane();
Button button1 = new Button("scene 1");
button1.setOnAction(event -> primaryStage.setScene(scene1));
Button button2 = new Button("scene 2");
button2.setOnAction(event -> primaryStage.setScene(scene2));
Button button3 = new Button("scene 3");
button3.setOnAction(event -> primaryStage.setScene(scene3));
//
// SCENE 1
//
Label l1 = new Label("this is scene 1");
BorderPane.setAlignment(l1, Pos.CENTER);
gp1.add(l1, 0, 0);
gp1.add(button2, 0, 2);
gp1.add(button3, 0, 3);
scene1 = new Scene(gp1, 500, 500);
//
// SCENE 2
//
Label l2 = new Label("this is scene 2");
BorderPane.setAlignment(l2, Pos.CENTER);
gp2.add(l2, 0, 0);
gp2.add(button1, 0, 2);
gp2.add(button3, 0, 3);
scene2 = new Scene(gp2, 500, 500);
//
// SCENE 3
//
Label l3 = new Label("this is scene 3");
BorderPane.setAlignment(l3, Pos.CENTER);
gp3.add(l3, 0, 0);
gp3.add(button1, 0, 2);
gp3.add(button2, 0, 3);
scene3 = new Scene(gp3, 500, 500);
primaryStage.setScene(scene1);
primaryStage.show();
}
}
按钮根本不显示。
您的 Button
个节点一次只能属于一个 GridPane
。在这里,您在将它们添加到 gp1
之后将它们添加到 gp2
和 gp3
。
您需要重新考虑您对此的设计 Stage
,因为您尝试这样做的方式是不可能的。
您可以通过注释掉场景 2 和 3 的所有代码来确认这一点。按钮将按预期显示。
我在 GridPane 中使用它们之前先声明按钮,这样我就可以使用 button.setOnAction() 函数来切换场景,但按钮不会出现。
当我在 gridpane.add() 函数中创建按钮时,它工作得很好。我试过使用 BorderPanes 来代替 EventHandler 的非 lambda 版本。
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Main extends Application {
public static Scene scene1;
public static Scene scene2;
public static Scene scene3;
public static Scene[] scenes = new Scene[]{null, scene1, scene2, scene3};
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Scene Switcher");
GridPane gp1 = new GridPane();
GridPane gp2 = new GridPane();
GridPane gp3 = new GridPane();
Button button1 = new Button("scene 1");
button1.setOnAction(event -> primaryStage.setScene(scene1));
Button button2 = new Button("scene 2");
button2.setOnAction(event -> primaryStage.setScene(scene2));
Button button3 = new Button("scene 3");
button3.setOnAction(event -> primaryStage.setScene(scene3));
//
// SCENE 1
//
Label l1 = new Label("this is scene 1");
BorderPane.setAlignment(l1, Pos.CENTER);
gp1.add(l1, 0, 0);
gp1.add(button2, 0, 2);
gp1.add(button3, 0, 3);
scene1 = new Scene(gp1, 500, 500);
//
// SCENE 2
//
Label l2 = new Label("this is scene 2");
BorderPane.setAlignment(l2, Pos.CENTER);
gp2.add(l2, 0, 0);
gp2.add(button1, 0, 2);
gp2.add(button3, 0, 3);
scene2 = new Scene(gp2, 500, 500);
//
// SCENE 3
//
Label l3 = new Label("this is scene 3");
BorderPane.setAlignment(l3, Pos.CENTER);
gp3.add(l3, 0, 0);
gp3.add(button1, 0, 2);
gp3.add(button2, 0, 3);
scene3 = new Scene(gp3, 500, 500);
primaryStage.setScene(scene1);
primaryStage.show();
}
}
按钮根本不显示。
您的 Button
个节点一次只能属于一个 GridPane
。在这里,您在将它们添加到 gp1
之后将它们添加到 gp2
和 gp3
。
您需要重新考虑您对此的设计 Stage
,因为您尝试这样做的方式是不可能的。
您可以通过注释掉场景 2 和 3 的所有代码来确认这一点。按钮将按预期显示。