2

我已经使用 Gluon 插件在 Android 中部署了一个 JavaFX 应用程序。我现在想做的是显示第二个阶段,但不幸的是它不会显示。

这是第二阶段的代码:

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Setting {

    public Setting(){
        Text title = new Text("Settings");
        title.setId("setting-title");
        title.setFont(Font.font("Arial", (int)(MainApp.HEIGHT * 0.04)));

        Label label_url = new Label("URL");
        label_url.setFont(Font.font("Arial", (int)(MainApp.HEIGHT * 0.03)));

        Label label_style = new Label("Style");
        label_style.setFont(Font.font("Arial", (int)(MainApp.HEIGHT * 0.03)));

        TextField text_url = new TextField(MainApp.URL);
        text_url.setFont(Font.font("Arial", (int)(MainApp.HEIGHT * 0.03)));

        ComboBox style_box = new ComboBox();
        style_box.setStyle("-fx-font-size:"+(int)(MainApp.HEIGHT * 0.03)+"px;");

        ObservableList<String> data = FXCollections.observableArrayList();
        data.add("Blue");
        data.add("Red");
        data.add("Yellow");
        style_box.setItems(data);

        GridPane grid = new GridPane();
        grid.setVgap(5);
        grid.setHgap(5);
        grid.add(label_url, 0, 0);
        grid.add(label_style, 0, 1);
        grid.add(text_url, 1, 0);
        grid.add(style_box, 1, 1);

        VBox root = new VBox();
        root.setAlignment(Pos.CENTER);
        root.getChildren().addAll(title,grid);

        Scene scene = new Scene(root, MainApp.WIDTH * 0.6, MainApp.HEIGHT * 0.4);

        if(MainApp.SETTING == null){
            MainApp.SETTING = new Stage();
            MainApp.SETTING.setScene(scene);
            MainApp.SETTING.initOwner(MainApp.MAINSTAGE);
            MainApp.SETTING.setTitle("Settings");
        }
    }

    public void show(){
        if(!MainApp.SETTING.isShowing()) MainApp.SETTING.show();
    }

 }

当我尝试将其作为桌面应用程序运行时,此代码有效。我使用的是安卓 4.2。

4

1 回答 1

2

使用 JavaFXPorts,您可以在 Android 上运行时更改场景和阶段。

我已经修改了 NetBeans 默认示例的 Gluon插件来实现这一点,首先切换场景:

private Scene mainScene;

@Override
public void start(Stage stage) {
    Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();

    Label label = new Label("Main Scene");        
    StackPane root = new StackPane(label);
    mainScene = new Scene(root, visualBounds.getWidth(), visualBounds.getHeight());

    stage.setScene(mainScene);
    stage.show();

    label.setOnMouseClicked(e->{
        Label labelSettings = new Label("Settings Scene. Click to go back");
        StackPane rootSettings = new StackPane(labelSettings);
        Scene settingsScene = new Scene(rootSettings, visualBounds.getWidth(), visualBounds.getHeight());
        stage.setScene(settingsScene);
        labelSettings.setOnMouseClicked(t->stage.setScene(mainScene));
    });
}

现在切换阶段:

private Scene mainScene;

@Override
public void start(Stage stage) {
    Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();

    Label label = new Label("Main Stage");        
    StackPane root = new StackPane(label);
    root.setStyle("-fx-background-color: aquamarine;");
    mainScene = new Scene(root, visualBounds.getWidth(), visualBounds.getHeight());

    stage.setScene(mainScene);
    stage.setTitle("Main Stage");
    stage.show();

    label.setOnMouseClicked(e->{
        Label labelSettings = new Label("Settings Stage. Click to go back");
        StackPane rootSettings = new StackPane(labelSettings);
        rootSettings.setStyle("-fx-background-color: burlywood;");
        Scene settingsScene = new Scene(rootSettings, visualBounds.getWidth(), visualBounds.getHeight());
        Stage stage2 = new Stage();
        stage2.setScene(settingsScene);
        stage2.show();
        labelSettings.setOnMouseClicked(t->stage2.hide());
    });
}

还有另一种可能性:您可以使用 Gluon Charm来管理不同的视图。

看看这个项目让你开始。

于 2015-06-22T08:38:38.273 回答