如何打开新阶段并关闭课程之间的前一阶段

时间:2014-06-30 11:12:20

标签: java javafx-2 javafx-8 stage

我尝试打开新阶段并关闭课程之间的前一阶段,但我无法在javafx中取得成功。也许如果我们使用单例模式,它可以成功运行。按下按钮时,第一个窗口将关闭,第二个窗口将打开。

这是代码;

头等舱:

 public class First extends Application {

   public  Stage primaryStage2,primaryStage3;

  Second v=new Second();

     @Override
   public void start(Stage primaryStage) {


Button btn = new Button();
btn.setText("Press");

this.primaryStage3=primaryStage;

btn.setOnAction(new EventHandler<ActionEvent>() {

    @Override
    public void handle(ActionEvent event) {

      v.open();
    }
   });

StackPane root = new StackPane();
root.getChildren().add(btn);      
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
 }


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


         }

第二课:

   public class Second {

 Stage  primaryStage2;

   First a=new First();

 public void open(){

  primaryStage2=new Stage();
 StackPane root = new StackPane();              
 Scene scene = new Scene(root, 300, 250);       
primaryStage2.setScene(scene);
 a.primaryStage3.close();
primaryStage2.show();
 }
}

4 个答案:

答案 0 :(得分:0)

为什么要创建新舞台?您可以根据需要轻松创建不同的场景。并根据需要更改场景。

  

尝试以下代码

主要课程

package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
public class Main extends Application 
{    
BorderPane root = new BorderPane();
Button btn=new Button("Click Here");
Stage primaryStage;
Scene scene ;
public Main()
{
    btn.setPrefWidth(75);
    btn.setPrefHeight(15);

    btn.setOnAction((e)->
    {
        SubClass s=new SubClass(primaryStage);
    });
}

@Override
public void start(Stage primaryStage)
{
    try {
        this.primaryStage=primaryStage;
        //          root.getChildren().add(btn);

        scene = initStage();
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e)
    {
        e.printStackTrace();
    }
}

private Scene initStage()
{
    root.setCenter(btn);
    return new Scene(root,150,150);
}


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

子类

package application;

import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class SubClass
{

StackPane root = new StackPane();
Stage stage;

public SubClass(Stage stage)
{
    Scene scene=new Scene(root,300,300);
    stage.setScene(scene);
    stage.show();
}
}
  

我建议你尝试改变场景而不是舞台。一切顺利

答案 1 :(得分:0)

尝试登录并打开一个新窗口

   package signIn;
  import javafx.application.Application;
  import javafx.event.ActionEvent;
  import javafx.event.EventHandler;
  import javafx.geometry.Insets;
  import javafx.geometry.Pos;
  import javafx.scene.Scene;
  import javafx.scene.control.Alert;
  import javafx.scene.control.Alert.AlertType;
  import javafx.scene.control.Button;
  import javafx.scene.control.Label;
  import javafx.scene.control.TextField;
  import javafx.scene.layout.StackPane;
  import javafx.scene.layout.VBox;
  import javafx.stage.Stage;

  public class JavaFXWindowDemo extends Application {

  private String loggedInUser;

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

  // How to open a new window in JavaFX
  @Override
  public void start(Stage primaryStage) throws Exception {
  Button btnLogin = new Button();
  btnLogin.setText("Login");
  btnLogin.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        showLoginScreen();
    }
 });
 // A layout container for UI controls
 StackPane root = new StackPane();
 root.getChildren().add(btnLogin);

 // Top level container for all view content
 Scene scene = new Scene(root, 300, 250);

// primaryStage is the main top level window created by platform
primaryStage.setTitle("JavaFX Demo");
primaryStage.setScene(scene);
primaryStage.show();
}

public void setLoggedInUser(String user) {
loggedInUser = user;

Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Successful login");
alert.setHeaderText("Successful login");
String s = user + " logged in!";
alert.setContentText(s);
alert.show();
}

public void showLoginScreen() {
Stage stage = new Stage();

VBox box = new VBox();
box.setPadding(new Insets(10));

// How to center align content in a layout manager in JavaFX
box.setAlignment(Pos.CENTER);

Label label = new Label("Enter username and password");

TextField textUser = new TextField();
textUser.setPromptText("enter user name");
TextField textPass = new TextField();
textPass.setPromptText("enter password");

Button btnLogin = new Button();
btnLogin.setText("Login");

btnLogin.setOnAction(new EventHandler<ActionEvent>() {

    @Override
    public void handle(ActionEvent event) {
        // Assume success always!
        setLoggedInUser(textUser.getText());
        stage.close(); // return to main window
    }
});

box.getChildren().add(label);
box.getChildren().add(textUser);
box.getChildren().add(textPass);
box.getChildren().add(btnLogin);
Scene scene = new Scene(box, 250, 150);
stage.setScene(scene);
stage.show();
 }
}

答案 2 :(得分:0)

public void get(ActionEvent actionEvent) throws IOException {

       Parent home_page = FXMLLoader.load(getClass().getResource("anotherpage.fxml"));
        Stage app = (Stage)((Node) actionEvent.getSource()).getScene().getWindow();
        app.setScene(new Scene(home_page,300, 275));
        app.show();
}

答案 3 :(得分:-1)

你的问题似乎在这里:

 public class Second {

 Stage  primaryStage2;

 First a=new First();

 public void open(){

  primaryStage2=new Stage();
 StackPane root = new StackPane();              
 Scene scene = new Scene(root, 300, 250);       
primaryStage2.setScene(scene);
 a.primaryStage3.close();
primaryStage2.show();
 }
}

在那里你创建了一个新的First然后首先获得该NEW的阶段并关闭它,也就是说,之前打开的那个,它实际上是另一个对象。

你的行动可能需要做更多这样的事情:

btn.setOnAction(new EventHandler<ActionEvent>() {

    @Override
    public void handle(ActionEvent event) {

      v.open();
primaryStage3.close(); // new LINE!!
    }
   });

也就是说,从按钮动作关闭舞台,而不是显示另一个窗口,因为这会导致两个对象不必要地耦合。

我还建议使用不同的类来处理应用程序本身以及要显示的窗口。现在这不是问题,但是当你的应用程序规模增长时,它将变成一个。 当为每个不同的窗口创建一个类时,这些窗口中的每一个都可以保存它的阶段,正如你的第二个类没有做的那样,但不会在Application类中不必要地重复这些引用。

相关问题