我正在从事Java作业,所以我不是一个“绝地大师”。 当Java出现的时候,实际上更多地是在“培训轮子阶段”, 因此,Java FX舞台和场景仍然可以掌握。 无论如何,这里是一些原型代码,显示了我遇到的问题...
[这些都驻留在testingMenuaction包中]
我的主类:“ testingMenuaction_main.java”
package testingMenuaction;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class testingMenuaction_main extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getclass().getResource("/testingMenuaction/testingMenuaction.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我的主要Java FXML文件:“ testingMenuaction.fxml”
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane fx:id="testingMenuactionPage" prefHeight="195.0" prefWidth="494.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testingMenuaction.testingMenuactionController">
<children>
<VBox prefHeight="186.0" prefWidth="493.0">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="Data">
<items>
<MenuItem fx:id="mnu_data_import" mnemonicParsing="false" text="Import File" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</VBox>
</children>
</AnchorPane>
我的Java FXML文件的控制器类:“ testingMenuactionController.java”
package testingMenuaction;
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import javafx.stage.Window;
public class testingMenuactionController implements Initializable {
@FXML
private AnchorPane testingMenuactionPage;
@FXML
private MenuItem mnu_data_import;
//this tests the operation of the alert dialog
//private Stage export_chooser_stage;
//===================================================================================================================
/*
* This method will initialiise the UI
*/
@Override
public void initialize(URL url,ResourceBundle rb)
{
//--------------------------------------------------------------------------------------------------------------------
/*
* This is the mnu_data_import.setOnaction((event)
*/
mnu_data_import.setOnaction((event) ->
{
System.out.println("You clicked to Import Data");
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.getExtensionFilters().addAll(
new ExtensionFilter("Text Files","*.csv") );
File selectedFile = fileChooser.showOpenDialog( mnu_data_import.getScene().getWindow() );
String FileNamePath = selectedFile.toString();
System.out.println("File name and path to file :" + FileNamePath);
});// close
}// close public void initialize(URL url,ResourceBundle rb)
}// close public class testingMenuactionController
编译器抱怨以下指令:
"File selectedFile = fileChooser.showOpenDialog( mnu_data_import.getScene().getWindow() );"
给出以下错误信息... “对于MenuItem类型,未定义方法getScene()”
不幸的是,我对Java Windows,Stage,Scenes和Nodes的经验仍然很少 [因为我仍在使用“教练轮”。] ...
结果,有人可以帮助我吗?