JavaFX8打开按钮单击“显示其他场景”
您好,我目前正在为我的工作开发一个PIN生成器,由于我对Java完全陌生,我遇到了一些困难,尤其是在使用JavaFX时。 我想让程序在单击每个按钮时显示另一个.fxml文件 这是我的密码:JavaFX8打开按钮单击“显示其他场景”,java,javafx-8,Java,Javafx 8,您好,我目前正在为我的工作开发一个PIN生成器,由于我对Java完全陌生,我遇到了一些困难,尤其是在使用JavaFX时。 我想让程序在单击每个按钮时显示另一个.fxml文件 这是我的密码: import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; pub
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class main extends Application {
public static void main(String[] args) {
Application.launch(main.class, args);
}
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));
stage.setTitle("PIN-Generator");
stage.setScene(new Scene(root, 600, 400));
stage.show();
}
}
我已经为场景中的每个按钮创建了一个控制器类
控制器类别代码:
package view;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
public class MainController {
@FXML
private Label dpmadirektpropingenerator;
@FXML
private Button unlockPinButton;
@FXML
private Button confirmationPinButton;
}
此代码应该有效。
按如下方式修改主类:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class main extends Application {
public static void main(String[] args) {
Application.launch(main.class, args);
}
static Stage stg;
@Override
public void start(Stage stage) throws Exception {
this.stg = stage;
Parent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));
stage.setTitle("PIN-Generator");
stage.setScene(new Scene(root, 600, 400));
stage.show();
}
}
这是您的按键功能:
public void pressButton(ActionEvent event){
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sedondFXML.fxml"));
Parent root = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
main.stg.close();
} catch(Exception e) {
e.printStackTrace();
}
}
下面是一个关于同样问题的答案,它可能会有所帮助:在我的主代码中进行了一些编辑后,这非常有效:)非常感谢,您知道如何在打开第二阶段时关闭第一阶段吗?