Javafx 无法在按钮单击事件上调用另一个fxml页
我试图想出一个简单的工具,我想调用另一个fxml页面上的按钮点击从目前的fxml页面。 我的主要Java类设计如下:Javafx 无法在按钮单击事件上调用另一个fxml页,javafx,fxml,Javafx,Fxml,我试图想出一个简单的工具,我想调用另一个fxml页面上的按钮点击从目前的fxml页面。 我的主要Java类设计如下: import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.stage.Stage; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.TitledPane;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.BorderPane;
public class Main extends Application
{
@Override
public void start(Stage stage) {
try {
TitledPane page = (TitledPane)FXMLLoader.load(getClass().getResource("NewTest.fxml"));
Scene scene = new Scene(page);
stage.setScene(scene);
stage.setTitle("Welome Page");
stage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Application.launch(Main.class, (java.lang.String[])null);
}}
控制器类设计为:
import javafx.event.ActionEvent;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TitledPane;
import javafx.stage.Stage;
public class SimpleController implements Initializable
{
@FXML // fx:id="loginbtn"
private Button loginbtn; // Value injected by FXMLLoader
@Override // This method is called by the FXMLLoader when
initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
assert loginbtn != null : "fx:id=\"myButton\" was not injected: check your FXML file 'simple.fxml'.";
// initialize your logic here: all @FXML variables will have been injected
loginbtn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
Stage stage;
Parent root;
if(event.getSource()==loginbtn){
//get reference to the button's stage
stage=(Stage) loginbtn.getScene().getWindow();
//load up OTHER FXML document
try{
TitledPane page =(TitledPane) FXMLLoader.load(getClass().getResource("secondPage.fxml"));
}catch(Exception e)
{e.printStackTrace();}
}
}
});
}
}
导入javafx.event.ActionEvent;
导入java.net.URL;
导入java.util.ResourceBundle;
导入javafx.event.EventHandler;
导入javafx.fxml.fxml;
导入javafx.fxml.fxmloader;
导入javafx.fxml.Initializable;
导入javafx.scene.Parent;
导入javafx.scene.control.Button;
导入javafx.scene.control.TitledPane;
导入javafx.stage.stage;
公共类SimpleControl实现了可初始化
{
@FXML//fx:id=“loginbtn”
私有按钮loginbtn;//FXMLLoader注入的值
@Override//FXMLLoader在
初始化完成
公共void初始化(URL fxmlFileLocation,ResourceBundle资源){
assert loginbtn!=null:“fx:id=\“myButton\”未被注入:请检查FXML文件“simple.FXML”;
//在此处初始化逻辑:所有@FXML变量都将被注入
loginbtn.setOnAction(新的EventHandler(){
公共无效句柄(ActionEvent事件){
阶段性;
亲本根;
if(event.getSource()==loginbtn){
//获取对按钮的阶段的引用
stage=(stage)loginbtn.getScene().getWindow();
//加载其他FXML文档
试一试{
标题窗格页面=(标题窗格)FXMLLoader.load(getClass().getResource(“secondPage.fxml”);
}捕获(例外e)
{e.printStackTrace();}
}
}
});
}
}
Fxml页面如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<TitledPane text="Welcome to the Vacation Planner" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
<content>
<AnchorPane prefHeight="330.0" prefWidth="497.0">
<children>
<Label layoutX="14.0" layoutY="52.0" prefHeight="25.0" prefWidth="189.0" text="User Name">
<font>
<Font size="24.0" />
</font>
</Label>
<TextArea layoutX="53.0" layoutY="106.0" prefHeight="41.0" prefWidth="391.0" />
<Label layoutX="14.0" layoutY="165.0" text="Password">
<font>
<Font size="24.0" />
</font>
</Label>
<Button fx:id="loginbtn" layoutX="114.0" layoutY="262.0" mnemonicParsing="false" text="Login">
<font>
<Font size="18.0" />
</font>
</Button>
<Button fx:id="exitbtn" layoutX="271.0" layoutY="262.0" mnemonicParsing="false" text="Exit">
<font>
<Font size="18.0" />
</font>
</Button>
<PasswordField layoutX="53.0" layoutY="207.0" prefHeight="39.0" prefWidth="391.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
当我单击按钮登录时,它并没有打开我试图在单击按钮时打开的第二个xml页面
提前感谢。您加载
节点,然后对其不做任何操作
您需要将它们添加到现有场景或替换该场景:
try{
TitledPane page =(TitledPane) FXMLLoader.load(getClass().getResource("secondPage.fxml"));
Scene newScene = new Scene(page);
stage.setScene(newScene);
} catch(Exception e) {
e.printStackTrace();
}
此外,您还没有将控制器与fxml关联。例如,使用fx:controller
属性,将控制器类的完全限定类名放在fxml的根目录下,以执行以下操作:
其中my.package
将是包含控制器的包。您加载节点,然后对其不做任何操作
您需要将它们添加到现有场景或替换该场景:
try{
TitledPane page =(TitledPane) FXMLLoader.load(getClass().getResource("secondPage.fxml"));
Scene newScene = new Scene(page);
stage.setScene(newScene);
} catch(Exception e) {
e.printStackTrace();
}
此外,您还没有将控制器与fxml关联。例如,使用fx:controller
属性,将控制器类的完全限定类名放在fxml的根目录下,以执行以下操作:
其中,my.package
将是包含控制器的包。Hi,我尝试向我们提供您建议的代码,但它没有打开我试图打开的fxml页面。@priya89我没有看到您在任何地方都没有使用控制器类;现在编辑答案。。。顺便说一句:我假设控制器进行编译,而不是导致编译时异常,因为初始化已完成
不在commentsHi中,所以我尝试使用您建议的代码,但它没有打开我试图打开的fxml页面。@priya89我没有看到您在任何地方都没有使用控制器类;现在编辑答案。。。顺便说一句:我假设控制器进行编译,而不是导致编译时异常,因为初始化已完成
不在注释中