英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论