运行JavaFx程序,但没有什么是空白的,为什么?

时间:2017-11-24 08:03:59

标签: javafx

所以我非常兴奋,以为我终于想出了这个程序,然后BAM启动它=空白屏幕。我在这里打电话给一个空白的窗格,其他信息不是填充的吗?它是否填充但屏幕外?出于某些原因,JavaFX真的很适合我。如果有人也有任何好的地方去阅读它。 (我知道谷歌的作品,很多我发现的网站/地点都不适合我。

package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class Main extends Application {

Button button1, button2;
Label label1, label2;
Scene scene1, scene2;
BorderPane border1, border2;
Stage stage;  

ComboBox<String> choseImage;
ImageView mga1 = new ImageView(new 
Image(getClass().getResourceAsStream("/res/mga1.jpg")));
ImageView mga2 = new ImageView(new 
Image(getClass().getResourceAsStream("/res/mga2.jpg")));
ImageView mga3 = new ImageView(new 
Image(getClass().getResourceAsStream("/res/mga3.jpg")));

@Override
public void start(Stage primaryStage) throws Exception {

    primaryStage.setTitle("multi-factor key");
    primaryStage.setScene(scene1);

    //passPhrase
    TextField passPhrase = new TextField();
    button1 = new Button("submit");
    button1.setOnAction(e -> handleButtonAction(e));
    label1 =new Label ("Type in your PassPhrase");

    border1=new BorderPane();
    border1.getChildren().addAll(passPhrase, button1, label1);
    border1.setPadding(new Insets(20));
    BorderPane.setAlignment(passPhrase, Pos.CENTER);
    BorderPane.setAlignment(button1, Pos.BOTTOM_CENTER);
    BorderPane.setAlignment(label1, Pos.TOP_CENTER);
    scene1 = new Scene(border1, 350, 500);


    //combobox
    choseImage = new ComboBox<>();
    choseImage.setPromptText("Select a image.");
    choseImage.getItems().addAll("mga1.png"
    + "mga2.jpg"
        +   "mga3.png");

    choseImage.setValue("mga1.png");
    label2=new Label("Select a image");
    System.out.println(choseImage.getValue());
    String Selection = choseImage.getValue();
    if (Selection == "mga1.png")
            BorderPane.setAlignment(mga1, Pos.CENTER_RIGHT);
    if (Selection == "mga2.jpg")
            BorderPane.setAlignment(mga2, Pos.CENTER_RIGHT);
    if (Selection == "mga3.jpg");
            BorderPane.setAlignment(mga3, Pos.CENTER_RIGHT);



    border2= new BorderPane();
    border2.setPadding(new Insets(20));
    border2.getChildren().addAll(choseImage,label2);
    BorderPane.setAlignment(choseImage, Pos.CENTER);
    BorderPane.setAlignment(label2, Pos.TOP_CENTER);

    scene2 = new Scene(border2, 350, 500);
    primaryStage.show();




}

private void handleButtonAction(ActionEvent event) {
    if (event.getTarget()==button1)
        stage.setScene(scene1);
}




}

1 个答案:

答案 0 :(得分:1)

启动应用程序时屏幕空白的原因有很多问题

  1. 当您调用primaryStage.setScene(scene1);时,场景为空,因此预计为空白,您应该在实例化之后设置场景。在你的情况下,行:scene1 = new Scene(border1, 350, 500);

  2. 即使您执行上一步操作,屏幕仍然保持空白,因为您拨打了border1.getChildren().addAll(...)这是错误的。 BorderPane有5个区域top bottom left rightcenter,因此您应该将节点设置为此区域,而不是将其添加到borderPane的孩子们。这取决于你想要的地方&#34;看&#34;他们你应该放在正确的地方。您应该执行以下操作:border1.setTop(...) border1.setBottom(...)等等。