【问题标题】:How to open new stage and close previous stage between classes如何在课堂之间打开新阶段和关闭前一阶段
【发布时间】:2014-08-20 17:56:55
【问题描述】:

我尝试在课程之间打开新阶段并关闭前一个阶段,但我无法在 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();
 }
}

【问题讨论】:

  • 我不推荐使用singleton,只在非常特殊的情况下,并且没有其他解决方案时,我使用这种模式。
  • 请缩进代码

标签: java javafx-2 javafx-8 stage


【解决方案1】:

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

试试下面的代码

主类

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();
}
}

我建议你尝试改变场景而不是舞台。万事如意

【讨论】:

    【解决方案2】:

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

       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();
     }
    }
    

    【讨论】:

      【解决方案3】:
      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();
      }
      

      【讨论】:

      • 这将帮助你兄弟只需给按钮提供方法然后当你点击它时会打开另一个页面(与场景构建器 fxml 一起使用)
      【解决方案4】:

      您的问题似乎在这里:

       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!!
          }
         });
      

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

      我还建议为应用程序本身以及您要显示的窗口使用不同的类。现在这不是问题,但是当你的应用程序变大时,它会变成一个。 当每个不同的窗口都有一个类时,它们中的每一个都可以保持第三阶段,就像您的 Second 类不正确一样,但不会在 Application 类中不必要地重复这些引用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-25
        • 1970-01-01
        • 2014-09-12
        • 2014-04-16
        • 2015-02-04
        • 1970-01-01
        相关资源
        最近更新 更多