JavaFX 从两个 FXML 操作标签

JavaFX Manipulate Labels from two FXML

我有一个计算器应用程序,它由 app.class - main.java 使用 controller.class - controller.java 运行,允许我操作两个 fxml - simplecalc.fxml和 engineercalc.fxml。菜单栏本身编码为 app.class。 我设法使用 MenuBar 中的复制按钮从 simplecalc.fxml 中的 Label 复制(到剪贴板)数据,但我在另一个 fxml - engineercalc 中未能这样做。我在 app.class 中使用控制器来操作这些标签。我如何同时从两个 fxml 复制?

Calculator visualisation

Switching between fxmls

main.java:

import java.io.IOException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.layout.BorderPane;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.MenuBar;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.KeyCombination;
import javafx.fxml.FXMLLoader;

public class Calcmain extends Application {

    public void start(Stage myStage) throws Exception{
        myStage.setTitle("Calculator");
        BorderPane rootNode = new BorderPane();
        Scene myScene = new Scene(rootNode, 235, 300);
        myStage.setScene(myScene);
        myStage.show();

        MenuBar mb = new MenuBar();
        Menu fileMenu = new Menu("_File");
        Menu editMenu = new Menu("_Edit");
        Menu viewMenu = new Menu("_View");
        Menu helpMenu = new Menu("_Help");


        fileMenu.setMnemonicParsing(true);
        editMenu.setMnemonicParsing(true);
        viewMenu.setMnemonicParsing(true);
        helpMenu.setMnemonicParsing(true);

        MenuItem openMenu = new MenuItem("Open");
        MenuItem exitMenu = new MenuItem("Exit");
        MenuItem copyMenu = new MenuItem("Copy");
        MenuItem pasteMenu = new MenuItem("Paste");
        MenuItem cutMenu = new MenuItem("Cut");
        MenuItem simpleMenu = new MenuItem("Standard");
        MenuItem engineerMenu = new MenuItem("Engineer");
        MenuItem aboutMenu = new MenuItem("About");

        openMenu.setAccelerator(KeyCombination.keyCombination(
"shortcut + O"));
        exitMenu.setAccelerator(KeyCombination.keyCombination(
"shortcut + E"));
        copyMenu.setAccelerator(KeyCombination.keyCombination(
"shortcut + C"));
        pasteMenu.setAccelerator(KeyCombination.keyCombination(
"shortcut + V"));
        cutMenu.setAccelerator(KeyCombination.keyCombination(
"shortcut + X"));
        simpleMenu.setAccelerator(KeyCombination.keyCombination(
"shortcut + 1"));
        engineerMenu.setAccelerator(KeyCombination.keyCombination(
"shortcut + 2"));
        aboutMenu.setAccelerator(KeyCombination.keyCombination(
"shortcut + A"));

        fileMenu.getItems().addAll(openMenu, new SeparatorMenuItem(), exitMenu);
        editMenu.getItems().addAll(copyMenu, pasteMenu, cutMenu);
        viewMenu.getItems().addAll(simpleMenu, new SeparatorMenuItem(), engineerMenu);
        helpMenu.getItems().addAll(aboutMenu);

        exitMenu.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent t) {
                Platform.exit();
                System.exit(0);
            }
        });

        simpleMenu.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent t) {
                Parent root;
                try {
                    root = FXMLLoader.load(getClass().getResource("Simplecalc.fxml"));
                    rootNode.setCenter(root);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

        FXMLLoader loader = new FXMLLoader(this.getClass().getResource("Simplecalc.fxml"));
        Parent root = loader.load();
        Controller controller = loader.getController();

        FXMLLoader loader2 = new FXMLLoader(this.getClass().getResource("Engineercalc.fxml"));
        Controller controller2 = loader2.getController();

        engineerMenu.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent t) {
                try {
                    Parent root2 = loader2.load();
                    rootNode.setCenter(root2);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });  
        mb.getMenus().add(fileMenu);
        mb.getMenus().add(editMenu);
        mb.getMenus().add(viewMenu);
        mb.getMenus().add(helpMenu);
        rootNode.setTop(mb);

        rootNode.setCenter(root);

        Clipboard clipboard = Clipboard.getSystemClipboard(); 
            Platform.runLater(new Runnable() {
                @Override public void run() {
                    copyMenu.setOnAction(new EventHandler<ActionEvent>() {
                        public void handle(ActionEvent t) {      
                        ClipboardContent content = new ClipboardContent();
                        content.putString(controller.display.getText());
                        clipboard.setContent(content);
                        }
                    });
                }
            });     
}

    public static void main(String[] args) {
        launch(args);       
    }
}

simplecalc.fxml:

<Label fx:id="display" alignment="CENTER_RIGHT" layoutY="2.0" prefHeight="98.0" prefWidth="235.0" AnchorPane.bottomAnchor="175.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="2.0">

engineercalc.fxml:

<Label fx:id="display" alignment="CENTER_RIGHT" layoutY="2.0" prefHeight="98.0" prefWidth="235.0" AnchorPane.bottomAnchor="175.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="2.0">

controller.java:

@FXML 
Label display;

它只适用于 simplecalc:

ClipboardContent content = new ClipboardContent();
content.putString(controller.display.getText());
clipboard.setContent(content);

当我尝试使用 engineercalc 时:

ClipboardContent content = new ClipboardContent();
content.putString(controller2.display.getText());
clipboard.setContent(content);

它调用:Exception in thread "JavaFX Application Thread" java.lang.NullPointerException

示例:

final Clipboard clipboard = Clipboard.getSystemClipboard();

copyMenu .setOnAction(new EventHandler<ActionEvent>() {
    public void handle(ActionEvent t) {            
        final ClipboardContent content = new ClipboardContent();
        content.putString("get test from textfield and put it here");
        clipboard.setContent(content);

    }
});

解决您的问题的一种方法是创建一个全局布尔值 isStandardShowing。设置为 true 是标准的 fxml 在启动时显示。在标准事件处理程序中,将布尔值设置为 true。在工程事件处理程序中将布尔值设置为 false。然后使用 if 语句从正确的显示中提取

    boolean isStandardShow = true;//This should be global.

    Clipboard clipboard = Clipboard.getSystemClipboard(); 
        Platform.runLater(new Runnable() {
            @Override public void run() {
                copyMenu.setOnAction(new EventHandler<ActionEvent>() {
                    public void handle(ActionEvent t) {      
                    ClipboardContent content = new ClipboardContent();
                    if(isStandardShow)
                    {
                        content.putString(controller.display.getText());
                    }
                    else
                    {                            
                        content.putString(OtherContorller.display.getText());
                    }
                    clipboard.setContent(content);
                    }
                });
            }
        });