javafx - 在场景中不显示任何按钮
问题描述
它是一个简单的 javafx 程序,将在两个场景之间切换。程序编译得很好,但没有在场景中显示任何组件。我使用了两种布局、两个按钮和两个场景。假设所有必要的包都已导入。源代码:github.com/tmtanzeel/javafx/Program5.java
public class Program5 extends Application {
Button button1;
Button button2;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
button1=new Button();
button2=new Button();
button1.setText("Yes");
button2.setText("No");
StackPane layout1=new StackPane();
layout1.getChildren().add(button1);
StackPane layout2=new StackPane();
layout2.getChildren().add(button1);
Scene scene1=new Scene(layout1, 450,250);
Scene scene2=new Scene(layout2, 250, 450);
button1.setOnAction(e -> {
primaryStage.setScene(scene1);
});
button2.setOnAction(e -> {
primaryStage.setScene(scene2);
});
primaryStage.setScene(scene1);
primaryStage.setTitle("Window-1");
primaryStage.show();
}
}
解决方案
您有一些语法错误,但是这可以工作,您之前设置了相同的场景,所以它会按照它被告知的方式切换到相同的场景,并且您只需将按钮 1 添加到两个屏幕
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Button button1 = new Button();
Button button2 = new Button();
button1.setText("Yes");
button2.setText("No");
StackPane layout1 = new StackPane();
layout1.getChildren().add(button1);
StackPane layout2 = new StackPane();
layout2.getChildren().add(button2); //This should be button2
Scene scene1 = new Scene(layout1, 450, 250);
Scene scene2 = new Scene(layout2, 250, 450);
button1.setOnAction(e -> primaryStage.setScene(scene2)); //You set the wrong scene here
button2.setOnAction(e -> primaryStage.setScene(scene1)); //And here
primaryStage.setScene(scene1);
primaryStage.setTitle("Window-1");
primaryStage.show();
}
}
推荐阅读
- html - React 按钮单击事件未触发 - 一键有效,一键无效
- xml - 将 XML 导入 Excel 时,是否可以在一行中导入节点及其子节点?如果是,怎么做?
- php - 除了 1 个 PHP 变量,来自 $_POST 的所有其他数据都在获取
- javascript - 如何在 Primereact 数据列中添加图像
- reactjs - React Mapbox Layer:为标记显示不同的图像
- r - R dplyr:通过向量定义的多个正则表达式过滤数据
- objective-c - 本机链接失败。请查看 Xamarin.iOS 跨平台中的构建日志
- kubernetes - Coredns 不时解决
- android - 如何将高级动画添加到我的移动应用程序?
- oracle - 我需要 quantity_sold 的总和小于 1000,并使 PRODUCT_NAME 在每个包含“-”的产品名称后都有符号