2017-04-15 110 views
1

我有一個計算器應用程序,它運行app.class - main.java使用controller.class - controller.java,它允許我操縱兩個fxmls - simplecalc.fxml和engineercalc.fxml。 Menubar本身在app.class中編碼。 我設法使用MenuBar中的複製按鈕從simplecalc.fxml中複製(剪貼板)Label中的數據,但是我沒有在另一個fxml-engineercalc中這樣做。我在app.class中使用控制器來操縱這些標籤。我如何從兩個fxmls同時複製?JavaFX從兩個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

+0

您需要在您的菜單項事件處理 – Sedrick

+0

你能,請把我的情況下,一個小例子拉? – L1oN

+0

爲什麼你在Platform.runLater中放置clipboad處理程序? – Sedrick

回答

0

例如:

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); 

    } 
}); 
+0

以後我會試試,謝謝你的幫助 – L1oN

+0

順便問一下,這個方法是否會在剪貼板上貼上標籤(顯示)? – L1oN

+0

你需要做label.getText()它說...放在這裏。 – Sedrick

0

解決您的問題的一種方法是創建一個全局布爾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); 
        } 
       }); 
      } 
     }); 
+0

先生,您已經完成了很好的工作來幫助我。你的指導幫助我寫出約50%的應用程序。我非常感謝你的幫助。結果 - 所有的作品完美無缺,就像我想要的,儘管代碼充滿了柺杖) – L1oN

+0

很高興我能幫上忙。祝好運與其餘的編碼。 – Sedrick