It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                            
                        
                    
                
                                7年前关闭。
            
                    
我将问题简化为Scala + JavaFX 2的FXMLLoader的简单行:

val path = "/com/myapp/views/main.fxml"

val loader = new FXMLLoader()
loader.setLocation(getClass.getResource(path))

val root = loader.load(getClass.getResourceAsStream(path)).asInstanceOf[Parent]


我试图使用FXMLLoader加载main.fxml文件,但最终导致:


  类javafx.fxml.FXMLLoader $ ValueElement无法访问其成员
  修饰符为“ private”的com.myapp.controllers.MainWindow类


FXML代码如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.Scene?>
<?import javafx.scene.control.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>

<BorderPane fx:controller="com.myapp.controllers.MainWindow" fx:id="mainWindow" prefHeight="703.0" prefWidth="803.0" xmlns:fx="http://javafx.com/fxml">
  <stylesheets>
    <URL value="@../styles/Styles.css" />
  </stylesheets>
  <top>
  ...


根据错误消息,JavaFX的FXMLLoader尝试访问我控制器的私有属性。但是,控制器中没有私有成员:

class MainWindow extends Initializable {
  override def initialize(location: URL, resourceBundle: java.util.ResourceBundle) {
    print("init")
  }
}


可能是什么问题呢?

最佳答案

该错误指向私有构造函数。您是否需要在Scala中明确公开构造函数?您可以使用javap查看编译的内容。

10-08 13:15