JavaFX Stage不会在Android中显示

时间:2015-06-22 03:09:17

标签: java javafx javafx-2 javafx-8 javafxports

我使用Gluon Plugin在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();
    }

 }

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

1 个答案:

答案 0 :(得分:2)

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

我为NetBeans默认示例修改了Gluon plugin以实现此目的,首先切换场景:

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来管理不同的观点。

看一下这个project让你入门。