首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaFX 8按钮点击显示其他场景

JavaFX 8按钮点击显示其他场景
EN

Stack Overflow用户
提问于 2017-03-29 07:19:11
回答 1查看 8.6K关注 0票数 1

你好,我目前正在为我的工作开发一个PIN生成器,由于我对Java完全陌生,所以我遇到了一些困难,特别是在使用JavaFX时。当单击每个按钮时,我想让程序显示另一个.fxml文件。

这是我的代码:

代码语言:javascript
复制
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();
        }
    }

我已经为场景中的每个按钮创建了一个控制器类。

控制器类代码:

代码语言:javascript
复制
    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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-29 07:32:18

这段代码应该能工作。

修改您的主类如下:

代码语言:javascript
复制
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();
    }
}

下面是您的pressButton函数:

代码语言:javascript
复制
 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();
      }
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43086875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档