切换JavaFX中一个包的场景到另一个包。

huangapple go评论72阅读模式
英文:

Switch scenes from one package to another package javafx

问题

我有两个包p1和p2。

P1包含主类、控制器类和一个fxml文件。
P2包含控制器类和一个fxml文件。

我想要从p1的fxml切换到p2的fxml文件。

这是我尝试的代码。这在P1包中。

public void btncontinue(ActionEvent event)throws IOException {
    String filepath = "file:///D:/Programs/InteliJProjects/C/src/p1/sample2.fxml";
    Parent nextScene = FXMLLoader.load(getClass().getClassLoader().getResource(filepath));
    Scene scene = new Scene(nextScene);
    Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    stage.setScene(scene);
    stage.show();
}

我得到的错误是需要指定位置(Location is required)。

英文:

I have two packages p1 and p2.

P1 contains the main class, controller class and one fxml file.
P2 contains the controller class and one fxml file.

I want to switch from p1 fxml to p2 fxml file.

here is the code I tried. this is in P1 package.

public void btncontinue(ActionEvent event)throws IOException {
        String filepath = "file:///D:/Programs/InteliJProjects/C/src/p1/sample2.fxml";
        Parent nextScene = FXMLLoader.load(getClass().getClassLoader().getResource(filepath));
        Scene scene = new Scene(nextScene);
        Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();
        stage.setScene(scene);
        stage.show();
    }

The error i am getting is Location is required.

答案1

得分: 0

这是从场景1到场景2的代码:

 package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;

import java.io.IOException;

public class sample
{

    @FXML
    RadioButton male;

    @FXML
    AnchorPane rootPane;


    public void add() throws IOException
    {
        AnchorPane pane = FXMLLoader.load(getClass().getResource("two.fxml"));
        rootPane.getChildren().setAll(pane);
    }

}

这是从场景2返回场景1的方法:

package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;

import java.io.IOException;

public class Two
{
    @FXML
    Button back;

    @FXML
    AnchorPane secPane;

    public void returnBack() throws IOException
    {
        AnchorPane pane = FXMLLoader.load(getClass().getResource("sample.fxml"));
        secPane.getChildren().setAll(pane);
    }
}

我已经尝试过这个方法,它正常工作,希望能帮助到你。

英文:

Well I've checked this is the code from scene1 to scene2

 package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;


import java.io.IOException;

public class sample
{

    @FXML
    RadioButton male;

    @FXML
    AnchorPane rootPane;


    public void add() throws IOException
    {
        AnchorPane pane = FXMLLoader.load(getClass().getResource("two.fxml"));
        rootPane.getChildren().setAll(pane);
    }

}

And this is how to return from scene2 to scene1

package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;

import java.io.IOException;

public class Two
{
    @FXML
    Button back;

    @FXML
    AnchorPane secPane;

    public void returnBack() throws IOException
    {
        AnchorPane pane = FXMLLoader.load(getClass().getResource("sample.fxml"));
        secPane.getChildren().setAll(pane);
    }
}

I've tried this and it is working fine hope it will help you

huangapple
  • 本文由 发表于 2020年8月8日 18:06:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63314153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定