在2个不同场景中的一个类中获取2个GridPanel
我想使用网格窗格创建单独的场景。我试图基本上复制所有的代码并创建一个新的场景,但是我放在第二个场景中的所有内容都不会捕捉到网格。我相当新编码,这是我的第五个项目。 (对不起的代码抱歉)在2个不同场景中的一个类中获取2个GridPanel
import javafx.application.Application; import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Main extends Application {
Stage window;
Scene s1, s2;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("Test");
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(10);
grid.setHgap(10);
Button b1 = new Button("Scene 2");
b1.setOnAction(e -> window.setScene(s2));
GridPane.setConstraints(b1, 2, 0);
Button b2 = new Button("Button 2");
GridPane.setConstraints(b2, 5, 0);
grid.getChildren().addAll(b1, b2);
Scene s1 = new Scene(grid, 500, 500);
GridPane grid2 = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(10);
grid.setHgap(10);
Button b3 = new Button("Scene 1");
b3.setOnAction(e -> window.setScene(s1));
GridPane.setConstraints(b3, 0, 0);
Button b4 = new Button("Button 2");
GridPane.setConstraints(b4, 6, 0);
grid2.getChildren().addAll(b3, b4);
s2 = new Scene(grid2, 500, 500);
window.setScene(s1);
window.show();
}
}
回答:
代码中存在复制粘贴错误。
GridPane grid2 = new GridPane(); grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(10);
grid.setHgap(10);
大概来自复制上面的代码,其中填充和间隙应用到第一个网格窗格。将其应用于第二网格窗格,它应该是
GridPane grid2 = new GridPane(); grid2.setPadding(new Insets(10, 10, 10, 10));
grid2.setVgap(10);
grid2.setHgap(10);
以上是 在2个不同场景中的一个类中获取2个GridPanel 的全部内容, 来源链接: utcz.com/qa/260454.html