我有一个计算器应用程序,它由app.class运行 - main.java使用controller.class - controller.java,它允许我操作两个fxmls - simplecalc.fxml和engineercalc.fxml。菜单本身在app.class中编码。 我设法使用MenuBar中的复制按钮从simplecalc.fxml中的Label复制(到剪贴板)数据,但我没能在另一个fxml - engineercalc中这样做。我在app.class中使用控制器来操作这些标签。我如何同时从两个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 :(得分: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);
}
});
答案 1 :(得分:0)
解决问题的一种方法是创建一个全局布尔值isStandardShowing。设置为true是启动时显示的标准fxml。在标准事件处理程序中,将布尔值设置为true。在Engineering事件处理程序中,将boolean设置为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);
}
});
}
});