我有一個計算器應用程序,它運行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處理標籤
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
您需要在您的菜單項事件處理 – Sedrick
你能,請把我的情況下,一個小例子拉? – L1oN
爲什麼你在Platform.runLater中放置clipboad處理程序? – Sedrick