为什么按钮没有出现在框架中?

时间:2015-12-18 00:37:22

标签: java javafx

我正在制作一个简单的程序来切换JavaFX中的场景。我创建了我的按钮对象,将它们添加到布局中,将布局添加到场景中,但由于某种原因,按钮根本没有出现。标签出现了。这是我的代码:

package switching.scenes;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SwitchingScenes extends Application {

private Scene scene1, scene2, scene3;
private Button button1, button2, button3;
private Label label1, label2, label3;
private VBox layout1, layout2, layout3;
private Stage frame;

@Override
public void start(Stage primaryStage) {

    frame = primaryStage;

    // Create buttons
    button1 = new Button("Go to scene 1");
    button2 = new Button("Go to scene 2");
    button3 = new Button("Go to scene 3");

    // Button action listener
    button1.setOnAction(e -> frame.setScene(scene1));
    button2.setOnAction(e -> frame.setScene(scene2));
    button3.setOnAction(e -> frame.setScene(scene3));

    // Create labels
    label1 = new Label("You are at scene 1");
    label2 = new Label("You are at scene 2");
    label3 = new Label("You are at scene 3");

    // Create layouts
    layout1 = new VBox(20);
    layout2 = new VBox(20);
    layout3 = new VBox(20);

    // Add elements to each layout
    layout1.getChildren().addAll(label1, button2, button3);
    layout2.getChildren().addAll(label2, button1, button3);
    layout3.getChildren().addAll(label3, button1, button2);

    // Create scenes
    scene1 = new Scene(layout1, 400,400);
    scene2 = new Scene(layout2, 400,400);
    scene3 = new Scene(layout3, 400,400);

    frame.setScene(scene1);
    frame.setTitle("Title");
    frame.show();
}

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

 }

1 个答案:

答案 0 :(得分:0)

为每个场景创建唯一按钮节点。检查以下代码

package switching.scenes;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SwitchingScenes extends Application {

    private Scene scene1, scene2, scene3;
    private Button button2_1, button3_1,button1_2, button3_2,button1_3, button2_3;
    private Label label1, label2, label3;
    private VBox layout1, layout2, layout3;
    private Stage frame;

    @Override
    public void start(Stage primaryStage) {

        frame = primaryStage;

        // Create buttons
        button1_2 = new Button("Go to scene 1");button1_3 = new Button("Go to scene 1");
        button2_1 = new Button("Go to scene 2");button2_3 = new Button("Go to scene 2");
        button3_1 = new Button("Go to scene 3");button3_2 = new Button("Go to scene 3");

        // Button action listener
        button1_2.setOnAction(e -> frame.setScene(scene1));button1_3.setOnAction(e -> frame.setScene(scene1));
        button2_1.setOnAction(e -> frame.setScene(scene2));button2_3.setOnAction(e -> frame.setScene(scene2));
        button3_1.setOnAction(e -> frame.setScene(scene3));button3_2.setOnAction(e -> frame.setScene(scene3));

        // Create labels
        label1 = new Label("You are at scene 1");
        label2 = new Label("You are at scene 2");
        label3 = new Label("You are at scene 3");

        // Create layouts
        layout1 = new VBox(20);
        layout2 = new VBox(20);
        layout3 = new VBox(20);

        // Add elements to each layout
        layout1.getChildren().addAll(label1, button2_1, button3_1);
        layout2.getChildren().addAll(label2, button1_2, button3_2);
        layout3.getChildren().addAll(label3, button1_3, button2_3);

        // Create scenes
        scene1 = new Scene(layout1, 400,400);
        scene2 = new Scene(layout2, 400,400);
    scene3 = new Scene(layout3, 400,400);

        frame.setScene(scene1);
        frame.setTitle("Title");
        frame.show();
    }

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

}

我使用了一个下划线" _"代表场景编号。希望有用。